public class Test{
public static void main(String [] args){
TheInterface a = new A();
TheInterface b = new B();
choose(a);
choose(b);
}
public static void choose(TheInterface x){//若这里不申明为 static 则error为无法从静态上下文中引用
//非静态方法 ?? 我不明白是什么意思..
x.print();
}
}
interface TheInterface{
void print();
}
class A implements TheInterface{
public void print(){
System.out.println("this is A");
}
}
class B implements TheInterface{
public void print(){
System.out.println("this is B");
}
}
----------------解决方案--------------------------------------------------------
在静态方法里面只能调用静态方法或者静态成员变量
如果想在静态方法里面访问非静态的东西,你得先生成一个对象,然后通过这个对象去调用非静态的东西
----------------解决方案--------------------------------------------------------
哦~原来是这样
public class Test{
public static void main(String [] args){
TheInterface a = new A();
TheInterface b = new B();
a.print();
b.print() ; }
}
那这样就应当没有问题吧
谢谢斑竹 也谢谢楼主...哈哈
----------------解决方案--------------------------------------------------------
问题是没问题,但接口也不是这样用吧...这样的话你也就没必要定义接口了
----------------解决方案--------------------------------------------------------