建筑“类”?扩展 SomeType>” Objective-C 中的变量声明
我只是好奇在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Objective-C 没有模板(如 C++)或具有类型擦除的泛型类型(如 Java)或运行时泛型类型(如 C#)。与这些语言不同,Objective-C 消息是在运行时动态分派的(而不是在编译时绑定的)。因此,许多用 C++、Java 或 C# 生成类型无关代码的系统都是不必要的。 Objective-C 更喜欢“鸭子类型”,即任何响应给定选择器(消息)的对象都可以通过调用代码给出该消息,而不管接收对象的类型如何。由于类是 Objective-C 中的对象,因此类方法和实例方法也是如此。
因此,给定的
调用代码可以如下所示:
此代码将编译并在运行时正常工作,假设
obj
是MyClassA
的实例或MyClassB
。当然,好的做法是在这种情况下定义一个
@protocol
:并声明您的
MyClassA
和MyClassB
都实现MyProtocol
协议。 您的调用代码将如下所示如果您尝试调用
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
calling code can look like this
This code will compile and will work fine at runtime, assuming
obj
is either an instance ofMyClassA
orMyClassB
.Of course, good practice would dictate that you define a
@protocol
in this situation:and declare that your
MyClassA
andMyClassB
both implement theMyProtocol
protocol. Your calling code would then look likeand 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 theMyProtocol
interface.Note that
id<MyProtocol>
is not a generic type, it's an instance of typeid
that you are claiming implements theMyProtocol
protocol. Also note that the first version of client code works just fine because all that really maters is whetherobj
can respond to the-myMethod
selector.