2014-11-05 162 views
1

我已阅读了许多网站上的私有构造函数,并且还提到了StackOverflow上的各种问题。但是,我不明白他们的用法。大多数网站都说当我们想限制可以创建的对象的实例数量时,可以使用私有构造函数使用私有构造函数创建类的对象

我尝试了以下程序:

public class PrivateCons{ 
    private PrivateCons(){ 
    System.out.println("I'm executed"); 
    } 

    public static void main(String[] args) { 
     PrivateCons p=new PrivateCons(); 
     PrivateCons q=new PrivateCons(); 
    } 
} 

我的计划执行得很好。我理解错了吗?

+1

你的主要方法是_inside_类具有私有的构造函数。所以只有你 - 作为这个类的开发者 - 有权创建它的实例,并且因此可以将它们的数量限制为你想要的数量。如果您将课堂作为图书馆的一部分提供给我,那么我不允许创建实例。这通常是提供静态工厂方法的单例或类的概念。 – Seelenvirtuose 2014-11-05 07:53:12

+0

感谢您的解释:) – Awani 2014-11-05 08:12:46

回答

2

Private字段是从类中访问,您将无法访问他们淘汰类,例如: -

class PrivateCons{ 
    private PrivateCons(){ 
    System.out.println("I'm executed"); 
    } 
} 

public class Test{ 
    public static void main(String[] args) { 
     PrivateCons p=new PrivateCons(); //this will fail- compiler error 
     PrivateCons q=new PrivateCons();//this will fail- compiler error 
    } 
} 

而且私有构造函数主要用于implementing Singleton Pattern,这是用来当该类的对象只有一个是必要的。以一个例子从链接本身维基百科的文章: -

public class singleton 
{ 
    private static singleton _obj; 

    private singleton() 
    { 
     // prevents instantiation from external entities 
    } 

    // Instead of creating new operator, declare a method 
    // and that will create object and return it. 

    public static singleton GetObject() 
    { 
     // Check if the instance is null, then it 
     // will create new one and return it. 
     // Otherwise it will return previous one. 

     if (_obj == null) 
     { 
      _obj = new singleton(); 
     } 

     return _obj; 
    } 

} 

您可以扩展这个例子,并限制你的对象,以2,3或任意数量的,或者换句话说restrciting你的类实例的数量。

+1

感谢您的帮助:) – Awani 2014-11-05 08:13:33

+0

请记住Singleton模式限制您的代码。对于Singleton模式,由于UnitTests使用相同的实例,所以不能让主构建和UnitTests并行。另外,当你想进行未来的调整时,比如有多个Singleton类的实例,这也会带来问题。 (例如:我有一个类Anthill和一个Ant类,Anthill可以是一个Singleton,并且你有一堆Ant对象,假设将来我想拥有多个Anthill,现在不可能了完全更改代码,因为只有1个Anthil实例可以存在 – 2014-11-05 08:29:40

+0

是的,这是真的,但也有单一模式的有效用法,例如'java.lang.Runtime'使用Singleton模式。 – 2014-11-05 15:04:37

1

从同一个类中,您可以访问私有构造函数。从其他类中,不能调用该构造函数。

您可以使用私有构造函数来实现Singleton设计模式。

public class PrivateCons 
{ 
    PrivateCons instance = new PrivateCons(); 
    private PrivateCons(){ 
    System.out.println("I'm executed"); 
    } 
    public static PrivateCons getInstance() 
    { 
     return instance; 
    } 
} 

现在,你的类的用户只能得到你的类的一个实例,通过getIstnace方法,因为他们无法通过私有构造函数创建新实例。

+0

这不是真正的最好的方式来创建单身...我会使用其他代码示例.. – Frank 2014-11-05 07:52:07

+0

谢谢:)请问你怎么会在这里调用getInstance()方法。我尝试了几种方法,但他们给了我错误。 – Awani 2014-11-05 08:05:12

+0

@ user2195963这是一个静态方法,所以你这样调用它:'PrivateCons.getInstance()' – Eran 2014-11-05 08:06:32

1

私人构造函数不限制创建实例的号码;它限制了,其中的构造函数可能会被调用。

它可以防止从顶级类的范围之外调用构造函数(在本例中为PrivateCons)。

0

请大家看看folloing例如:

public class ClassWithPrivateConstructor { 

private static final Random random = new Random(); 
private static final int count = 10; 
private static final ClassWithPrivateConstructor[] instances = new ClassWithPrivateConstructor[count]; 
static  
{ 
    for (int i = 0; i < count; i++) { 
     instances[i] = new ClassWithPrivateConstructor(); 
    } 
} 

public static ClassWithPrivateConstructor newInstance() { 
    return instances[random.nextInt(count)]; 
} 

private ClassWithPrivateConstructor() {   
} 
} 

测试类:

public class ClassWithPrivateConstructorTest { 
public static void main(String[] args) { 
    for(int i=0;i<20; ++i) { 
     System.out.println(ClassWithPrivateConstructor.newInstance()); 
    } 
} 
} 

输出示例: ClassWithPrivateConstructor @ 14ae5a5 ClassWithPrivateConstructor @ 7f31245a ClassWithPrivateConstructor @ 7f31245a ClassWithPrivateConstructor @ 14ae5a5 ClassWithPrivateConstructor @ 6d6f6e28 ClassW ithPrivateConstructor @ 7f31245a ClassWithPrivateConstructor @ 14ae5a5 ClassWithPrivateConstructor @ 135fbaa4 ClassWithPrivateConstructor @ 6d6f6e28 ClassWithPrivateConstructor @ 45ee12a7 ClassWithPrivateConstructor @ 330bedb4 ClassWithPrivateConstructor @ 45ee12a7 ClassWithPrivateConstructor @ 7f31245a ClassWithPrivateConstructor @ 135fbaa4 ClassWithPrivateConstructor @ 14ae5a5 ClassWithPrivateConstructor @ 2503dbd3 ClassWithPrivateConstructor @ 6d6f6e28 ClassWithPrivateConstructor @ 4b67cf4d ClassWithPrivateConstructor @ 7ea987ac ClassWithPrivateConstructor @ 45ee12a7

正如你所看到的类实例的数量在这种情况下被限制在10

相关问题