protected关键字

protected关键字主要作用:控制类的继承者的访问权限。

(1)对于一个包含protected成员的类来说,其他任何位于同一包中的类(当然包括该类的子类)都可以访问其protected成员--包内访问权限;

(2)对于一个包含protected成员的类来说,继承该类的子类(不管该子类与其父类位于同一包中还是不同包中)都可以访问其protected成员

例如:

People.java:

package protecteddemo;
public class People {
    private String name;
    public People(String name){
        this.name = name;
    }
    protected void setName(String name){
        this.name = name;
    }
    public String toString(){
        return "Class People: " + name;
    }
    public static void main(String[] args){
        People people = new People("huhu");
        System.out.println(people);
    }
}

运行结果:

Class People: huhu

1.同一包中的其他类(均可访问People的protected成员):

(1)非子类:

package protecteddemo;

public class Student{
    private int sno;
    public Student(String name, int sno){
        this.sno = sno;
    }
    public void change(String name, int sno){
        new People("huhu").setName(name);
        this.sno = sno;
    }
    public String toString(){
        return "Class Student: " + sno + " ";
    }
    public static void main(String[] args){
        Student student = new Student("huhu", 1);
        System.out.println(student);
        student.change("huahua", 2);
        System.out.println(student);
    }
}

运行结果:

Class Student: 1
Class Student: 2


(2)子类:

package protecteddemo;

public class Student extends People {
    private int sno;
    public Student(String name, int sno){
        super(name);
        this.sno = sno;
    }
    public void change(String name, int sno){
        setName(name);
        this.sno = sno;
    }
    public String toString(){
        return "Class Student: " + sno + " " + super.toString();
    }
    public static void main(String[] args){
        Student student = new Student("huhu", 1);
        System.out.println(student);
        student.change("huahua", 2);
        System.out.println(student);
    }
}

运行结果:

Class Student: 1 Class People: huhu
Class Student: 2 Class People: huahua


2.不同包的其他类(只有People的子类可以访问People的protected成员):

(1)非子类(编译报错):

package protecteddemo2;
import protecteddemo.People;
public class Student{
    private int sno;
    public Student(String name, int sno){
        this.sno = sno;
    }
    public void change(String name, int sno){
        // 报错:setName(java.lang.String)具有protected访问权限,不可以protecteddemo2包中访问
        new People("huhu").setName(name);
        this.sno = sno;
    }
    public String toString(){
        return "Class Student: " + sno + " ";
    }
    public static void main(String[] args){
        Student student = new Student("huhu", 1);
        System.out.println(student);
        student.change("huahua", 2);
        System.out.println(student);
    }
}


(2)子类:

package protecteddemo2;
import protecteddemo.People;
public class Student extends People {
    private int sno;
    public Student(String name, int sno){
        super(name);
        this.sno = sno;
    }
    public void change(String name, int sno){
        setName(name);
        this.sno = sno;
    }
    public String toString(){
        return "Class Student: " + sno + " " + super.toString();
    }
    public static void main(String[] args){
        Student student = new Student("huhu", 1);
        System.out.println(student);
        student.change("huahua", 2);
        System.out.println(student);
    }
}

运行结果:

Class Student: 1 Class People: huhu
Class Student: 2 Class People: huahua


转载于:https://my.oschina.net/tashi/blog/188834

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值