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);
}
解决方案
推荐阅读
- angularjs - Firestore:snapshotChanges 不是函数
- php - 寻找将小时和分钟附加到修改后的 Datetime 对象的更好方法
- jenkins - 使用 Jenkins 触发器执行管道的测试套件运行
- javascript - 如何使用 Javascript 淡出元素
- sql - 获取要添加的值加 1 以在 SQL Server 存储过程中工作
- docker - 如何在 Docker Desktop 上创建新的 Kubernetes 集群?
- python - 获取具有多个元素的数组的真值以进行模糊变换
- python - 在 Python 中解析来自 AWS 开发工具包的 Cloudformation 字符串响应
- java - 如何使用 poi 库获取 excel 文件中特定列的行数
- javascript - 使用 HTML 值数组来选择复选框