简体   繁体   English

Java相当于Objective-C实例类型?

[英]Java equivalent of Objective-C instancetype?

In Objective-C, instancetype can be used as the return type of methods that return an instance of the class they are called on (or a subclass of that class). 在Objective-C中, instancetype可以用作返回它们被调用的类的实例(或该类的子类)的方法的返回类型。

What is the equivalent of instancetype in Java? Java中的instancetype相当于什么?

The closest to thing is to use generics 最接近的是使用泛型

interface Base<B extends Base<B>> {
    // do something and return this.
    B append(String s);
}

interface SubBase<B extends SubBase<B>> extends Base<SubBase<B>> {
    // append returns a SubBase<B>
}

class MyClass implements SubBase<MyClass> {
    public MyClass append(String s) {
         // do something
         return this;
    }
}

It's not so elegant, but it works. 它不是那么优雅,但它有效。

If you mean something like this: 如果你的意思是这样的:

class Vehicle {
    instancetype doNothing() {return this;}
}
class Car extends Vehicle {}

Car c = new Car().doNothing(); // no compile errors here

There is no equivalent. 没有等价物。

One workaround that's sometimes used is to pass the type as a generic parameter: 有时使用的一种解决方法是将类型作为泛型参数传递:

class Vehicle<instancetype> {
    instancetype doNothing() {return this;}
}
class Car extends Vehicle<Car> {}

Car c = new Car().doNothing(); // works

but then you can't use the type Vehicle without getting warnings everywhere (it has to be Vehicle<?> ). 但是你不能在没有得到任何警告的情况下使用类型的Vehicle (它必须是Vehicle<?> )。

I'm no Javaist but likely there is no equivalent, because Java needs it only rarely. 我不是Javaist,但可能没有等价物,因为Java很少需要它。 (And Objective-C do so, too.) (而Objective-C也这样做。)

Please note that in Objective-C you do not need it most of the time, too. 请注意,在Objective-C中,您大多数时间也不需要它。 This is, because the compiler infers the "real" return type from the receiver, even it is declared id . 这是因为编译器从接收器推断出“真实”返回类型,即使它被声明为id This applies to -init… , +alloc… , -new… : 这适用于-init…+alloc…-new… +alloc… -new…

To determine whether a method has an inferred related result type, the first word in the camel-case selector (eg, “init” in “initWithObjects”) is considered, and the method will have a related result type if its return type is compatible with the type of its class and if: 要确定方法是否具有推断的相关结果类型,将考虑camel-case选择器中的第一个单词(例如,“initWithObjects”中的“init”),如果其返回类型兼容,则该方法将具有相关的结果类型与其类的类型和如果:

  • the first word is “alloc” or “new”, and the method is a class method, or 第一个单词是“alloc”或“new”,方法是类方法,或
  • the first word is “autorelease”, “init”, “retain”, or “self”, and the method is an instance method. 第一个单词是“autorelease”,“init”,“retain”或“self”,该方法是一个实例方法。

In Java this is inferred for the new operator, too. 在Java中,这也是new运算符的推断。 The constructors does not have any return type, so it does not need to be inferred. 构造函数没有任何返回类型,因此不需要推断。

Up to here: No need for an (explicit) keyword in Java or Objective-C. 到此为止:不需要Java或Objective-C中的(显式)关键字。

In some cases you have additional methods returning a new instance of the receivers class. 在某些情况下,您有其他方法返回接收器类的新实例。 Cloning/copying is such a case. 克隆/复制就是这种情况。 In Java you have to type cast the result. 在Java中,您必须键入强制转换结果。 In Objective-C you can use instancetype to make code easier to read. 在Objective-C中,您可以使用instancetype使代码更易于阅读。 (BTW: NSObject 's -copy has id as return type, not instancetype . But again usually this is no problem, because you typically assign the return value to a typed reference.) (顺便说一句: NSObject-copyid作为返回类型,而不是instancetype 。但通常这也没问题,因为你通常会将返回值赋给一个类型化的引用。)

So the short conclusion: In both Java and Objective-C the return type can be and is inferred by the compiler for most of the use cases. 所以简短的结论是:在Java和Objective-C中,返回类型可以由编译器推断,并且可以用于大多数用例。 In rare use cases you have to explicitly cast in Java. 在极少数用例中,您必须使用Java显式转换。

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

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