Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
关于Java反射的几个问题_Java_Inheritance_Reflection - Fatal编程技术网

关于Java反射的几个问题

关于Java反射的几个问题,java,inheritance,reflection,Java,Inheritance,Reflection,我有父类实体: package incubator; import java.lang.annotation.Annotation; import java.lang.reflect.Field; public class Entity { private String getTableName() { String result = null; Class<?> cl = getClass(); System.out.p

我有父类
实体

package incubator;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

public class Entity {

    private String getTableName() {
        String result = null;
        Class<?> cl = getClass();
        System.out.println(cl.getName());
        for (Annotation a : cl.getAnnotations()) {
            if (a instanceof EntityTable) {
                EntityTable ent = (EntityTable) a;
                result = ent.name();
                break;
            }
        }
        return result;
    }

    private String getKeyName() {
        String result = null;
        Class<?> cl = getClass();
        System.out.println(cl.getName());
        for (Field f : cl.getDeclaredFields()) {
            for (Annotation a : f.getAnnotations()) {
                if (a instanceof PrimaryKey) {
                    PrimaryKey ann = (PrimaryKey) a;
                    result = ann.name();
                }
            }
        }
        return result;
    }

    public Entity get(int id) throws IllegalAccessException, InstantiationException {
            System.out.println("SELECT * FROM "
                    + getTableName() + " WHERE (" + getKeyName() + "=?);");
            return getClass().newInstance();
    }

    public void delete() {
            System.out.println("DELETE FROM "
                    + getTableName() + " WHERE (" + getKeyName() + "=?);");
    }

}
所有注释都类似于

package incubator;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface EntityTable {
    String name();
}
所以,我有一个问题:有没有办法让类
实体的静态方法
get(final int id)
返回
子对象的实例?如何在父类中指定子类的结果类型

谢谢你为我浪费时间。致以最良好的祝愿

为此使用泛型:

// Subclasses will have to pass their type as a generic type argument
// Which you will use to declare the return type of get()

class Entity<T extends Entity<T>> {
    T get(int id) {
        T value = ...; // Load the value from db or whatever
        return value;
    }
}

// Child tells its parent that the dynamic type is Child
class Child extends Entity<Child> {

}
//子类必须将其类型作为泛型类型参数传递
//您将使用它来声明get()的返回类型
类实体{
T get(int-id){
T value=…;//从db或其他任何地方加载值
返回值;
}
}
//Child告诉其父级动态类型为Child
类子扩展实体{
}
为此使用泛型:

// Subclasses will have to pass their type as a generic type argument
// Which you will use to declare the return type of get()

class Entity<T extends Entity<T>> {
    T get(int id) {
        T value = ...; // Load the value from db or whatever
        return value;
    }
}

// Child tells its parent that the dynamic type is Child
class Child extends Entity<Child> {

}
//子类必须将其类型作为泛型类型参数传递
//您将使用它来声明get()的返回类型
类实体{
T get(int-id){
T value=…;//从db或其他任何地方加载值
返回值;
}
}
//Child告诉其父级动态类型为Child
类子扩展实体{
}

由于类型擦除,在运行时无法在静态上下文中获取实际类型。调用该方法时,必须显式声明该类。使用通用方法,它将如下所示:

public static <T extends Entity> T get(int id, Class<T> clazz) {
    return clazz.newInstance();
}
publicstatict-get(int-id,Class-clazz){
返回clazz.newInstance();
}

我不确定这在您的情况下是否有用,但这是您想要变为静态的唯一方法。

由于类型擦除,在运行时无法在静态上下文中获取实际类型。调用该方法时,必须显式声明该类。使用通用方法,它将如下所示:

public static <T extends Entity> T get(int id, Class<T> clazz) {
    return clazz.newInstance();
}
publicstatict-get(int-id,Class-clazz){
返回clazz.newInstance();
}

我不确定这在您的情况下是否有用,但如果您想保持静态,这是唯一的方法。

为什么要重新发明轮子?看看自动生成查询的Spring数据。我只想试试反射API。我知道Hibernate等。你为什么要重新发明轮子?看看自动生成查询的Spring数据。我只想试试反射API。我知道Hibernate等等。