
class Chinese
{

public static void print()
{
System.out.println("this is 中国人");
}
} 
class Guangdongren extends Chinese
{ 
public static void print()
{
System.out.println("this is 广东人 ");
}
} 
class HunanRen extends Chinese
{ 
public static void print()
{
System.out.println("this is 湖南人 ");
}
} 
class GuangzhouRen extends Guangdongren
{ 
public static void print()
{
System.out.println("this is 广州人 ");
}
}

public class Test
{

public static void main(String args[])
{
Chinese c=new Chinese();//这样编译可以通过,但运行会抛出异常
HunanRen h=(HunanRen)c;// 这是因为向下转型错误,中国人不一定是湖南人,这里
//是不是可以这样理解要将c强制转换成一个HunanRen
HunanRen h=new HunanRen();//这样就没有问题,这是向上转型,
Chinese c=(Chinese)h; //湖南人一定是中国人这是是否可以理解为将h转换成Cinese
Chinaese h=new HunanRen();//向上转型
h.print();
}
} 
3080

被折叠的 条评论
为什么被折叠?



