import java.util.Scanner; class Employee { private String name;// 职员名称 public Employee() { }; public Employee(String s) {// 构造函数,初始化职员名称 this.name = s; } public String GetName() { // 获取职员名称 return name; } } class Manager extends Employee{ String department; public Manager(String a,String b){ super(a); department=b; } public String GetDepartment(){ return department; } } //write your own codes public class Main { public static void main(String[] args) { Scanner sca = new Scanner(System.in); String str = sca.next(); String str1 = sca.next(); Manager m = new Manager(str, str1); System.out.println("name:"+m.GetName()+" department:"+m.department); } }
****************************************************************************
子类在调用父类的构造函数时,需要使用super();来调用父类中隐藏的变量;
子类成员变量无法直接调用父类私有变量。
时间: 2024-10-24 20:44:33