我正在做一個 Java 練習,其中父類是抽象的,而子類會覆寫該isSimilar方法。此方法應采用設備引數。在Server子類中,此方法應比較品牌、作業系統和可用服務。在NetworkDevice子類中,此方法應比較品牌、埠數和以太網供電。
這種方法的問題是,如果它只接受一個引數,我無法比較設備。(至少我不知道該怎么做。)
-- 設備類 --
public abstract class Device {
protected int id;
protected String brand;
// The Constructor Methods.
public Device(){}
public Device(int id, String brand){
this.id = id;
this.brand = brand;
}
// The Get Methods.
public int getId() {return id;}
public String getBrand() {return brand;}
// The Set Methods.
public void setId(int id) {this.id = id;}
public void setBrand(String brand) {this.brand = brand;}
// Returning Information.
public String toString(){
String s = "Id; " brand ", Brand: " brand ". ";
return s;
}
// Other Abstract Classes.
public abstract boolean isSimilar(Device d);
}
-- 服務器類 --
public class Server extends Device{
private String operatingSystemType;
private String availableServices;
// The Constructor Methods.
public Server(){}
public Server(int id, String brand, String operatingSystemType, String availableServices){
super(id, brand);
this.operatingSystemType = operatingSystemType;
this.availableServices = availableServices;
}
// The Get Methods.
public String getOperatingSystemType() {return operatingSystemType;}
public String getAvailableServices() {return availableServices;}
// The Set Methods.
public void setOperatingSystemType(String operatingSystemType) {this.operatingSystemType = operatingSystemType;}
public void setAvailableServices(String availableServices) {this.availableServices = availableServices;}
// Server Related Methods.
public boolean addAService(String service){
String[] services = getAvailableServices().split(":");
for (int i = 0; i < services.length; i ) {
if (services[i].equalsIgnoreCase(service))
return false;
}
setAvailableServices(getAvailableServices() ":" service);
return true;
}
public boolean findAService(String service){
String[] services = getAvailableServices().split(":");
for (int i = 0; i < services.length; i ) {
if (services[i].equalsIgnoreCase(service))
return true;
}
return false;
}
@Override
public boolean isSimilar(Device d) {
if(d.getBrand().equalsIgnoreCase(getBrand())) {
return true;
}
return false;
}
// Returning Information.
@Override
public String toString() {
String s = super.toString() ", Operating System: " getOperatingSystemType() ", Services: " getAvailableServices() ".";
return s;
}
}
-- 網路設備類 --
public class NetworkDevice extends Device{
private int numberOfPorts;
private boolean poweredByEthernet;
// The Constructor Methods.
public NetworkDevice(){}
public NetworkDevice(int id, String brand, int numberOfPorts, boolean poweredByEthernet){
super(id, brand);
this.numberOfPorts = numberOfPorts;
this.poweredByEthernet = poweredByEthernet;
}
// The Get Methods.
public int getNumberOfPorts() {return numberOfPorts;}
public boolean isPoweredByEthernet() {return poweredByEthernet;}
// The Set Methods.
public void setNumberOfPorts(int numberOfPorts) {this.numberOfPorts = numberOfPorts;}
public void setPoweredByEthernet(boolean poweredByEthernet) {this.poweredByEthernet = poweredByEthernet;}
@Override
public boolean isSimilar(Device d) {
if(d.getBrand().equalsIgnoreCase(getBrand()))
return true;
return false;
}
// Returning Information.
@Override
public String toString() {
return super.toString() "NetworkDevice{"
"numberOfPorts=" numberOfPorts
", poweredByEthernet=" poweredByEthernet
'}';
}
}
uj5u.com熱心網友回復:
你的方法是正確的。為澄清起見,每當您重寫 isSimilar 方法時,您都可以使用此關鍵字來顯示將在傳遞的設備品牌和呼叫該方法的物件之間進行比較。您對 isSimilar 的實作也是正確的,但您可以通過將其保留為一個襯墊來優化它。
@Override
public boolean isSimilar(Device d) {
return d.getBrand().equalsIgnoreCase(this.getBrand()) ;
}
現在比較它們,我創建了一個主類,在其中創建類服務器和網路設備的物件并呼叫 isSimilar 方法。
public class Main
{
public static void main(String args[]) {
Device deviceA = new Server(1,"samsung","core-4","ten");
Device deviceB = new Server(1,"samsung","core-4","twenty");
System.out.println(deviceA.isSimilar(deviceB));
Device deviceC = new NetworkDevice(1,"samsung",4,false);
Device deviceD = new NetworkDevice(1,"galaxy",6,true);
System.out.println(deviceC.isSimilar(deviceD));
}
}
輸出如下
true
false
uj5u.com熱心網友回復:
如果我正確理解了分配,只有當設備具有相同的子類和相同的屬性時 isSimilar 才為真。
在這種情況下,您可以簡單地檢查設備型別并將其轉換為子類。例如,對于 Server 類
@Override
public boolean isSimilar(Device d) {
if (d == null) {
return false;
}
if (d.getClass() != this.getClass()) {
return false;
}
final Server otherServer = (Server) d;
return this.getBrand().equalsIgnoreCase(otherServer.getBrand()) &&
this.getOperatingSystemType().equalsIgnoreCase(otherServer.getOperatingSystemType()) &&
this.getAvailableServices().equalsIgnoreCase(otherServer.getAvailableServices());
}
順便說一句,這種型別的邏輯讓我想起了默認的 java 物件函式 - equals()。(https://www.baeldung.com/java-equals-hashcode-contracts)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/412246.html
標籤:
