Как получить доступ к переменной экземпляра абстрактного суперкласса
Итак, у меня есть два класса:Property
а такжеHouses
. Property
абстрактный суперкласс иHouses
это его подкласс.
Вот код для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() + "\nValue: " + getValue() + "\nYear: " + getYear());
}
}
Вот код для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());
}
}
Моя проблема сейчас в том, что в методеdepreciation
всякий раз, когда я пытаюсь запустить его вmain
метод, подобный следующему
public static void main(String[] args) {
Houses newHouses = new Houses("111",20.11,1992,4,2);
newHouses.depreciation();
}
он печатает 0.0. Почему не печатается 20.11? И как мне это исправить?
==============================================
Отредактировано: Спасибо за исправление моей глупой ошибки>. <
Однако давайте просто скажем, что моя собственность использовала
private String pCode;
private double value;
private int year;
теперь я не могу получить к ним доступ, потому что это частный доступ, есть ли другой способ получить к ним доступ?