简体   繁体   English

在Java中不使用参数化类型就引用其中一种

[英]Reference a kind of that without using parameterized types in Java

I have a class that needs to maintain or know about (has-a) reference to three kinds of other classes. 我有一个需要维护或了解(有一个)对其他三种类型的引用的类。 What is the best way to provide such a reference? 提供此类参考的最佳方法是什么?

class Hello<T extends IsLetter> {
    private T refOtherClass;
}
// getter and setter for refOtherClass here

interface IsLetter {}

class A implements IsLetter {
    private String a = "A data";
}
class B implements IsLetter {
    private String b = "B data";
}

now is there a way to do this without using parameterized types? 现在有一种方法可以不使用参数化类型来做到这一点吗? Since if Im using parameterized types, any class that uses Hello will need to provide it when creating it (which is fine) but also when using Hello, i cant just say 因为如果我使用参数化类型,那么任何使用Hello的类都需要在创建它时提供它(很好),而且在使用Hello时也必须提供它,我不能只说

Hello hi = new Hello(); 
// set the refOtherClass ...
hi.getRefOtherClass(); // and here use the refOtherClass like it was of type A
or B, when using hi I need to provide, again, the type that was put in hi.

Hints? 提示?

You can of course use a cast: 您当然可以使用强制转换:

A a = (A) hi.getRefOtherClass();

What generics do here is move the verification of this cast to an earlier stage - compile time. 泛型在这里所做的工作是将对该强制类型转换的验证移至更早的阶段-编译时间。 If you don't use generics, and if you try to cast to the wrong type, you'll get the error at runtime. 如果不使用泛型,并且尝试转换为错误的类型,则在运行时会收到错误消息。

This is in case you need to work with a concrete type. 这是万一您需要使用具体类型的情况。 If the interface IsLetter (better call it just Letter ) has all the methods, just make the method return Letter and work with the interface. 如果接口IsLetter (最好称其为Letter )具有所有方法,则只需使该方法返回Letter并使用该接口即可。

You can get the exact class of an object by calling object.getClass() , or perhaps use an if-clause like if (a instanceof A) 您可以通过调用object.getClass()来获取对象的确切类,或者可以使用if (a instanceof A)子句

Your poor explanation probably didn't get you any anwers. 您的拙劣解释可能并没有给您带来任何麻烦。 But here is a try of what i think you are asking. 但是,请尝试一下我认为您要问的问题。

So first you need to do this: 所以首先您需要这样做:

Hello<A> hi = new Hello<A>(); 

Because you are using generics in Java. 因为您在Java中使用泛型。

Now you can do 现在你可以做

A a = hi.getRefOtherClass();
//or
IsLetter a = hi.getRefOtherClass();

Finally, you should probaby put a method on you interface like so: 最后,您应该在接口上放一个方法,如下所示:

interface IsLetter{ String getValue();}

Which of course then you can do a.getValue(); 那你当然可以做a.getValue(); And because of the beauty of OO, you can return "B VALUE" from B or "A Value" from A. 并且由于OO的优点,您可以从B返回“ B VALUE”或从A返回“ A Value”。

I'm not clear on why you want to do this without generics - this is exactly the sort of situation generics make clearer: 我不清楚您为什么要在没有泛型的情况下执行此操作-这正是泛型使您更加清楚的一种情况:

class Hello<T extends IsLetter> { 
   public T getRefOtherClass(){ ... }
 }

class A implements IsLetter { ... }

class B implements IsLetter { ... }

Hello hi = new Hello<A>();
A a = hi.getRefOtherClass();

You can only get rid of the parameterization by casting the output of the get method, which is messier, or by subclassing the Hello so that the parameterization is "encapsulated" in your subclassing (which might make sense in some cases, although not in a simple example like this ) eg 您只能通过强制转换get方法的输出(它比较杂乱)或通过子类化Hello来摆脱参数化,以便在子类中“封装”参数化(在某些情况下可能有意义,尽管在像这样的简单例子

 class HelloA extends Hello<A> {...
  A a = new HelloA.get()

But again, you're not getting rid of the generics, you're just shifting them around. 但是同样,您并没有摆脱泛型,只是在转移它们。

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

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