如何访问Abstract超类实例变量 [英] How to access Abstract superclass instance variable

查看:171
本文介绍了如何访问Abstract超类实例变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有两个类: Property Houses 属性是抽象超类, Houses 是它的子类。

So I have two classes: Property and Houses. Property is the abstract super class and Houses is its subclass.

以下是属性的代码

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());
    }
}

以下是的代码房屋

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());
    }

}

我现在的问题是方法折旧,每当我尝试在 main 方法中运行它时,如下所示

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();
     }

打印出0.0。为什么不打印20.11?我该如何解决?

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 ?

推荐答案

那是因为 95/100 是整数除法结果产生 0 。尝试

That's because 95 / 100 is an integer division which yields 0 as a result. Try with

0.95 * super.value

95.0 / 100 * super.value

这篇关于如何访问Abstract超类实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆