繁体   English   中英

Java 中的抽象类和方法

[英]Abstract Classes & Methods In Java

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

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

-- 器件 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 方法时,您可以使用此关键字来显示将在传递的设备品牌和调用该方法的 object 之间进行比较。 您对 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));
    
  }
}

output如下

true
false

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

在这种情况下,您可以简单地检查设备类型并将其转换为子类。 例如,对于服务器 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());

}

By the way this type of logic reminds me default java object function - equals().( https://www.baeldung.com/java-equals-hashcode-contracts )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM