【发布时间】:2022-01-15 05:33:33
【问题描述】:
我正在做一个 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 +
'}';
}
}
【问题讨论】:
-
您可能需要检查有关覆盖
equals()方法的问题,例如stackoverflow.com/questions/8180430/…,因为问题可能类似。
标签: java oop inheritance abstract-class