简体   繁体   English

Java的'self'关键字是什么

[英]What is Java's 'self' keyword

I'm trying to write a factory method in an abstract class in Java (so I want it to return a new instance of the extending class, rather than the super-class).我正在尝试在 Java 的抽象 class 中编写工厂方法(所以我希望它返回扩展 class 的新实例,而不是超类)。

In PHP I'd do this using the self keyword:在 PHP 中,我会使用self关键字来执行此操作:

abstract class Superclass {
    public static function factory($arg) {
        return new self($arg);
    }

    private function __construct($arg) {}

    abstract public function doSomething() {}
}

Does Java have a keyword like self I can use for this? Java 是否有类似self的关键字可以用于此?

No;不; in Java, static methods are not inherited in the same way as non-static methods are.在 Java 中,static 方法的继承方式与非静态方法不同。 A subclass will have the static methods of its superclass, but when they execute, they will execute in context of the superclass - so there is no keyword that can be used in static methods to find out what class the method was invoked through.子类将具有其超类的 static 方法,但是当它们执行时,它们将在超类的上下文中执行 - 因此没有可在 static 方法中使用的关键字来找出 ZA2F2ED4F8EBC2CBB4C21A2 调用的方法。

Edit: A more precise formulation is that static methods are not inherited at all;编辑:更精确的表述是 static 方法根本不被继承; however, the language allows us to use Subclass.foo() to call the static method Superclass.foo() .但是,该语言允许我们使用Subclass.foo()来调用 static 方法Superclass.foo()

Based on what you seem to want to achieve, you might want to implement the Abstract Factory pattern .根据您似乎想要实现的目标,您可能想要实现抽象工厂模式 It goes approximately like this:它大约是这样的:

public abstract class Superclass {}
public class SubclassA extends Superclass {}
public class SubclassB extends Superclass {}

public abstract class AbstractFactory {
    public abstract Superclass Create();
}

public class FactoryA extends AbstractFactory {
    public Superclass Create() {
        return new SubclassA();
    }
}

public class FactoryB extends AbstractFactory {
    public Superclass Create() {
        return new SubclassB();
    }
}

Now, you can eg create a method that takes an AbstractFactory (which, in reality, will be either a FactoryA or a FactoryB ).现在,您可以例如创建一个采用AbstractFactory的方法(实际上,它将是FactoryAFactoryB )。 Calling Create() on this object will produce either a SubclassA or a SubclassB .在此 object 上调用Create()将产生一个SubclassA或一个SubclassB

Edit: Fixed compilation error (forgot to make the factories extend AbstractFactory ).编辑:修复编译错误(忘记让工厂扩展AbstractFactory )。

If you absolutely have to you can use this code form a static context:如果您绝对需要,您可以使用此代码形成 static 上下文:

Class cls = new Object() { }.getClass().getEnclosingClass();
Object instance = cls.newInstance();

Your class need to have a nullary constructor.您的 class 需要有一个空构造函数。

You need some hacks to achieve this.你需要一些技巧来实现这一点。 One way I can think of to obtain this is:我能想到的一种方法是:

public static <T extends SuperClass> T factory(Class<T> clazz) {
    return clazz.newInstance();
}

I don't think it is possible in Java to find out the name of the "current" subclass.我认为不可能在 Java 中找出“当前”子类的名称。 And especially some dynamic object generation won't be possible.尤其是某些动态 object 生成将是不可能的。

So you'll need to define that static function in every subclass instead.因此,您需要在每个子类中定义 static function 。

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

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