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

2 篇文章 0 订阅

在编写子类时,我们仍然可以声明成员变量,一种特殊的情况就是,所声明的成员变量的名字和从父类继承来的成员变量的名字相同(声明是类型可以不同),在这种情况下,子类就会隐藏所继承的成员变量。
子类隐藏继承的成员变量特点如下:
1.子类对象以及子类自己定义的方法操作与父类同名的成员变量是指子类重新声明的这个成员变量。
2.子类对象仍然可以调用从父类继承的方法操作被子类隐藏的成员变量,也就是说,子类继承的方法所操作的成员变量一定是被子类继承或者隐藏的成员变量。
下面举个例子:
父类:

package Example7;

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

子类继承父类成员变量被隐藏

package Example7;

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

主函数

package Example7;

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

调试结果

int型的weight =198
对象cheapGood的weight值是:198
cheapGoods用子类新增的优惠方法计算价格:1980.0
double型的weight =198.555
cheapGoods使用继承的方法(无优惠)计算价格:1985.5500000000002

关于方法的重写:
方法的重写也称作方法覆盖。
重写的语法规则:
如果子类可以继承父类的某个方法,那么子类就有权利重写这个方法。那么子类就有权利重写这个方法。方法重写是指子类中定义一个方法,这个方法的类型和父类的方法的类型一致或者是父类的方法的类型的子类型。(所谓的子类型,是指如果父类 的方法是“类”,那么允许子类重写方法的类型的是“子类”),并且这个方法的名字、参数个数、参数的类型的父类的方法完全相同。子类如此定义的方法叫做子类的重写的方法(不属于新增的方法)。
重写的目的:
子类通过方法的重写可以隐藏继承的方法,子类通过方法的重写可以吧父类的状态和行为改变为自身的状态和行为。如果父类的方法可以被子类继承,子类就有权利重写该方法,一旦子类重写了父类的方法,那么子类就银城了继承的方法,那么子类调用的方法一定是重写的方法。
下面举个例子:
父类

package Example8;

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

子类,重写方法

package Example8;

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

    }
}

主函数调用子类;

package Example8;

public class Demo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        double math = 62,english = 76, Chinese = 69;
        ImportanceUniversity importanceUniversity = new ImportanceUniversity();
        importanceUniversity.entreRule(math, english, Chinese);
        //调用重写方法
        math = 90;
        english = 100;
        Chinese = 77;
        importanceUniversity.entreRule(math, english, Chinese);
    }

}

调试结果:

207.0分数未达到大学录取线
267.0分数达到重点大学录取线

下面我们再来看一个例子:
父类:

package Example9;

public class Father {
    float computer(float x,float y){
        return x + y;
    }
    public int g(int x,int y){
        return  x + y;
    }

}

子类

package Example9;

public class Sun extends Father{
    float computer(float x, float y){
        return x*y;
    }
//  double computer(float x,float y){
//      return x*y;
//  }
/*
 * 这种写法是错误的写法,首先 这个方法的类型是double类型,很显然不是重写父类的方法,而这个方法的
 * 方法名又和父类的computer方法同名,并且参数也相同,这样的写法是不正确的。
 */
}

主函数

package Example9;

public class Demo {

    public static void main(String[] args) {
        Sun sun = new Sun();
        double result = sun.computer(8, 9);//调用重写方法
        System.out.println(result);
        int m = sun.g(15, 4);//调用Sun继承的方法
        System.out.println(m);
    }

}

调试结果

72.0
19

这边需要注意的是:
重写父类方法时,不允许降低方法的访问权限,但是可以提高访问权限。
访问权限从高到低的排序如下:
public、protect,友好的,private

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值