传递子类或实现类的类类型;然后访问静态方法并创建该类型的实例;在 Java 中可能吗?如何实现?

问题描述 投票:0回答:1

实现代码的多态性和可重用性;是否可以将一些代表扩展超类(可能是抽象但不一定)的类类型的Java构造传递给方法,然后该方法动态检测类类型并可以调用其静态方法并实例化它的实例?

以下代码不可运行,而只是一个地图/蓝图来传达我对我想要解释的内容的想象。

最重要的是在下面的类

Animal
Zoo
中进行了解释,并在注释中进行了更多解释,但是我并不局限于接口或超类的指定结构,只是我想最好通过多态性来实现代码可重用性,并且不通过反射(如果可能),也不通过
instanceof
/
switch
语句中的
else-if
检查,因此;动态分辨率。

动物界面:

public interface AnimalInterface {

    public String getNameInstanceMethod();

    public void setNameInstanceMethod(String name);

}

动物:

public abstract class Animal implements AnimalInterface {

    /**
     * @return instance of class that extends Animal
     */
    public static Animal getNewInstance(){
        // Is it possible to implement it to return an instance
        // of a type which extends this Animal abstract class
    }
}

狗:

public class Dog extends Animal {

    private static final String DB_ENTITY = "dogs";
    private String name;
    
    public static String getDB_entity_by_staticMethod(){
        return Dog.DB_ENTITY;
    }

    @Override
    public String getNameInstanceMethod() {
        return name;
    }

    @Override
    public void setNameInstanceMethod(String name) {
        this.name = name;
    }
}

猫:

public class Cat extends Animal {
    
    private static final String DB_ENTITY = "cats";
    private String name;
    
    public static String getDB_entity_by_staticMethod(){
        return Cat.DB_ENTITY;
    }

    @Override
    public String getNameInstanceMethod() {
        return name;
    }

    @Override
    public void setNameInstanceMethod(String name) {
        this.name = name;
    }
}

动物园

public class Zoo {

    /**
     * The passed argument (AnimalClassType) to processAnimalDetails() must NOT
     * be instance, but just type such as Dog.class or Cat.class, or whatever
     * Java construct that conveys class that extends Animal without creating
     * dummy instance so that Java dynamically detect the subclass without
     * reflection (if possible) and without instanceof checks within switch / else-if
     * statements.
     */
    public static void processAnimalDetails(/*AnimalClassType*/) {
        /**
         * AnimalClassType determined dynamically at run time; If method
         * argument was Dog.class then method returns "dogs", If method argument
         * was Cat.class then method returns "cats".
         */
        String dbEnitiyName = AnimalClassType.getDB_entity_by_staticMethod();
        // dbEnitiyName can be used to retrieved from db table
        String animalNamefromDatabase = "Name retrieved from database";
        // Since AnimalClassType will be determined at run time;
        // If method argument was Dog.class, then animalInstance with be instance of Dog class
        // If method argument was Cat.class, then animalInstance with be instance of Cat class
        Animal newAnimalInstance = AnimalClassType.getNewInstance();
        newAnimalInstance.setNameInstanceMethod(animalNamefromDatabase);
    }
}

主要

public class Main {

    public static void main(String[] args) {
        /**
         * Pass Dog.class or whatever Java construct that conveys class that extends Animal
         * without creating dummy instance, so that we access static methods of the class
         * and create instances of it.
         */
        Zoo.processAnimalDetails(Dog.class);
    }
}
java dynamic polymorphism
1个回答
0
投票

创建工厂接口:

interface AnimalFactory {
  String getDbEntityName();
  Animal createInstance();
}

实现

Dog
Cat

class DogFactory implements AnimalFactory {
 ...
}

那么你的

main
方法是:

Zoo.processAnimalDetails(new DogFactory());

并且

processAnimalDetails
是:

public static void processAnimalDetails(AnimalFactory factory) {

        String dbEnitiyName = factory.getDBEntityName();
        String animalNamefromDatabase = "Name retrieved from database";
        
        Animal newAnimalInstance = factory.getNewInstance();
        newAnimalInstance.setNameInstanceMethod(animalNamefromDatabase);
    }
© www.soinside.com 2019 - 2024. All rights reserved.