我正在做一个 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)
0 评论