建筑“类”?扩展 SomeType>” Objective-C 中的变量声明

发布于 2024-10-25 21:36:07 字数 480 浏览 0 评论 0原文

我只是好奇在 Objective-C 中是否可以以 Java 已知的方式声明变量:

Class<?extends SomeType>

例如:我有一个名为 MyClass 的类。它有一个静态方法,

+ (void)myMethod

它还有两个子类:MySubclassA 和 MySubclassB。我有这样的代码:

Class myClass;
if(<some condition>) {
  myClass = [MySubclassA class];
} else {
  myClass = [MySubclassB class];
}
[myClass myMethod];

这段代码工作正常,没有编译器警告,但我只是好奇我提到的构造是否以某种方式存在于 Objective-C 中。

谢谢!

I am just curious about whether declaring a variable in a way known from Java is possible in Objective-C:

Class<?extends SomeType>

For example: I have a class called MyClass. It has a static method

+ (void)myMethod

It has also two subclasses: MySubclassA and MySubclassB. I have such code:

Class myClass;
if(<some condition>) {
  myClass = [MySubclassA class];
} else {
  myClass = [MySubclassB class];
}
[myClass myMethod];

This code works fine, there are no compiler warnings, but I am just curious whether the construction I mentioned is somehow present in Objective-C.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时光的容颜 2024-11-01 21:36:07

Objective-C 没有模板(如 C++)或具有类型擦除的泛型类型(如 Java)或运行时泛型类型(如 C#)。与这些语言不同,Objective-C 消息是在运行时动态分派的(而不是在编译时绑定的)。因此,许多用 C++、Java 或 C# 生成类型无关代码的系统都是不必要的。 Objective-C 更喜欢“鸭子类型”,即任何响应给定选择器(消息)的对象都可以通过调用代码给出该消息,而不管接收对象的类型如何。由于类是 Objective-C 中的对象,因此类方法和实例方法也是如此。

因此,给定的

@interface MyClassA : NSObject
{}
 - (void)someMethod;
@end

@interface MyClassB: NSObject
{}
 - (void)someMethod;
@end

调用代码可以如下所示:

- (void)someOtherMethodInAnOtherClassWithObject:(id)obj
{
  [obj someMethod];
}

此代码将编译并在运行时正常工作,假设 objMyClassA 的实例或MyClassB

当然,好的做法是在这种情况下定义一个 @protocol

@protocol MyProtocol
 - (void)myMethod
@end

并声明您的 MyClassAMyClassB 都实现 MyProtocol 协议。 您的调用代码将如下所示

- (void)someOtherMethodInAnOtherClassWithObject:(id<MyProtocol>)obj
{
  [obj someMethod];
}

如果您尝试调用 someOtherMethodInAnOtherClassWithObject:,传递未实现的类型的对象,那么 ,并且编译器会给您一个警告/错误(取决于 -W 标志) MyProtocol 接口。

请注意,id不是泛型类型,它是类型 id 的实例,您声称它实现了 MyProtocol< /代码> 协议。另请注意,客户端代码的第一个版本工作得很好,因为真正重要的是 obj 是否可以响应 -myMethod 选择器。

Objective-C does not have templates (like C++) or generic types with type erasure (like Java) or runtime generic types (like C#). Unlike these languages, Objective-C messages are dynamically dispatched at runtime (instead of bound at compile time). Thus, many of the systems for producing type-agnostic code in C++, Java or C# are unnecessary. Objective-C prefers "duck-typing" whereby any object that responds to a given selector (message) can be given that message by calling code, regardless of the receiving object's type. Since classes are objects in Objective-C, the same is true for class methods as for instance methods.

So, given

@interface MyClassA : NSObject
{}
 - (void)someMethod;
@end

@interface MyClassB: NSObject
{}
 - (void)someMethod;
@end

calling code can look like this

- (void)someOtherMethodInAnOtherClassWithObject:(id)obj
{
  [obj someMethod];
}

This code will compile and will work fine at runtime, assuming obj is either an instance of MyClassA or MyClassB.

Of course, good practice would dictate that you define a @protocol in this situation:

@protocol MyProtocol
 - (void)myMethod
@end

and declare that your MyClassA and MyClassB both implement the MyProtocol protocol. Your calling code would then look like

- (void)someOtherMethodInAnOtherClassWithObject:(id<MyProtocol>)obj
{
  [obj someMethod];
}

and the compiler would give you a warning/error (depending on -W flags) if you tried to call someOtherMethodInAnOtherClassWithObject:, passing an object of a type that doesn't implement the MyProtocol interface.

Note that id<MyProtocol> is not a generic type, it's an instance of type id that you are claiming implements the MyProtocol protocol. Also note that the first version of client code works just fine because all that really maters is whether obj can respond to the -myMethod selector.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文