Cast

class Person{
public String name;
Person(String name){
this.name=name;
}
}
class Student extends Person{
public int studentId;
Student(String str,int id){
super(str);
studentId=id;

}
void studying(){
System.out.println("I am studying hard!");
}
}
class Employee extends Person{
public int salary;
Employee(String str,int s){
super(str);
salary=s;
}
void working(){
System.out.println("I am working hard!");
}
}
public class Cast {

public void testCast(Person p) {
System.out.println("name is:"+p.name);
if(p instanceof Student){
Student s=(Student)p;
System.out.println("studentId is:"+s.studentId);
}
else if(p instanceof Employee){
Employee e=(Employee)p;
System.out.println("studentId is:"+e.salary);
}

}
public static void main(String[] args) {
Cast cast=new Cast();
Person p=new Person("Tom");
Student s=new Student("John",18);
Employee e=new Employee("Lucy",2000);
System.out.println(p instanceof Person);
System.out.println(s instanceof Person);
System.out.println(e instanceof Person);
System.out.println(p instanceof Student);
p=new Employee("Mary",3000);
System.out.println(p.name);
System.out.println(p instanceof Person);
System.out.println(p instanceof Employee);
Employee e1=(Employee) p;
System.out.println(e1.salary);
e1.working();
p=new Student("Lily",4000);
System.out.println(p.name);
Student s1=(Student) p;
System.out.println(s1.studentId);
s1.studying();
cast.testCast(p);
cast.testCast(s);
cast.testCast(e);
}

}

原文地址:https://www.cnblogs.com/wlp1115/p/6704909.html