我有这门课:
public class TestSubject {
public TestSubject(List<Integer> list) {
}
}
Run Code Online (Sandbox Code Playgroud)
我正在以这样的方式实例化它并以某种方式它正在工作,即使我正在插入一个ArrayList<String>接受的构造函数List<Integer>:
List<String> strings = new ArrayList<>();
strings.add("foo");
Constructor<TestSubject> constructor = TestSubject.class.getConstructor(List.class);
TestSubject test = constructor.newInstance(strings);
Run Code Online (Sandbox Code Playgroud)
这是我在实例化后看到的:
这怎么可能?
另外,如何从实例化代码中确保正在使用正确类型的列表?
考虑以下通用转换器类(为简洁起见,已简化/不完整)...
public abstract class BaseConverter <TModel>
{
public void convert(String data, Class<TModel> classOfModelType)
{
}
public void convert(String data)
{
convert(data, TModel.class); // This doesn't work!
}
}
Run Code Online (Sandbox Code Playgroud)
在子类外部,我可以convert毫无问题地调用第一个。将此子类视为Car泛型类型参数。我可以进去Car.class,像这样...
public class CarConverter extends BaseConverter<Car>
{
public void someFunction()
{
String someString;
convert(someString, Car.class);
}
}
Run Code Online (Sandbox Code Playgroud)
我想做的就是尽可能多地移动到基类。由于 'Car' 在这里是已知的,因此convert需要Car.class, 并在基类中TModel表示Car(无论如何对于这个具体的 BaseConverter ),我试图TModel.class在基类中使用,但如上所述,它不起作用。
那么如何在 Java 中实现这一点呢?
我一直在寻找设计思路来解决Java中的这个问题.
我正在使用一个库(我无法改变它),对于这个例子,我只是称之为"动物".它包含一个Animal接口和一堆实现; 我需要根据动物的实现调用不同的方法:
List<Animal> animals = Service.getAnimals();
for(Animal a : animals) {
process(a);
}
private void process(Animal animal) {
if (animal instanceOf Cat) {
processCat(animal);
} else if (animal instanceOf Dog) {
processDog(animal);
} else {
System.out.println("unsopported animal");
}
}
Run Code Online (Sandbox Code Playgroud)
我目前正在通过反射解决这个问题,一个类包含所有"处理器"并使用它来调用它们
String methodName = "process" + Animal getClass().getSimpleName(); //ugh
Run Code Online (Sandbox Code Playgroud)
我正在使用Java 8,我很确定必须有更好的设计来解决这个问题.
任何帮助表示赞赏!
我正在研究java中的反射。我知道这是一个常见问题,并且有很多关于它的文章,但我有点困惑,似乎无法找到适合我想要实现的目标的正确解决方案。
我有 :
public class ClassA {
private List<?> myList;
// getters, setters
}
Run Code Online (Sandbox Code Playgroud)
“myList”将包含对象列表(DTO):
List<?> myList; // could be List<DTO1> or List<DTO2> ...
Run Code Online (Sandbox Code Playgroud)
我正在尝试从另一个类获取列表的类型,如下所示:
public class ClassB {
public void myMethod() {
// get type of ClassA.getMyList()
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用 getClass()、getType() 之类的东西时,我只能得到“ArrayList”...尝试获取列表中元素的类会导致“LinkedHashMap”。
我真的想通过使用列表的通配符类型来实现这一点。
那么有没有一种方法可以用最少的代码获取 myList 的类型?
编辑 :
谢谢大家的答案。
只有一件事:myList包含 type 的元素LinkedHashMap,这在我的帖子中不是很清楚。
看来这确实是做不到的。每一个都LinkedHashMap代表一个对象,但你不知道是哪一个。它们只是键+值的列表。
我有一个静态方法,它将根据类的类型返回自定义类型,
public class GenericMethod {
public static <T> T returnGeneric(Class<T> clazz) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我想传递一个带有泛型类的类,
CustomType<String> type = GenericMethod.returnGeneric(CustomType.class);
Run Code Online (Sandbox Code Playgroud)
唯一的问题是上面的语句给出和取消选中转换警告.
我尝试了解决new CustomType<String>().getName()问题的解决方法.
有没有正确的方法,或唯一的解决方案是使用@SuppressWarnings?
有没有办法获得泛型方法的返回类型 - 返回类型?
public interface MyGeneric<T> {
T doSomething();
}
public interface MyElement extends MyGeneric<Element> {
}
public class Main {
public static final void main(String[] args) {
System.out.println(MyElement.class.getMethod("doSomething", new Class<?>[0]).magic()); // => Element
}
}
Run Code Online (Sandbox Code Playgroud)
使用Method.getReturnType()我得到java.lang.Object没有.方法"魔法"是否存在?
我有以下方法:
public <U, V> boolean isEqual(List<U> a, List<V> b) {
// check if U == V
}
Run Code Online (Sandbox Code Playgroud)
我要检查,如果U和V是同一类.
请考虑以下代码:
public class Context {
private final Class<?> clazz;
private final String resource;
private final com.thirdparty.Context context;
public Context(final String resource, final Class<?> clazz) {
this.clazz = clazz;
this.resource = resource;
this.context = com.thirdparty.Context.newInstance(this.clazz);
}
public String marshall(final Object element) {
return this.context.marshall(element);
}
public Object unmarshall(final String element) {
return this.context.unmarshall(element);
}
}
Context context = new Context("request.xsd", Request.class);
// Marshall
Request request = new Request();
String xml = context.marshall(request);
// Unmarshall
Request roundTrip = Request.cast(context.unmarshall(xml));
Run Code Online (Sandbox Code Playgroud)
我试图用泛类版本的Context类替换它:
public class …Run Code Online (Sandbox Code Playgroud) 我有一些同时实现Hash和的结构MyTrait。我将它们用作&MyTrait特征对象。
现在我&MyTrait也想实施Hash。我已经尝试了几件事:
天真地trait MyTrait: Hash {}:
the trait `MyTrait` cannot be made into an object
Run Code Online (Sandbox Code Playgroud)然后我尝试了这个:
impl Hash for MyTrait {
fn hash<H: Hasher>(&self, hasher: &mut H) {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
但是我认为我需要委托给hash具体类型的方法self。
因此,天真的下一步是戴上它MyTrait:
fn my_hash<H: Hasher>(&self, hasher: &mut H);
Run Code Online (Sandbox Code Playgroud)
这使我回到了第一点。
我读了一些有关使用特征对象而不是通用参数的内容,这听起来很聪明,因此我将其放在 MyTrait
fn my_hash(&self, hasher: &mut H);
Run Code Online (Sandbox Code Playgroud)
然后,我需要实际执行此操作。最好不要用手为每个特征:
impl<T: 'static + Hash> MyTrait for T {
fn as_any(&self) -> &Any { …Run Code Online (Sandbox Code Playgroud)我有这个编译问题:
这是有问题的课程:
package huru.entity;
import io.vertx.core.json.JsonObject;
import java.util.Date;
public class BaseEntity <T extends BaseModel> extends JsonObject {
private T model;
public BaseEntity(T m){
this.model = m;
}
public void setUpdateInfo(String user){
this.model.updatedBy = user;
this.model.updatedAt = new Date();
}
public JsonObject toJsonObject(){
return JsonObject.mapFrom(this.model);
}
public T getEntityType (){
return this.model.getClass(); // doesn't compile
}
}
Run Code Online (Sandbox Code Playgroud)
我也试过用
public T getEntityType (){
return T; // doesn't compile
}
Run Code Online (Sandbox Code Playgroud)
但这显然也无效.有谁知道如何返回该泛型类的类实例?
我也试过这个:
public Class<T> getEntityType (){
return this.model.getClass();
}
Run Code Online (Sandbox Code Playgroud)
我得到:
然后我尝试了这个:
public Class<? …Run Code Online (Sandbox Code Playgroud) java ×9
generics ×7
reflection ×2
class ×1
constructor ×1
rust ×1
traits ×1
types ×1
vert.x ×1
wildcard ×1