函数的复写

1. 函数的复写(override)

2. 使用super调用父类的成员函数

 

1. 函数的复写

    修改父类中成员函数, 就叫复写

2. 使用super调用父类的成员函数   

    this()就可调用本类的构造函数, this.函数名 即可调用本类的成员函数

    super()可调用父类的构造函数, super.函数名 即可调用父类的成员函数

    override语法特征:<1>在具有父子关系的两个类中

                             <2>父类和子类各有一个函数, 函数的定义(返回值类型、函数名和参数列表)完全相同

    Eg. 

 

1 class Person{
2     int age ;
3     String name ;
4     void introduce(){
5         System.out.println("我的名字是" + this.name + ",我的年龄是" + this.age);        
6     } 
7 }

 

 

1 class Student extends Person{
2     String address ;
3     
4     void introduce(){
5         System.out.println("我的名字是" + this.name + ",我的年龄是" + this.age);    
6         System.out.println("我的家在" + this.address);
7     } 
8 }
1 class Test{
2     public static void main(String args []){
3         Student student = new Student();
4         student.age = 10 ;
5         student.name = "张三" ;
6         student.address = "北京";
7         student.introduce();
8     }
9 }

         Tips : javac *.java可编译当前目录中所有java文件 

             

 

2. 使用super调用父类的成员函数 

      上述代码中, Student有重复代码, 修改如下

     

1 class Student extends Person{
2     String address ;
3     
4     void introduce(){
5         super.introduce();
6         System.out.println("我的家在" + this.address);
7     } 
8 }

     

 

转载于:https://www.cnblogs.com/iMirror/p/3736190.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值