Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我是 Java 泛型的新手,我正在尝试将局部变量设置为传入的类型。传入的类型扩展了自定义类 Model:
如下所示:
public void myMethod(<T extends Model> MyType){ MyType item = (MyType)getObjectFromStore(); }
MyType 可以属于 Model 类或其子类。
这在Java中可能吗?我很欣赏任何正确方向的指示。我意识到这可能是一个基本问题,但我似乎找不到正确的方法来解决这个问题。
你可以 myMethod这样定义:
myMethod
public <T extends Model> void myMethod(T myType) { .. }
它表示方法 parameter( myType) 可以是类型Model或其子类。
myType
Model