请教各位乐于帮助别人困难的英雄,下面的语句错在那里吗
请问各位乐于帮助别人困难的英雄,下面的语句错在那里吗?
下面的语句我在编译的时候出现这样的提示:无法从静态上下文中引用非静态方法;我不知道怎么改,请各位指教
class D
{
void fn1()
{
}
}
abstract class E
{
abstract void fn2();
}
class F extends D
{
E getE()
{
return new E()
{
public void fn2()
{
}
};
}
}
class Text
{
public void method1(D d)
{
d.fn1();
}
public void method2(E e)
{
e.fn2();
}
public static void main (String [] args)
{
F f=new F();
method1(f);
method2(f.getE());
}
}
------解决方案--------------------
public static void main (String [] args)
{
F f=new F();
method1(f);
method2(f.getE());
}
这里啊,老兄,你要这样
public static void main (String [] args)
{
F f=new F();
Text t=new Text();
t.method1(f);
t.method2(f.getE());
}
不能在静态方法中直接调用非静态的成员或方法。!!
------解决方案--------------------
method1和method1是Text类的非静态方法,由对象来调用.
------解决方案--------------------
public static void main (String [] args)
{
F f=new F();
Text t=new Text();
t.method1(f);
t.method2(f.getE());
}
要先创建对象,才能通过对象调用对象的方法!
------解决方案--------------------
下面的语句我在编译的时候出现这样的提示:无法从静态上下文中引用非静态方法;我不知道怎么改,请各位指教
class D
{
void fn1()
{
}
}
abstract class E
{
abstract void fn2();
}
class F extends D
{
E getE()
{
return new E()
{
public void fn2()
{
}
};
}
}
class Text
{
public void method1(D d)
{
d.fn1();
}
public void method2(E e)
{
e.fn2();
}
public static void main (String [] args)
{
F f=new F();
method1(f);
method2(f.getE());
}
}
------解决方案--------------------
public static void main (String [] args)
{
F f=new F();
method1(f);
method2(f.getE());
}
这里啊,老兄,你要这样
public static void main (String [] args)
{
F f=new F();
Text t=new Text();
t.method1(f);
t.method2(f.getE());
}
不能在静态方法中直接调用非静态的成员或方法。!!
------解决方案--------------------
method1和method1是Text类的非静态方法,由对象来调用.
------解决方案--------------------
public static void main (String [] args)
{
F f=new F();
Text t=new Text();
t.method1(f);
t.method2(f.getE());
}
要先创建对象,才能通过对象调用对象的方法!
------解决方案--------------------