简体   繁体   English

Java 中的引用变量究竟是什么? 它与其他变量有何不同?

[英]What exactly is a reference variable in Java? How does it differ from other variables?

I have been studying Inheritance in Java and the writer makes the statement "it is the type of object being referred to (not the type of reference variable) that determines which version of an overridden method will be executed."我一直在研究 Java 中的继承,作者声明“是被引用的对象类型(而不是引用变量的类型)决定了将执行哪个版本的重写方法。” This statement is awfully confusing.这个说法非常混乱。

What the book is referring to is polymorphism , more specifically through dynamic dispatch .这本书所指的是多态性,更具体地说是通过动态分派

In a nutshell, imagine the following classes:简而言之,想象一下以下类:

public class Person {

    public Person() {
    }

    public void introduceYourself() {
    } 

}

public class Texan extends Person {

    public Texan() {
    }

    public void introduceYourself() {
        System.out.printLn("Howdy y'all!");
    } 

}

public class NewYorker extends Person {

    public NewYorker() {
    }

    public void introduceYourself() {
        System.out.printLn("Yo. You got a problem with that?");
    } 

}

Now, let's create a reference variable of type Person .现在,让我们创建一个Person类型的引用变量。

Person myBFF;

Let's instantiate him让我们实例化他

myBFF = new NewYorker();

Let's ask him to introduce himself我们请他做自我介绍

myBFF.introduceYourself();

This prints:这打印:

Yo.哟。 You got a problem with that?你有什么问题吗?

Now, let's change your BFF to a Texan.现在,让我们将您的 BFF 更改为德克萨斯人。

myBFF = new Texan();

Let's call that same line again and ask our BFF to introduce himself.让我们再次拨打同一条线路,请我们的 BFF 自我介绍。

myBFF.introduceYourself();

This prints:这打印:

Howdy y'all!大家好!

In each case, the reference variable you were using was of type Person .在每种情况下,您使用的引用变量都是Person类型。 The instance of the variable, in each case, was NewYorker and Texan respectively.在每种情况下,变量的实例分别是NewYorkerTexan That instance type determines which verson of introduceYourself() is called.实例类型决定了调用的是 IntroductionYourself () 的哪个版本。

A reference variable is the type you specify on the left hand side (a variable that holds a reference type).引用变量是您在左侧指定的类型(保存引用类型的变量)。 What the author is referring to is when the right hand side then differs.作者所指的是当右手边不同时。 Consider考虑

Object a = new Foo(); 
System.out.println(a.toString());

if Foo overrides the Object.toString() (that is, if Foo provides a public String toString() ) method then it is Foo 's toString that is invoked (not Object 's).如果Foo覆盖Object.toString()即,如果Foo提供了一个public String toString()方法,然后FootoString被调用(不Object的)。 See also Overriding and Hiding Methods in the Java tutorials.另请参阅 Java 教程中的覆盖和隐藏方法

A reference variable looks like this :参考变量如下所示:

Coordinate cords; //Cords is the ref. var

Inside of that reference variable is an address inside of your computer's RAM which stores the attributes of that object.引用变量内部是计算机 RAM 内的地址,用于存储该对象的属性。 Since we did not instantiate (actually make an object of) the address of the aforementioned cords object is null由于我们没有实例化(实际上是创建一个对象),上述cords对象的地址为null

Reference variables hold addresses to reserved parts of memory.引用变量保存内存保留部分的地址。

cords = new Coordinate(0.0,0.0,0.0);

Now inside of the RAM of the computer is a reserved space that holds three floating type variables.现在计算机的 RAM 内部是一个保留空间,其中包含三个浮动类型的变量。 Upon instantiation, a reference variable holds the address.在实例化时,引用变量保存地址。 So what can we do with addresses in Java?那么我们可以用 Java 中的地址做什么呢?

Nothing of use.没什么用。 Java memory address are useless, and you can not see them(Though they look something like 0xFFFFFFF) Java内存地址没用,你看不到它们(虽然它们看起来像0xFFFFFFF)

For a visual representation click here有关视觉表示,请单击此处

Reference variable is a term to indicate/explain that a variable is only referring to a type and yet not instantiated.引用变量是一个术语,用于指示/解释变量仅引用一种类型而尚未实例化。 Also after instantiation it's type will be something that would depend on how or by which class a reference variable will be instantiated.同样在实例化之后,它的类型将取决于引用变量将如何或由哪个类实例化。

Say from the above example by Robert Columbia saying从上面的例子中说罗伯特哥伦比亚说

   Person myBFF;

Or by the example of Brenann或者以 Brenann 为例

   Coordinate cords; //Cords is the ref. var

In both these cases myBFF and cords are only referring to Person or Coordinate classes respectively.在这两种情况下,myBFF 和cords分别仅指Person 或Coordinate 类。 Note that they are not instantiated yet!请注意,它们尚未实例化!

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

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