如何访问抽象超类实例变量 [英] How to access Abstract superclass instance variable
问题描述
所以我有两个类:Property
和 Houses
.Property
是抽象超类,Houses
是它的子类.
这是Property
公共抽象类属性{字符串代码;双倍价值;整数年;公共属性(字符串 pCode,双值,整数年){this.pCode = pCode;this.value = 值;this.year = 年;}公共财产(){pCode = "";值 = 0;年 = 0;}公共抽象无效折旧();//存取器私人字符串 getCode(){返回代码;}私人双 getValue(){返回值;}私人 int getYear(){回归年份;}//突变体私有无效集代码(字符串新代码){this.pCode = 新代码;}私有无效集值(双新值){this.value = newValue;}私有无效 setYear(int newYear){this.year = newYear;}公共字符串 toString(){return ("代码:" + getCode() + "
Value:" + getValue() + "
Year:" + getYear());}}
这是Houses
public class Houses extends Property{内部卧室;内部楼层;公共房屋(){极好的();//调用构造函数这.卧室= 0;this.storeys = 0;}公共房屋(字符串 pCode,双值,整数年,整数卧室,整数楼层){超级(代码,价值,年);this.bedrooms = 卧室;this.storeys = 楼层数;}//访问器私人 int getBedrooms(){返回卧室;}私人 int getStoreys(){返回楼层;}私人空间 setBedrooms(int 卧室){this.bedrooms = 卧室;}私有无效 setStoreys(int storeys){this.storeys = 楼层数;}公共无效折旧(){this.value = 95/100 * super.value;System.out.println(this.value);}公共字符串 toString(){返回 (super.toString() + "卧室:" + getBedrooms() + "Storeys:" + getStoreys());}}
我现在的问题是,在方法 depreciation
中,每当我尝试在 main
方法中运行它时,如下所示
public static void main(String[] args) {房屋 newHouses = new Houses("111",20.11,1992,4,2);newHouses.depreciation();}
它打印出 0.0 .为什么不打印 20.11 ?我该如何解决?
================================================/p>
已感谢您修复我的愚蠢错误>.<
但是,我们可以说我的财产正在使用
私有字符串 pCode;私人双重价值;私人整数年;
现在我无法访问它们,因为它们是私人访问,还有其他方法可以访问它们吗?
那是因为 95/100
是一个整数除法,结果是 0
.试试
0.95 * super.value
或
95.0/100 * super.value
So I have two classes: Property
and Houses
. Property
is the abstract super class and Houses
is its subclass.
Here is the code for Property
public abstract class Property{
String pCode;
double value;
int year;
public Property(String pCode, double value , int year){
this.pCode = pCode;
this.value = value;
this.year = year;
}
public Property(){
pCode = "";
value = 0;
year = 0;
}
public abstract void depreciation();
//Accessors
private String getCode(){
return pCode;
}
private double getValue(){
return value;
}
private int getYear(){
return year;
}
//Mutators
private void setCode(String newCode){
this.pCode = newCode;
}
private void setValue(double newValue){
this.value = newValue;
}
private void setYear(int newYear){
this.year = newYear;
}
public String toString(){
return ("Code: " + getCode() + "
Value: " + getValue() + "
Year: " + getYear());
}
}
Here is the code for Houses
public class Houses extends Property{
int bedrooms;
int storeys;
public Houses(){
super(); // call constructor
this.bedrooms = 0;
this.storeys = 0;
}
public Houses(String pCode , double value , int year ,int bedrooms , int storeys){
super(pCode,value,year);
this.bedrooms = bedrooms;
this.storeys = storeys;
}
//accessors
private int getBedrooms(){
return bedrooms;
}
private int getStoreys(){
return storeys;
}
private void setBedrooms(int bedrooms){
this.bedrooms = bedrooms;
}
private void setStoreys(int storeys){
this.storeys = storeys;
}
public void depreciation(){
this.value = 95 / 100 * super.value;
System.out.println(this.value);
}
public String toString(){
return (super.toString() + "Bedroom:" + getBedrooms() + "Storeys:" + getStoreys());
}
}
My problem now is that in the method depreciation
, whenever I try to run it in the main
method like the following
public static void main(String[] args) {
Houses newHouses = new Houses("111",20.11,1992,4,2);
newHouses.depreciation();
}
it prints out 0.0 . Why is it not printing 20.11 ? And how do I fix it?
==============================================
Edited : Thanks for fixing my silly error >.<
However lets just say my property were using
private String pCode;
private double value;
private int year;
now I'm not able to access them because they are private access, is there any other way to access them ?
That's because 95 / 100
is an integer division which yields 0
as a result. Try with
0.95 * super.value
or
95.0 / 100 * super.value
这篇关于如何访问抽象超类实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!