简体   繁体   English

可以在Java中为变量分配其他类型吗?

[英]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). 如果TableTypeATableTypeB都实现了一个称为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.

相关问题 Java switch case语句返回不同类型的值(不同的数据类型)并分配给var类型的相同变量 - Java switch case statement is returning different type of values(different data-type) and is assigned to same variable of type var 如果不同的Java泛型类型参数具有相同的名称,为什么可以为其分配不同的类型? - If different Java generic type parameter have the same name, why it can be assigned different types? 是否可以为值分配或与值类型变量进行比较? - Can null be assigned to or be compared to a value type variable? Java:最终的局部变量无法分配,因为它是用封闭类型定义的 - Java: the final local variable cannot be assigned, since it was defined in an enclosing type 分配给另一个的 ArrayList 类型变量在 Android Java 中作为引用传递 - ArrayList type variable assigned to another is passed as reference in Android Java 分配给Java中更广泛类型的不同实现之间的差异 - Difference between different implementations assigned to broader type in Java 最终局部变量无法分配,因为它是在封闭类型java中定义的 - The final local variable cannot be assigned, since it is defined in an enclosing type java 为什么不能将嵌套通配符分配给带有类型参数的变量? - Why can't a nested wildcard be assigned to a variable with a type parameter? 是否可以将属于某个类的对象分配给属于子类的类型的变量而无需强制转换? - Can an object belong to a class be assigned to a variable of a type that is a subclass without casting? Java - 可以分配枚举的未初始化的最终变量而不会产生错误? - Java - Uninitialized final variable of an Enum can be assigned without producing error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM