How to create objects from a class with private constructor?

前端 未结 6 1526
花落未央
花落未央 2021-01-14 00:39

I have a class Game that is my main class and a second class Card. Class Card hast its properties and constructor private, only function init is public. Function init checks

6条回答
  •  生来不讨喜
    2021-01-14 01:13

    You should define your init method as static, implementing the static factory method that Braj talks about. This way, you create new cards like this:

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

提交回复
热议问题