class b1{
void bp(){
System.out.println("b1");
}
}
class b2 extends b1{
void bp(){
System.out.println("b2");
}
}
class b{
public static void main(String args[])
{
/*
b1 x1=new b1(); '说静态函数不能调用非静态方法
b1.bp();
b2 x2=new b2();
b2.bp();
*/b1 x3=new b2(); '可以,为什么?不理解?
x3.bp();}
}
void bp(){
System.out.println("b1");
}
}
class b2 extends b1{
void bp(){
System.out.println("b2");
}
}
class b{
public static void main(String args[])
{
/*
b1 x1=new b1(); '说静态函数不能调用非静态方法
b1.bp();
b2 x2=new b2();
b2.bp();
*/b1 x3=new b2(); '可以,为什么?不理解?
x3.bp();}
}
x1.bp();
b2 x2=new b2();
x2.bp();
你的b1、b2类中的bp方法不是static的当然不能用类名.方法名,来调用,应该用对象名.方法来调用
static void bp(){
System.out.println("b1");
}
}
class b2 extends b1{
static void bp(){
System.out.println("b2");
}
}
public class ghyghost{
public static void main(String args[])
{
/*
b1 x1=new b1(); '说静态函数不能调用非静态方法
b1.bp();
b2 x2=new b2();
b2.bp();
*/b1 x3=new b2(); //可以,为什么?不理解?
x3.bp();}
}
应该说是不可以的,父类不能用子类构造
public class ghyghost{
public static void main(String args[])
{b1 x1=new b1(); '说静态函数不能调用非静态方法
x1.bp();
b2 x2=new b2();
x2.bp();}
}运行绝对没问题