public class Test {
public static void main(String[] args) {
Person p = new Student();
Teacher t = new Teacher();
Student s = new Student();
if(t instanceof Person){p = t;}
if(s instanceof Person){s = (Student)p;}
}
}
Exception in thread "main" java.lang.ClassCastException: Teacher cannot be cast to Student
at Test.main(Test.java:8)
if(s instanceof Person){s = (Student)p;}
編譯時出錯