倒 这么说吧: class A { public static A getA() { return new A(); } }A a = A.getA(); 最会不是还用new了? NumberFormat使用的是factory method来代替new的操作,NumberFormat是一个abstract的类,也就是jdk不想让你直接去new 具体的NumberFormat类,而使用getNumberInstance()去实现透明的i18n支持
类名 myobject = new 构造函数();NumberFormat nf = NumberFormat.getNumberInstance();一个是实例变量,一个是类变量生成实例 Test instance = new Test() 一般显视的用new关键字 getNumberInstance()此方法一定是类方法,而不是实例方法 他们区分的条件,是否有关键static public class A{ public static test(){} //类方法,全局的,非面向对象的编写 public testA(){} 实例方法 面向对象的编写 }
不过对用户来说不可见。
工厂模式运用很广的,
对象的具体类型,可以推迟到运行时才确定。
这么说吧:
class A {
public static A getA() {
return new A();
}
}A a = A.getA();
最会不是还用new了?
NumberFormat使用的是factory method来代替new的操作,NumberFormat是一个abstract的类,也就是jdk不想让你直接去new 具体的NumberFormat类,而使用getNumberInstance()去实现透明的i18n支持
getNumberInstance()此方法一定是类方法,而不是实例方法
他们区分的条件,是否有关键static public class A{
public static test(){} //类方法,全局的,非面向对象的编写
public testA(){} 实例方法 面向对象的编写
}