关注! 我这个例子通过,想不通楼主的程序为何不行!class A { void fun() { System.out.println("hello, world"); } }public class B extends A { public static void main(String[] args) { A a = new A(); a.fun(); B b = new B(); b.fun(); } }最后打印: hello, world hello, world
我的两个.class不再同一个package
楼上没看见:public class Untitled1 extends test1{ 楼主的程序中Untitled1继承了test1, fun()现在也是Untitled1的成员函数了。----------------------------------既然fun都已经是class Untitled1的成员了那为什么只可以调用static的?
test1 x=new test1();
x.fun(); 应该是fun方法不写static也可以用的啊
x.fun(); 应该是fun方法不写static也可以用的啊
----------------------------问题是不行,我认为这存在语义冲突!!
楼主的程序中Untitled1继承了test1, fun()现在也是Untitled1的成员函数了。
语法就是这么规定的。-----------------------谁说不行,这么调用就OK,而且符合语义Untitled1 x=new Untitled1()
x.fun(); //有没有static都可以进行调用
void fun() {
System.out.println("hello, world");
}
}public class B extends A {
public static void main(String[] args) {
A a = new A();
a.fun();
B b = new B();
b.fun();
}
}最后打印:
hello, world
hello, world
楼主的程序中Untitled1继承了test1, fun()现在也是Untitled1的成员函数了。----------------------------------既然fun都已经是class Untitled1的成员了那为什么只可以调用static的?
x.fun();
试下
问”楼主的不就是继承关系吗,怎么不可以,晕,糊涂了。。
关注