摘要 类 和 Java 中的方法

Abstract Classes & Methods In Java

我正在做一个 Java 练习,其中父 class 是抽象的,子 class 重写 isSimilar 方法。此方法应采用设备参数。在 Server subclass 中,此方法应该比较品牌、操作系统和可用服务。在 NetworkDevice 子 class 中,此方法应比较品牌、端口数和以太网供电。

这种方法的问题是,如果只接受一个参数,我无法比较设备。 (至少我不知道该怎么做。)

-- 设备 Class --

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);
}

-- 服务器 Class --

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;
    }
}

--网络设备Class--

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 +
                '}';
    }
}

你的做法是正确的。 为了澄清起见,无论何时覆盖 isSimilar 方法,您都可以使用此关键字来表明比较将在传递的设备品牌和调用的对象之间进行 方法。你对 isSimilar 的实现也是正确的,但你可以通过将它保持为一个衬里来优化它。

  @Override
  public boolean isSimilar(Device d) {
        return d.getBrand().equalsIgnoreCase(this.getBrand()) ;
  }

现在为了比较它们,我创建了一个主要 class,我在其中创建 class 服务器和网络设备的对象并调用 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

如果我对分配的理解正确,只有当设备具有相同的子class、具有相同的属性时,isSimilar 才为真。

在这种情况下,您只需检查设备类型并将其转换为子class。例如,对于服务器 class

@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)