子类就可以继承父类的所有成员和方法,包括私有属性,只是私有属性被继承下来却不能访问 只能通过public方法访问继承下来的私有属性 可以用set get 方法来访问
例如:- class A {
- private int a;
-
- public int getA(){
- return a;
- }
- public void setA(int a){
- this.a=a;
- }
- }
- class B extends A{
- private int b;
- public int getB() {
- return b;
- }
- public void setB(int b) {
- this.b = b;
- }
- }
复制代码 那么当B b1=new B()时。b1可以使用setA()getA()访问控制从A类中继承来的私有属性a;
楼主的意思是打印出一个经理的详细信息打印出来:可以定义一个show()方法 代码如下:- abstract class Employee
- {
- private String id;
- public String getid(){
- return id;
- }
- //public void setid(String id){
- // this.id=id;
- //}
- private String name;
- public String getname(){
- return name;
- }
- //public void setname(String name){
- // this.name=name;
- //}
- private double pay;
- public double getpay(){
- return pay;
- }
- //public void setpay(double pay){
- // this.pay=pay;
- //}
- public Employee()
- {}
- public Employee(String id,String name,double pay)
- {
- this.id = id;
- this.name = name;
- this.pay = pay;
- }
- public abstract void work();
- }
- class Manager extends Employee
- {
- private double bonus;
- public Manager(String id,String name,double pay,double bonus)
- {
- super(id,name,pay);
- this.bonus = bonus;
- }
- public void work()
- {
- System.out.println("manager work");
- }
- public void show(){
- System.out.println("id:"+super.getid()+",name:"+super.getname()+",pay:"+super.getpay()+",bonus:"+bonus);
- }
- }
- class Personnal extends Employee
- {
- public Personnal(String id,String name,double pay)
- {
- super(id,name,pay);
- }
- public void work()
- {
- System.out.println();
- }
-
- }
- class AbstractTest001
- {
- public static void main(String[] args)
- {
- new Manager("JL","zhangsan",500,500).show();
- }
- }
复制代码 楼主new Manager("JL","zhangsan",500,500).work(),只是调用了Manager类中的work()方法,自然只输出:manager work |