简体   繁体   English

是否有可能制作一种可能是不同的新变量?

[英]Is it possible to make a new kind of variable that can be different things?

I want to make a new variable with which i can do the following: 我想创建一个新变量,我可以使用它来执行以下操作:

Variable v1 = 1;
Variable v2 = "Test";
Variable v3 = 7.31;

Is this possible, or is this impossible? 这可能,或者这是不可能的?

If you take advantage of auto-boxing (Java SE 5.0 and later) then every one of those can be stored in an Object (ie the base class of all Java types): 如果你利用自动装箱(Java SE 5.0及更高版本),那么每一个都可以存储在一个Object (即所有Java类型的基类):

public class foo {
    static public void main(String[] params) {
        Object v1 = 1;
        Object v2 = "Test";
        Object v3 = 7.31;

        System.out.println(v1 + " " + v1.getClass().getName());
        System.out.println(v2 + " " + v2.getClass().getName());
        System.out.println(v3 + " " + v3.getClass().getName());
    }
}

output: 输出:

% java foo
1 java.lang.Integer
Test java.lang.String
7.31 java.lang.Double

This does not necessarily make it a good idea, though! 但这并不一定是个好主意!

The output shown above shows how although every variable was declared as an Object , their real class name is one more appropriate for their type. 上面显示的输出显示了虽然每个变量都被声明Object ,但它们的真实类名称更适合它们的类型。 It's only because every class in Java is derived from Object that the declarations can work that way. 这只是因为Java中的每个类都是从Object派生的,声明才能以这种方式工作。

It is possible. 有可能的。 Check the code below: 检查以下代码:

    Object a;
    Object b;
    Object c;

    a = 1;
    b = "abc";
    c = 1.72d;

or another way: 或另一种方式:

    Object a;

    a = 1;
    a = "abc";
    a = 1.72d;

In this case we observe autoboxing , when a value is wrapped to a suitable object type. 在这种情况下,当值被包装到合适的对象类型时,我们会观察到自动装箱

As Object class is the parent for all java objects, then we have a way to code as I described above. 由于Object类是所有java对象的父类,因此我们可以像上面描述的那样编写代码。

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

相关问题 哪些事情可以用 Java 而不是 Python? - What kind of things can be done with Java but not Python? 您可以更改变量的值,但是用旧值实例化的所有其他内容都可以更改为新值吗? - Can you change the value of a variable, but have all other things that were instantiated with the old value change to the new value? 如何使JTable单元在单击时执行与双击不同的操作? - How can I make a JTable cell do different things on single-click than on double-click? 制作不同类型的构建的maven标准是什么? - What is the maven standard to make different kind of builds? 创建一个可以返回两个不同内容的方法 - Creating a method that can return two different things Android> Google Maps>叠加层:点按不同的内容以使不同的事情发生 - Android > Google Maps > Overlay: Tap different things to make different things happen 是否可以在同一方法中将对象转换为两个不同的东西? - Is it possible to cast an object as 2 different things within the same method? 使相同的常规JButton根据其子类做不同的事情 - Make the same general JButton do different things depending on its subclass 根据Javafx中的对象,使鼠标按下时发生不同的事情 - Make different things occur on Mouse Pressed depending on Object in Javafx 为什么标记Java变量volatile会降低同步性? - Why does marking a Java variable volatile make things less synchronized?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM