java学习笔记13-重写与重载

重写

重写是子类对父类允许访问的方法实现过程进行重新编写,返回值和参数都不能变。

重写方法不能抛出新的检查异常和比被重写方法更加宽泛的异常

访问权限不能比被重写方法低

声明为final的方法不能被重写

声明为static的方法不能被重写

子类和父类在同一个包中,子类可以重写父类除了声明为private和final的方法。不在同一个包中,子类只能重写父类声明为public和protected的非final方法

构造方法不能被重写

不是继承的方法,无法重写

 

在上一篇文章的代码进行改动,在FootBallPalyer和BasketBallPlayer中新增playBall()

public class Player {
    public int number;  //号码
    public int score;   //得分
    public String position; //司职
    public String name; //姓名

    public Player(){
        System.out.println("我是Player");
    }

    public void playBall(){
        System.out.println("姓名:"+this.name);
        System.out.println("号码:"+this.number);
        System.out.println("得分:"+this.score);
        System.out.println("司职:"+this.position);
    }
}
public class FootBallPlayer extends Player {
    public FootBallPlayer(){
        System.out.println("我是FootBallPlayer");
    }

    public void playBall(){
        System.out.println("我是一名足球运动员,我的名字叫"+this.name+",娱乐不能考手");
    }

    public static void main(String[] args){
        FootBallPlayer player2 = new FootBallPlayer();
        player2.name = "齐达内";
        player2.number = 10;
        player2.position = "中场";
        player2.score = 2;
        player2.playBall();
    }
}

public class BasketBallPlayer extends Player{
    public BasketBallPlayer(){
        System.out.println("我是BasketBallPlayer");
    }

    public void playBall(){
        System.out.println("我是一名篮球运动员,我叫"+this.name+",我从来不上脚");
    }

    public static void main(String[] args){
        BasketBallPlayer player1 = new BasketBallPlayer();
        player1.name = "jordan";
        player1.number = 23;
        player1.position = "得分后卫";
        player1.score = 33;
        player1.playBall();
    }
}

可以看到父类和子类都有相同返回和参数的playBall()方法,在调用时,调用了子类自己的playBall()方法。

 

重载

重载是在同一个类中,方法名字相同,而参数不同。返回类型也可以不同

每一个重载方法必须有一个独一无二的参数类型列表

重载方法必须改变参数列表,参数的个数或参数的类型

被重载方法可以改变返回类型,改变访问修饰符。可以声明新的更广的检查异常。

方法能够在同一个类中,或者子类中被重载

不能以返回类型来区别调用哪个方法

 

在Player类中新建带一个string参数的构造方法。在FootBallPlayer类和BasketBallPlayer类中使用super关键字调用父类的方法

public class Player {
    public int number;  //号码
    public int score;   //得分
    public String position; //司职
    public String name; //姓名

    public Player(){
        System.out.println("我是Player");
    }

    public Player(String club){
        System.out.println("俱乐部名称:"+club);
    }

    public void playBall(){
        System.out.println("姓名:"+this.name);
        System.out.println("号码:"+this.number);
        System.out.println("得分:"+this.score);
        System.out.println("司职:"+this.position);
    }
}

public class BasketBallPlayer extends Player{
    public BasketBallPlayer(){
        super("篮球俱乐部");
        System.out.println("我是BasketBallPlayer");
    }

    public void playBall(){
        System.out.println("我是一名篮球运动员,我叫"+this.name+",我从来不上脚");
    }

    public static void main(String[] args){
        BasketBallPlayer player1 = new BasketBallPlayer();
        player1.name = "jordan";
        player1.number = 23;
        player1.position = "得分后卫";
        player1.score = 33;
        player1.playBall();
    }
}

可以看到,调用的是对应带str参数的构造方法。

 

 

super关键字

super关键字是用来在子类中调用父类方法的关键字

当调用父类的构造方法时,super关键字需要在第一个行,可以看上面的例子,把super换到打印语句下面去。会有编译错误的提示

 

当调用的不是构造方法时,位置就随意了,在BasketBallPlayer类中添加helloPlayer()方法并调用

public class BasketBallPlayer extends Player{
    public BasketBallPlayer(){
        super("篮球俱乐部");
        System.out.println("我是BasketBallPlayer");
    }

    public void playBall(){
        System.out.println("我是一名篮球运动员,我叫"+this.name+",我从来不上脚");
    }

    public void helloPlayer(){
        System.out.println("Hello"+this.name);
        super.playBall();
        System.out.println("你好"+this.name);
    }

    public static void main(String[] args){
        BasketBallPlayer player1 = new BasketBallPlayer();
        player1.name = "jordan";
        player1.number = 23;
        player1.position = "得分后卫";
        player1.score = 33;
        player1.helloPlayer();
        player1.playBall();
    }
}

可以看到调用了父类的playBall()方法

posted @ 2019-06-12 13:32  梦忆安凉  阅读(257)  评论(0编辑  收藏  举报