创建没有init方法的类(Objective-c)

时间:2023-01-15 18:51:50

Is it possible to create a class with no init method so as to force all callers to create the object with a factory method instead?

是否可以创建一个没有init方法的类,以强制所有调用者使用工厂方法创建对象?

5 个解决方案

#1


5  

So basically, you want to make sure that your class is never initialized using -init, right? You can't do exactly what you want to do, but you can come close.

所以基本上,你想确保你的类永远不会使用-init初始化,对吧?你不能做你想做的事,但你可以接近。

Since you inherit from NSObject, you have an init method and there's nothing you can do to prevent it from being called. That said, you could override init to this:

由于你从NSObject继承,你有一个init方法,你无法阻止它被调用。也就是说,你可以覆盖init到这个:

- (id)init
{
   [self dealloc];
   @throw [NSException exceptionWithName:@"MyExceptionName" reason:@"Reason" userInfo:nil];
   return nil;
}

This way, anytime someone calls your -init method, it kills the object, so practically speaking, your init method is pretty much un-callable.

这样,只要有人调用你的-init方法,它就会杀死对象,所以实际上你的init方法几乎是不可调用的。

#2


4  

If you really wanted to cause trouble for users of your class who use init, you can do:

如果您真的想为使用init的班级用户带来麻烦,您可以:

@implementation MyClass

- (id) init
{
    // Still have to make sure the runtime has initialised everything for "self"
    self = [super init];
    if (!self) return nil;
    [self release]; // some say you should use [super dealloc]
    [super doesNotRecognizeSelector:_cmd];
    return nil;
}

@end

You invoke super's doesNotRecognizeSelector: because you might want to implement your own behaviour for unrecognised selectors for your class.

您调用super的doesNotRecognizeSelector:因为您可能希望为类的无法识别的选择器实现自己的行为。

#3


1  

Depends. If you have your class inherit from NSObject, it will have the inherited init method (which does nothing to your instance variables). So in that sense, even if you really really wanted to not have an init method, you'd most likely still have one. So if your question was "Do I need to implement a trivial init method?", the answer is "no, you don't need to". However, if your question was "Do I need to call the init method if I didn't override it?", then the answer is "yes, you do". Whatever you do with NSObject subclasses, at some point you still need to call init after the object is created. Such is the way of life.

要看。如果你的类继承自NSObject,它将拥有继承的init方法(它对你的实例变量没有任何作用)。所以从这个意义上讲,即使你真的不想拥有一个init方法,你很可能还有一个。因此,如果您的问题是“我是否需要实现一个简单的初始化方法?”,答案是“不,您不需要”。但是,如果您的问题是“如果我没有覆盖它,我是否需要调用init方法?”,那么答案是“是的,你做的”。无论你使用NSObject子类做什么,在某些时候你仍然需要在创建对象后调用init。这就是生活方式。

That being said, you most likely want an init method, unless your object initialization requires nothing more than zeroing your whole object.

话虽这么说,你很可能想要一个init方法,除非你的对象初始化只需要将整个对象归零。

Otherwise, if you choose to not inherit from NSObject or any of its subclasses and just inherit from nothing, which is clearly a bad idea because of how the NSObject class deals with everything the ObjC runtime needs to do and the requirements are quite high, then you'll potentially end up with no init method at all. But seriously, don't try this at home.

否则,如果你选择不继承NSObject或它的任何子类而只是从什么都没有继承,这显然是一个坏主意,因为NSObject类如何处理ObjC运行时需要做的所有事情并且要求非常高,那么你可能最终没有使用init方法。但说真的,不要在家里试试。

#4


0  

Sure. In Objective-C, there are no actual constructors. init-type methods are typically used to initialize a class, in the same vein as a constructor, but they're just a "normal" method (there's nothing special about them like there are with, e.g., Java constructors).

当然。在Objective-C中,没有实际的构造函数。 init-type方法通常用于初始化一个类,与构造函数一样,但它们只是一个“普通”方法(没有什么特别的,就像Java构造函数一样)。

That said, unless your class does no initialization for its instances, you probably want to have some sort of init method.

也就是说,除非你的类没有对它的实例进行初始化,否则你可能想要某种init方法。

#5


0  

NSObject implements an init method for you that does whatever it does. If your class has nothing to setup when it's instantiated then simply do not override the -(id)init method provided by NSObject. But you still call it when you create the instance.

NSObject为您执行一个init方法,它可以执行任何操作。如果你的类在实例化时没有任何设置,那么就不要覆盖NSObject提供的 - (id)init方法。但是在创建实例时仍然会调用它。

#1


5  

So basically, you want to make sure that your class is never initialized using -init, right? You can't do exactly what you want to do, but you can come close.

所以基本上,你想确保你的类永远不会使用-init初始化,对吧?你不能做你想做的事,但你可以接近。

Since you inherit from NSObject, you have an init method and there's nothing you can do to prevent it from being called. That said, you could override init to this:

由于你从NSObject继承,你有一个init方法,你无法阻止它被调用。也就是说,你可以覆盖init到这个:

- (id)init
{
   [self dealloc];
   @throw [NSException exceptionWithName:@"MyExceptionName" reason:@"Reason" userInfo:nil];
   return nil;
}

This way, anytime someone calls your -init method, it kills the object, so practically speaking, your init method is pretty much un-callable.

这样,只要有人调用你的-init方法,它就会杀死对象,所以实际上你的init方法几乎是不可调用的。

#2


4  

If you really wanted to cause trouble for users of your class who use init, you can do:

如果您真的想为使用init的班级用户带来麻烦,您可以:

@implementation MyClass

- (id) init
{
    // Still have to make sure the runtime has initialised everything for "self"
    self = [super init];
    if (!self) return nil;
    [self release]; // some say you should use [super dealloc]
    [super doesNotRecognizeSelector:_cmd];
    return nil;
}

@end

You invoke super's doesNotRecognizeSelector: because you might want to implement your own behaviour for unrecognised selectors for your class.

您调用super的doesNotRecognizeSelector:因为您可能希望为类的无法识别的选择器实现自己的行为。

#3


1  

Depends. If you have your class inherit from NSObject, it will have the inherited init method (which does nothing to your instance variables). So in that sense, even if you really really wanted to not have an init method, you'd most likely still have one. So if your question was "Do I need to implement a trivial init method?", the answer is "no, you don't need to". However, if your question was "Do I need to call the init method if I didn't override it?", then the answer is "yes, you do". Whatever you do with NSObject subclasses, at some point you still need to call init after the object is created. Such is the way of life.

要看。如果你的类继承自NSObject,它将拥有继承的init方法(它对你的实例变量没有任何作用)。所以从这个意义上讲,即使你真的不想拥有一个init方法,你很可能还有一个。因此,如果您的问题是“我是否需要实现一个简单的初始化方法?”,答案是“不,您不需要”。但是,如果您的问题是“如果我没有覆盖它,我是否需要调用init方法?”,那么答案是“是的,你做的”。无论你使用NSObject子类做什么,在某些时候你仍然需要在创建对象后调用init。这就是生活方式。

That being said, you most likely want an init method, unless your object initialization requires nothing more than zeroing your whole object.

话虽这么说,你很可能想要一个init方法,除非你的对象初始化只需要将整个对象归零。

Otherwise, if you choose to not inherit from NSObject or any of its subclasses and just inherit from nothing, which is clearly a bad idea because of how the NSObject class deals with everything the ObjC runtime needs to do and the requirements are quite high, then you'll potentially end up with no init method at all. But seriously, don't try this at home.

否则,如果你选择不继承NSObject或它的任何子类而只是从什么都没有继承,这显然是一个坏主意,因为NSObject类如何处理ObjC运行时需要做的所有事情并且要求非常高,那么你可能最终没有使用init方法。但说真的,不要在家里试试。

#4


0  

Sure. In Objective-C, there are no actual constructors. init-type methods are typically used to initialize a class, in the same vein as a constructor, but they're just a "normal" method (there's nothing special about them like there are with, e.g., Java constructors).

当然。在Objective-C中,没有实际的构造函数。 init-type方法通常用于初始化一个类,与构造函数一样,但它们只是一个“普通”方法(没有什么特别的,就像Java构造函数一样)。

That said, unless your class does no initialization for its instances, you probably want to have some sort of init method.

也就是说,除非你的类没有对它的实例进行初始化,否则你可能想要某种init方法。

#5


0  

NSObject implements an init method for you that does whatever it does. If your class has nothing to setup when it's instantiated then simply do not override the -(id)init method provided by NSObject. But you still call it when you create the instance.

NSObject为您执行一个init方法,它可以执行任何操作。如果你的类在实例化时没有任何设置,那么就不要覆盖NSObject提供的 - (id)init方法。但是在创建实例时仍然会调用它。