简体   繁体   English

当 class 有子类时,如何实现 Singleton 设计模式?

[英]How to implement Singleton design pattern when class has subclasses?

I have a Player class for a game and there are different player types: Animal, Person, Robot.我有一个游戏玩家 class 并且有不同的玩家类型:动物、人、机器人。 I want my game to have a single instance of a player using the Singleton pattern but not sure if this is possible?我希望我的游戏有一个使用 Singleton 模式的玩家实例,但不确定这是否可行?

在此处输入图像描述

think simple, man.想得简单,伙计。 it's polymorphism.它是多态性。 you can check it in here: https://www.w3schools.com/java/java_polymorphism.asp你可以在这里查看: https://www.w3schools.com/java/java_polymorphism.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM