在继承中使用的protected

  1. 继承
  2. 在继承中使用protected
  3. 使用规范

一般情况下,我们只会用到public和private。
protected关键字的使用场景只会出现在继承中,所以我们称之为在继承中使用的protected。

继承

下面先展示一个简单的继承demo:

class Person {
    private String name;
    private int age;

    public Person(String name,int age){
        this.name=name;
        this.age=age;
    }

    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name=name;
    }
    public int getAge() {
        return this.age;
    }
    public void setAge(int age) {
        this.age=age;
    }

}

class Student extends Person {
    private int score;

    public Student(String name,int age,int score){
        super(name,age);
        this.score=score;
    }
    public int getScore() {
        return this.score;
    }
    public void setScore(int score) {this.score=score;}
}

public class Hello {
    public static void main(String[] args) {
        Person p1 = new Student("Xiao Ming",12,90);

        System.out.println(p1.getName() + ", " + p1.getAge());   //   Xiao Ming, 12

        Student s1 = (Student) p1;
        System.out.println(s1.getName() + ", " + s1.getAge() + ", " + s1.getScore());   //   Xiao Ming, 12, 90

    }
}

向上转型:把子类型安全变为更抽象的父类型,比如上面把Student类型变为Person类型。向上转型后只能访问抽象后的Person类型的内容,如果需要访问子类型的字段或方法时则需要再次将对象转为子类型。

在继承中使用protected

我们要为Student类添加一个introduce方法,如下是报错的,因为子类无法访问父类的private字段或者private方法。

class Student extends Person {
    private int score;

    public Student(String name,int age,int score){
        super(name,age);
        this.score=score;
    }
    public int getScore() {
        return this.score;
    }
    public void setScore(int score) {this.score=score;}

    public void introduce() {
        System.out.println("Hello,my name is " + this.name);
    }
}

这时我们可以使用protected关键字,用protected修饰的字段可以保证外部无法直接访问Person类的属性,同时支持继承Person类的子类Student可以访问,改写后如下:

class Person {
    protected String name;
    protected int age;

    public Person(String name,int age){
        this.name=name;
        this.age=age;
    }

    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name=name;
    }
    public int getAge() {
        return this.age;
    }
    public void setAge(int age) {
        this.age=age;
    }

}

class Student extends Person {
    private int score;

    public Student(String name,int age,int score){
        super(name,age);
        this.score=score;
    }
    public int getScore() {
        return this.score;
    }
    public void setScore(int score) {this.score=score;}

    public void introduce() {
        System.out.println("Hello,my name is " + this.name);
    }
}

public class Hello {
    public static void main(String[] args) {
        Student s1 = new Student("Xiao Ming",12,90);
        s1.introduce();    //    Hello,my name is Xiao Ming
    }
}

使用规范

注意,如果不涉及继承去访问父类的属性或方法,那么就不要用protected关键字。
如上Student类不需要被继承,所以里面的score属性还是private并且编写对应的public接口供外部间接访问。


他の者にできたか?ここまでやれたか?この先できるか?いいや、仆にしかできない!

目录
×

喜欢就点赞,疼爱就打赏