package P162_第一小题;
class Student {
protected String name;//如果不用protected,想想会发生什么?
protected int age;//
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public void show() {
System.out.println(name + " 的年龄是 " + age);
}
}
class Undergraduate extends Student {
private String degree;
public Undergraduate(String name, int age, String degree) {
super(name, age);
this.degree = degree;
}
public void show() {
System.out.println(name + " 的年龄是 " + age + ", 学位是" + degree);
}
}
public class Test {
public static void main(String[] args) {
new Student("郭浩东", 20).show();
new Undergraduate("郭浩东",20,"本科").show();
}
}
如何恰到好处地使用protected
最新推荐文章于 2023-12-05 23:46:08 发布