[英]Can a variable be assigned different type in java?
I was wondering if it's possible in Java to have a variable that can potentially be different types? 我想知道在Java中是否有可能具有不同类型的变量? To be specific, I have two different objects of different class, both of them are tables.
具体来说,我有两个不同类的不同对象,它们都是表。 So let's say we have:
假设我们有:
TableTypeA t1;
TableTypeB t2;
Let's say that both classes have a method called getSelectedRow(), is it possible for me to make a third variable so that I can do the following: 假设两个类都有一个名为getSelectedRow()的方法,是否可以使我创建第三个变量,以便执行以下操作:
SomeType t3;
t3 = t1;
t3.getSelectedRow();
t3 = t2;
t3.getSelectedRow();
TableTypeA and TableTypeB do not extend the same class, the closest common superclass they have do not have the getSelectedRow() method. TableTypeA和TableTypeB没有扩展相同的类,它们拥有的最接近的公共超类没有getSelectedRow()方法。 Seeing how similarly they function, one of them should probably be extending the other or their classes should've been merged together, but let's say a structural change like that is not an option, is what I'm suggesting possible to do?
看到它们的功能有多么相似,它们中的一个可能应该扩展另一个,或者应该将它们的类合并在一起,但是让我们说这样的结构性改变不是一种选择,我建议可以这样做吗?
Thanks. 谢谢。
Edit: I think I will add an interface for these, thank you very much for all of the quick responses! 编辑:我想我将为这些添加界面,非常感谢您的所有快速回复!
If the classes implement an interface that has getSelectedRow()
, sure. 如果这些类实现了具有
getSelectedRow()
的接口,请确保。
Such a change isn't structural/organizational in the sense that re-organizing a class hierarchy is. 在重新组织类层次结构的意义上,这种变化不是结构性/组织性的。
It's possible, if TableTypeA
and TableTypeB
both implement an interface called SomeType
(rather than deriving from a base class). 如果
TableTypeA
和TableTypeB
都实现了一个称为SomeType
的接口 (而不是从基类派生),则是可能的。 Eg: 例如:
interface SomeType {
Row getSelectedRow();
}
class TableTypeA implements SomeType {
Row getSelectedRow() {
// ...
}
}
class TableTypeB implements SomeType {
Row getSelectedRow() {
// ...
}
}
If not, then no, you have to go via reflection . 如果不是,则为否,您必须经过反思 。
It is possible with an interface: 可以使用以下接口:
public interface Table {
Row getSelectedRow();
}
public class TableTypeA implements Table {
Row getSelectedRow() { // Must be defined
}
}
public class TableTypeB implements Table {
Row getSelectedRow() { // Must be defined
}
}
Without a change to the architecture of the classes, it is possible to use the instanceof
operator and typecasts. 在不更改类的体系结构的情况下,可以使用
instanceof
运算符和类型转换。
... but let's say a structural change like that is not an option, is what I'm suggesting possible to do?
...但是我们不能说像这样的结构性改变,我建议可以这样做吗?
Without a change to the types involved, it is not possible to do that. 如果不更改所涉及的类型,则不可能这样做。 The best you can hope for is to write some ugly code using
instanceof
and type casts, and hide it inside a method. 您所希望的最好的办法是使用
instanceof
和类型强制转换编写一些难看的代码,并将其隐藏在方法中。 Reflection is an alternative, but that is uglier and more expensive (IMO). 反射是一种替代方法,但是丑陋且昂贵(IMO)。
Without a common Interface or super Class, no, not in a straight forward way. 没有通用的接口或超类,没有,不是直接的方法。 The compiler will complain about the assignment and request an explicit cast.
编译器将抱怨分配并请求显式强制转换。 The explicit cast would cause a
ClassCastException
. 显式强制转换将导致
ClassCastException
。
An alternative would be to use reflection. 另一种选择是使用反射。
Only if they share an interface. 只有它们共享一个接口。 This isn't like JavaScript.
这不像JavaScript。
不,Java不支持鸭子类型 。
if you need this behaviour AND you cannot change the super classes/implement interfaces then all you can do is create wrapper classes. 如果您需要这种行为并且不能更改超类/实现接口,那么您所能做的就是创建包装器类。
interface Table{ Row getSelectedRow(); }
class WrapperA extends TableTypeA implements Table{
// copy all constructors and forward to super()
}
class WrapperB extends TableTypeB implements Table{
// same
}
If you don't control the original classes, Adapters can come in handy http://en.wikipedia.org/wiki/Adapter_pattern 如果您不控制原始类,则适配器可以派上用场http://en.wikipedia.org/wiki/Adapter_pattern
public interface TableAdapter {
Row getSelectedRow();
}
public class TableTypeAAdapter implements TableAdapter {
private TableTypeA tableTypeA;
public TableTypeAAdapter(TableTypeA tableTypeA) {
this.tableTypeA = tableTypeA;
}
public Row getSelectedRow() {
tableTypeA.getSelectedRow();
}
}
And a similar class for TableTypeB 和TableTypeB的相似类
Then: 然后:
TableAdapter table = new TableTypeAAdapter(tableTypeA);
table.getSelectedRow();
table = new TableTypeBAdapter(tableTypeB);
table.getSelectedRow();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.