成员变量的隐藏和方法重写

一:成员变量的隐藏

子类继承的方法只能操作子类继承和隐藏的成员变量。子类新定义的方法可以操作子类继承和子类新声明的成员变量,但无法操作子类隐藏的成员变量(需使用super关键字操作子类隐藏的成员变量(下次更新super关键字))。

Goods.java

 

package chengyuanbianliangdeyincang;

public class Goods {
    public double weight;
    public void oldSetWeight(double w) {
        weight=w;
        System.out.println("double型的weight="+weight);
    }
    
    public double oldGetPrice() {
        double price=weight*10;
        return price;
    }
}

 

 

 

 

CheapGoods.java

 

package chengyuanbianliangdeyincang;

public class CheapGoods extends Goods{
    public int weight;
    public void newSetWeight(int w) {
        weight=w;
        System.out.println("int型的weight="+weight);
    }
    
    public double newGetPrice() {
        double price=weight*10;
        return price;
    }
}

 

 

 

 

yincang.java

 

package chengyuanbianliangdeyincang;

public class yincang {

    public static void main(String[] args) {
        CheapGoods cheapGoods=new CheapGoods();
        //cheapGoods.weight=198.98;  是非法的,因为子类对象的weight变量已经是int型
        cheapGoods.newSetWeight(198);
        System.out.println("对象cheapGoods的weight的值是:"+cheapGoods.weight);
        System.out.println("cheapGoods用子类新增的优惠方法计算价格:"+cheapGoods.newGetPrice());
        cheapGoods.oldSetWeight(198.987);   //子类对象调用继承的方法操作隐藏的double型变量weight
        System.out.println("cheapGoods使用继承的方法(无优惠计算价格)"+cheapGoods.oldGetPrice());
    }

}

 

 

 

二:方法重写

1.重写的语法规则

  如果子类可以继承父类的某个方法,那么子类就有权利重写这个方法。所谓方法重写,是指子类中定义一个方法,这个方法的类型和父类的方法的类型一致或者是父类的方法的类型的子类型(所谓子类型,是指如果父类的方法的类型是“类”,那么允许子类的重写方法的类型是“子类”),并且这个方法的名字,参数个数,参数的类型和父类的方法完全相同,子类如此定义的方法称作子类重写的方法。

2.重写的目的

  重写方法既可以操作继承的成员变量、调用继承的方法,也可以操作子类新声明的成员变量、调用新定义的其他方法,但无法操作被子类隐藏的成员变量和方法。如果子类想使用被隐藏的方法或成员变量,必须使用关键字super(下次更新super用法)。

 

在下面的例子中,ImportantUniversity是University类的子类,子类重写了父类的enterRule()方法。

University.java

 

package fangfachongxie;

public class University {
    void enterRule(double math,double english,double chinese) {
        double total=math+english+chinese;
        if(total>=180) {
            System.out.println(total+"分数达到大学录取线");
        }
        else {
            System.out.println(total+"分数未达到大学录取线");
        }
    }
}

 

 

 

ImportantUniversity.java

 

package fangfachongxie;

public class ImportantUniversity extends University {
    void enterRule(double math,double english,double chinese) {
        double total=math+english+chinese;
        if(total>=220) {
            System.out.println(total+"分数达到重点大学录取线");
        }
        else {
            System.out.println(total+"分数未达到重点大学录取线");
        }
    }
}

 

 

 

Chongxie.java

 

package fangfachongxie;

public class Chongxie {

    public static void main(String[] args) {
        double math=62,english=76.5,chinese=67;
        ImportantUniversity univer=new ImportantUniversity();
        univer.enterRule(math, english, chinese);    //调用重写的方法
        math=91;
        english=82;
        chinese=86;
        univer.enterRule(math,english,chinese);
    }

}

 

 

运行结果如下:

205.5分数未达到重点大学录取线
259.0分数达到重点大学录取线

 

3.重写的注意事项

  重写父类的方法时,不允许降低方法的访问权限,但可以提高访问权限(访问限制修饰符按访问权限从高到低的排列顺序是public、protected、友好的、private)。下面的代码中,子类重写父亲的方法w,该方法在父类中的访问权限是protected级别,子类重写时不允许级别低于protected,例如:

class A{
    protected float w(float x,float y) {
        return x-y;
    }
}

class B extends A{
    float w(float x,float y) {    //非法,因为降低了访问权限
        return x+y;
    }
}

class C extends A{
    public float w(float x,float y) {    //合法,提高了访问权限
        return x*y;
    }
}

 

posted @ 2019-10-15 16:00  失忆_诗意  阅读(844)  评论(0编辑  收藏  举报