手册
目录
收藏776
分享
阅读792
更新时间2025-07-22
前言:
定义和用法protected 关键字是用于属性、方法和构造函数的访问修饰符,使它们在同一包和子类中可访问。实例Student 子类访问带有 protected
protected 关键字是用于属性、方法和构造函数的访问修饰符,使它们在同一包和子类中可访问。
Student 子类访问带有 protected 属性的 Person 类:
class Person {
  protected String fname = "Bill";
  protected String lname = "Gates";
  protected String email = "bill@doe.com";
  protected int age = 24;
}
class Student extends Person {
  private int graduationYear = 2024;
  public static void main(String[] args) {
    Student myObj = new Student();
    System.out.println("名字:" + myObj.fname + " " + myObj.lname);
    System.out.println("电邮:" + myObj.email);
    System.out.println("年龄:" + myObj.age);
    System.out.println("毕业年:" + myObj.graduationYear);
  }
}
点击 "运行实例" 按钮查看在线实例
相关
视频
RELATED VIDEOS
科技资讯
1
2
3
4
5
6
7
8
9
精选课程
							
							共5课时
17.2万人学习
							
							共49课时
77万人学习
							
							共29课时
61.7万人学习
							
							共25课时
39.3万人学习
							
							共43课时
70.9万人学习
							
							共25课时
61.6万人学习
							
							共22课时
23万人学习
							
							共28课时
33.9万人学习
							
							共89课时
125万人学习