在Objective -C中子类化的正确方法是什么? [英] What's the correct method to subclass a singleton class in Objective -C?

查看:92
本文介绍了在Objective -C中子类化的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个单例类,并且我想创建一个属于该单例类的子类的类,正确的方法是

I have created a singleton class and I want to create a class which is subclass of this singleton class, what is the correct method to do it

推荐答案

我并不特别了解Objective-C,但在一般情况下,单例类应防止子类化.如果您拥有基类的实例和子类的实例,那么您实际上拥有了两个对象,可以将它们视为基类"singleton"的实例,不是吗? ?

I don't know about Objective-C in particular, but in general singleton classes should prevent subclassing. If you've got an instance of the base class and an instance of the subclass, then you've effectively got two objects you can regard as instances of the base "singleton" class, haven't you?

一旦有了两个实例,它实际上就不再是单例了……而这排除了存在多个子类或子类本身允许创建多个实例的可能性.

As soon as you've got two instances, it's not really a singleton any more... and that's leaving aside the possibilities that there are multiple subclasses, or that the subclass itself allows multiple instances to be created.

当然,您可以更改基类,使其仅具有获取单个默认"实例的方式,但这与使它成为单例并不完全相同.

Of course you can change your base class so it just has a way of getting at a single "default" instance, but that's not quite the same as making it a singleton.

这篇关于在Objective -C中子类化的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆