结论:
1.父类(基本类)转子类(扩展类),报错
2.子类(扩展类)转父类(基本类),成功
实验如下:
父类(基本类)
public class Person { private String name; private String doc; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDoc() { return doc; } public void setDoc(String doc) { this.doc = doc; } }
子类(扩展类)
public class CNPerson extends Person { }
实现类:
@Test //失败 public void test1(){ Person p=new Person(); p.setName("Name"); CNPerson cp=(CNPerson)p; System.out.println(cp.getName()); } @Test //成功 public void test2(){ CNPerson cp=new CNPerson(); cp.setName("Name"); Person p=(Person)cp; System.out.println(p.getName()); }
原文:http://www.cnblogs.com/shoubianxingchen/p/4232473.html