方法重写和属性隐藏

前言

在本篇中,我们会介绍继承中会涉及的属性隐藏和方法的重写,前面有篇文章介绍了方法的回顾和加深,基础的内容我们不再赘述。

简介

子类可以继承父类的成员,也可以隐藏父类的属性和重写父类的方法。

属性的隐藏:子类与其父类中有同名的属性时,在子类中该属性隐藏父类的同名属性。子类不能引用父类的被隐藏属性。但可以通过super访问父类的隐藏属性。

方法的重写:子类的方法与其父类的方法的原型相同时,子类的方法重写了父类的方法——子类的方法改变了父类方法的行为,与父类方法的行为不同。

方法的重写要求:子类和父类方法的原型相同。

方法的重写包括三种:静态方法,实例方法,接口的重写。在本篇中,关于后两种我们不做介绍。下面我们给出两个例子,分别作为对属性隐藏和方法重写的具体应用,从而对其有更深的理解。

两个例子

例子1—关于属性隐藏

package com.oop.Rew;
//父类是银行类 有三个属性
class Bank {
    int savedMoney; //余额
    int year;       //存款年限
    double interest;//利息
    //银行内有一个计算利息的方法
    public double computerInterest() {
        interest=year*0.035*savedMoney;
        System.out.printf("%d元存在银行%d年的利息:%f元\n",
                savedMoney,year,interest);
        return interest;
    }
}
//子类是建设银行类,从银行类中派生而来
class ConstructionBank extends Bank {
    double year;//建设银行类中定义了一个存款年限的属性,该属性隐藏了其父类银行类的存款年限的属性
    public double computerInterest() {
        super.year=(int)year;
        double remainNumber=year-(int)year;
        int day=(int)(remainNumber*1000);
        interest=super.computerInterest()+day*0.0001*savedMoney;
        System.out.printf("%d元存在建设银行%d年零%d天的利息:%f元\n",
                savedMoney,super.year,day,interest);
        return interest;
    }
}

public class Example03 {

    /*
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        int amount=5000;
       //在主类中,我们定义一个建设银行类对象,设置其
        ConstructionBank bank=new ConstructionBank();
        bank.savedMoney=amount;
        bank.year=5.216;
        double interest=bank.computerInterest();
        System.out.println("银行利息 = " + interest);
    }

}

例子2—关于方法重写

package com.oop.Rew;
//父类 普通大学类
class University {
    void enterRule(double math,double english,double chinese) {
        double total=math+english+chinese;
        if(total>=200)
            System.out.println("考分"+total+"达到大学最低录取线");
        else
            System.out.println("考分"+total+"未达到大学最低录取线");
    }
}
//子类 重点大学类
class ImportantUniversity extends University{
    void enterRule(double math,double english,double chinese) {
        double total=math+english+chinese;
        if(total>=245)
            System.out.println("考分"+total+"达到重点大学最低录取线");
        else
            System.out.println("考分"+total+"未达到重点大学最低录取线");
    }
}

public class Example04 {
    public static void main(String[] args) {
        double math=64,english=76.5,chinese=66;
        ImportantUniversity univer = new  ImportantUniversity();
        univer.enterRule(math,english,chinese); //调用重写的方法
        math=89;
        english=80;
        chinese=86;
        univer = new  ImportantUniversity();
        univer.enterRule(math,english,chinese); //调用重写的方法
    }

}

重写的要求

需要有继承关系,子类重写父类的方法!
1.方法名必须相同
2.参数列表必须相同
3.修饰符:范围可以扩大,但不能缩小;
4.抛出的异常:范围,可以被缩小,但不能扩大;ClassNotFoundException -->Exception(大)
重写:子类的方法和父类必须要一致;方法体不同!

为什么需要重写?

父类的功能,子类不一定需要,或者不一定满足!
方法重写的IDEA快捷键:Alt+Fn+Insert:选中Override

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值