本帖最后由 许庭洲 于 2015-4-24 15:24 编辑
//若想静态方法内部调用非静态方法,可以传递一个对象的引用到静态方法里
package com.jldx.test;
public class StaticInvokeNonStaticMethod
{
private String name;
public Static InvokeNonStaticMethod(String name)
{
this.name = name;
}
public static void main(String[] args)
{
Static InvokeNonStaticMethod s = new StaticInvokeNonStaticMethod("北京");
Static InvokeNonStaticMethod s2 = new StaticInvokeNonStaticMethod("上海");
s.ask(s);
s.ask(s2);
} public static void ask(StaticInvokeNonStaticMethod s)
{
System.out.println("静态方法内部调用非静态方法");
s.say();
}
public void say()
{
System.out.println("this city is " + name);
}
}
|