簡體   English   中英

如何使用私有構造函數從類創建對象?

[英]How to create objects from a class with private constructor?

我有一個類游戲,是我的主要類和第二類卡。 類卡具有私有屬性和構造函數,只有函數init是公共的。 函數init檢查值的合理性,如果一切正常,則構造函數獲取值並創建一個對象。 現在我在課堂游戲中創建一個Card類的對象。 我該怎么做?

這是我的代碼:

課堂游戲:

import java.util.List;
import java.util.Vector;


public class Game {


public  static void main(String[] args)
{
   /*
CREATING NEW CARD OBJECT
 */

    int value = 13;
    Vector<Card> _card_set = new Vector<Card>();
    for (int i = 2; i < 54; i++)
    {

        if(--value == 0)
        {
            value = 13;
        }

        Card _myCard;
        _myCard.init(i,value);
     }
   }
 }

類卡:

public class Card {

private int index;
private  int value;
private String symbol;

/*
CREATING A PLAYCARD
*/

private Card(int index,int value)
{
    this.index = index;
    this.value = value;
    value = (int) Math.floor(index % 13);

    if(this.index >= 2 && this.index <= 14)
    {
        this.symbol = "KARO";
    }
    else if (this.index >= 15 && this.index <= 27)
    {
        this.symbol = "HERZ";
    }
    else if (this.index >= 26 && this.index <= 40)
    {
        this.symbol = "PIK";
    }
    else if (this.index >= 41 && this.index <= 53)
    {

        this.symbol = "KREUZ";
    }
    System.out.println("Card object wurde erstellt: " + symbol + value);
    System.out.println("------<><><><>------");
}

/*
SHOW FUNCTION
GET DETAILS ABOUT PLAYCARD
 */
public String toString()
{
    return "[Card: index=" + index + ", symbol=" + symbol + ", value=" + value + "]";
}

/*
Initialize Card object
 */

public Card init(int index, int value)
{
    /*
    Check for plausibility
    if correct constructor is called
     */

    if((index > 1 || index > 54) && (value > 0 || value < 14))
    {
       Card myCard = new Card(index,value);
        return  myCard;
    }
    else
    {
        return null;
    }
  }
}

您應該將init方法定義為static,實現Braj談論的靜態工廠方法。 這樣,您可以像這樣創建新卡:

Card c1 = Card.init(...);
Card c2 = Card.init(...);
...

正如@Braj在評論中提到的,你可以使用靜態工廠。 私有構造函數不能在類外部訪問,但可以從內部訪問,如下所示:

public class Test
{

    private Test(){

    }

    static Test getInstance(){
        return new Test();
    }
}

例如,該模式可用於制作建造者。

注意:您可以像在公共靜態工廠方法中一樣從類本身訪問私有構造函數。
您可以從封閉類訪問它,它是一個嵌套類。

public class demo
{
    private demo()
    {

    }
    public static demo getObject()
    {
        return new demo();
    }

    public void add()
    {

    }
}
class Program
{
    static void Main(string[] args)
    {
        demo d1 = demo.getObject();
        d1.add();
    }
}

我想說不要將構造函數設為私有,不要在構造函數下創建構建代碼(將它放在一個新方法中,這可以是私有的)並創建一個方法將卡返回到類外。

然后使用卡對象並調用方法從Card類中檢索您的卡,確保聲明類型卡並確保方法正確返回。

Class<?> class = Class.forName("SomeClassName"); 
Constructor<?> constructor = class.getConstructors[0];
constructor.setAccessible(true);

將私有聲明的構造函數轉換為公共構造函數直到程序執行。 此外,此概念與Reflection API有關。

Object o = constructor.newInstance();
if(o.equals(class)) {
    System.out.println("Object for \"SomeClassName\" has been created");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM