对,就象楼上所说的,只要把方法声明成static就可以直接调用了 public class A { public static void method() { ...... } } class B { A.method(); }
楼主说得好像不是这个意思吧! 不过也不懂他的具体意思。 static方法的确可以实现!
上面的办法我觉得可以实现但 用单态的设计模式也可是实现 class A { privat A(){} privat statidc final A a=new A(); public static A getA() { return a; }
}
用单例模式,你在任何类都可以用这个类的实例。 如: class My { private static My my; public static My getMy() { if(my==null) my=new My(); return my; } public void change(){ system.out.print("hello");} } 或者 class My{ private static My my=newMy(); public static My getMy() { return my; } public void change(){ system.out.print("hello");} } class example{ My.getMy().change();//在任何类里都可以用 }
public class A
{
public static void method()
{
......
}
}
class B
{
A.method();
}
不过也不懂他的具体意思。
static方法的确可以实现!
用单态的设计模式也可是实现
class A
{
privat A(){}
privat statidc final A a=new A();
public static A getA()
{
return a;
}
}
如: class My
{
private static My my;
public static My getMy()
{
if(my==null) my=new My();
return my;
}
public void change(){ system.out.print("hello");}
}
或者
class My{
private static My my=newMy();
public static My getMy()
{
return my;
}
public void change(){ system.out.print("hello");}
}
class example{
My.getMy().change();//在任何类里都可以用 }