//parent class class Employee{ public Employee(){ System.out.println("Employee"); } public void print() { System.out.println("Employee Details"); } } // child class class Manager extends Employee { public Manager() { System.out.println("Manager"); } public void print() { System.out.println("Manager Details"); } } // child class class Supervisor extends Employee { public Supervisor() { System.out.println("Supervisor"); } public void print() { System.out.println("Supervisor Details"); } } class Test { public static void main(String[] args) { Employee emp1, emp2; //reference Manager m1; Supervisor s1; emp1 = new Manager(); emp2 = new Supervisor(); Employee emp3 = new Manager();//Upcasting of Object m1 = (Manager) emp1; // Employee reference but Manager Object -> DownCasting s1=(Supervisor) emp2; emp3.print(); } } 运行结果: Employee Manager Employee Supervisor Employee Manager Manager Details
这里面有几个问题我不明白。
1.例如class Employee中的 public Employee()和 public void print()这两个语句有啥区别?他们的作用是不是输出Employee和Employee Details?
2.最后psvm中的Employee emp1, emp2;
把Employee创建为两个对象是什么意思? 这一句中的emp1和emp2是不是指代的一样的意思?只是在后面把其指代为不同的对象?
3.emp1 = new Manager(); new了一个Manager()对象,并被emp1指向,但是此时的emp1是等于Manager class中的"Manager"还是"Manager Details"??
4.运行结果是怎么运行的才得到的这样一个结果呢?emp3.print(); 是啥意思?
小弟小白,希望大神悉心讲解。感谢感谢!
ziom
夜很殇37
相关分类