有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java知道超类函数中的子类类型

我试着为那些在当地登记的人做一个算法,他们想为房产缴纳年税。我有一个属性的抽象类,它为其他类型的属性提供基本变量和方法。问题是,我想为一个已知的客户端打印所有属性,但我不知道如何通过超类获取属性类型

我考虑过在Properties类中声明另一个变量,它会告诉我是否有一个范围,然后,知道了这一点,我就会有一个建筑或一个字段。但这并不尊重多态性属性。如果我要推出一种新的房产,我需要改变一切。 我对java很陌生,没有其他线索。谢谢

public class Client {
private int CNP;
private String Name;
private int totalSum;
private Properties[] properties;

public Client(String Name, Properties[] properties) {
    this.Name = Name;
    this.properties = properties;
    setTotalSum(properties);
    CNP = 0;
}
public Client(int CNP, Properties[] properties) {
    Name = "No Name Specified";
    this.properties = properties;
    setTotalSum(properties);
    this.CNP = CNP;
}
public Client(String Name, int CNP, Properties[] properties) {
    this.Name = Name;
    this.properties = properties;
    setTotalSum(properties);
    this.CNP = CNP;
}

public String toString() {
    String s = "Contribuabil:   " + getName() + "\n" +
            "Proprietati:   \n";
    for(Properties property:properties) {
        s += property.toString();
    }
    s += "Suma totala:   " + getTotalSum();
    return s;
}

属性类

abstract class Properties {
private String nameAdress;
private int number;
private int paySum;

public Properties(String nameAdress, int number) {
    this.nameAdress = nameAdress;
    this.number = number;
}

public void setPaySum(int surface) {
    paySum = 500 * surface;
}
public void setPaySum(int surface, int rang) {
    paySum = (350 * surface) / rang;
}
public int getPaySum() {
    return paySum;
}
public String toString() {
 //here I would like to print the type of property;
}

亚班大楼

public class Building extends Properties {
private int surface;

public Building(String nameAdress, int number, int surface) {
    super(nameAdress, number);
    this.surface = surface;
    setPaySum(surface);
}

子类字段

public class Field extends Properties {
private int surface;
private int rang;

public Field(String nameAdress, int number, int surface, int rang) {
    super(nameAdress, number);
    this.surface = surface;
    this.rang = rang;
    setPaySum(surface, rang);
}

共 (0) 个答案