class X
{
public void display()
{
System.out.println("你好,欢迎使用基类。");
}
}
class Y extends X
{
public void display()
{
System.out.println("你好,欢迎使用子类。");
}
}
public class Text { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
X a=new Y();
a.display();
Y b=(Y) new X();
b.display();
//((X)b).display();
}}
{
public void display()
{
System.out.println("你好,欢迎使用基类。");
}
}
class Y extends X
{
public void display()
{
System.out.println("你好,欢迎使用子类。");
}
}
public class Text { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
X a=new Y();
a.display();
Y b=(Y) new X();
b.display();
//((X)b).display();
}}
不可以。class 中国人 {}
class 北京人 extends 中国人 {}中国人 x = new 北京人(); // 这个当然可以,任何一个北京人必然是中国人,但是
北京人 y = new 中国人(); // 这个绝对错误,你再怎么转型都是错的。