什么是类,引用和对象?

问题描述:

我已经编程java 2年了,显然我遇到了一个问题,我不能理解和区分类,引用和一个对象(我不知道为什么我忘了这些概念)。

I've been programming java for 2 years now, and apparently I have encountered a problem where I couldn't understand and differentiate class, reference, and an object again (I do not get why I forget these concepts).

让我们回到这个问题,这是我不知道一个类或引用是否相同,虽然我已经知道什么是对象。

Lets get to down to the problem, which is that I am not sure if a class or reference are the same, though I have already an idea what is object.

有人可以区分一个漂亮,可理解和完整的方式什么是类,引用和对象?

Can someone differentiate in a nice and understandable and complete manner what are classes, references and object?

我知道的是,类更像是一个对象的模板(蓝图到一个房子,其中类是蓝图,房子是一个对象)。

All I know is that the class is more like of a template for an object (blueprint to a house where the class is the blueprint and the house is an object).

如果您喜欢住宅隐喻:


  • 就像房子的蓝图。

  • 您建立的每个房屋(或在OO lingo中实例化)是一个对象,也是已知的作为实例

  • 每个房子也有一个地址,当然。如果你想告诉别人在哪里,你给他们一张带有地址的卡片。

  • 如果您想访问住宅,请查看卡上写的地址。这称为取消引用

  • a class is like the blueprint for a house. Using this blueprint, you can build as many houses as you like.
  • each house you build (or instantiate, in OO lingo) is an object, also known as an instance.
  • each house also has an address, of course. If you want to tell someone where the house is, you give them a card with the address written on it. That card is the object's reference.
  • If you want to visit the house, you look at the address written on the card. This is called dereferencing.

您可以根据需要复制该引用,房子 - 你只是复制的卡上有地址,而不是房子本身。 Java方法总是按值传递,但值可以是对象的引用。所以,如果我有:

You can copy that reference as much as you like, but there's just one house -- you're just copying the card that has the address on it, not the house itself. Java methods are always pass-by-value, but the value could be an object's reference. So, if I have:

Foo myFoo = new Foo();     // 1
callBar(myFoo);            // 2
myFoo.doSomething()        // 4

void callBar(Foo foo) {
    foo = new Foo();       // 3
}

然后让我们看看发生了什么。

Then let's see what's happening.


  1. 一些事情发生在第1行。 new Foo()告诉JVM使用 Foo 蓝图。 JVM会这样做,并返回对房子的引用。然后将此引用复制到 myFoo 。这基本上就像要求承包商建造你的房子。他说,然后告诉你房子的地址;

  2. 在第2行中,您将此地址赋予另一个方法 callBar 。让我们跳到那个方法。

  3. 这里,我们有一个参考 Foo foo 。 Java是传值的,因此 callBar 中的 foo myFoo 参考。想想它给予 callBar 其自己的卡与房子的地址就可以了。 callBar 对这张卡有什么作用?它要求建一个新房子,然后使用你给它的卡来写这个新房子的地址。请注意, callBar 现在无法到达第一个房子(我们在第一行建立的房子),但是房子没有改变, 。

  4. 回到第一种方法,我们取消引用 myFoo 来调用方法( doSomething())。这就像看着卡片,去地址在卡上的房子,然后在那家里做点什么。请注意, myFoo 的地址的卡由 callBar 方法保持不变 - 记住,我们给予 callBar A的复制的我们借鉴的。

  1. Several things are happening in line 1. new Foo() tells the JVM to build a new house using the Foo blueprint. The JVM does so, and returns a reference to the house. You then copy this reference to myFoo. This is basically like asking a contractor to build you a house. He does, then tells you the house's address; you write this address down.
  2. In line 2, you give this address to another method, callBar. Let's jump to that method next.
  3. Here, we have a reference Foo foo. Java is pass-by-value, so the foo in callBar is a copy of the myFoo reference. Think of it like giving callBar its very own card with the house's address on it. What does callBar do with this card? It asks for a new house to be built, and then uses the card you gave it to write that new house's address. Note that callBar now can't get to the first house (the one we built in line 1), but that house is unchanged by the fact that a card that used to have its address on it, now has some other house's address on it.
  4. Back in the first method, we dereference myFoo to call a method on it (doSomething()). This is like looking at the card, going to the house whose address is on the card, and then doing something in that house. Note that our card with myFoo's address is unchanged by the callBar method -- remember, we gave callBar a copy of our reference.

整个序列会是这样的:


  1. 请JVM盖房子。它做,并给我们的地址。我们这个地址拷贝到一个名为卡 myFoo

  2. 我们调用 callBar 。我们这样做之前,我们复制写在 myfoo 来新卡的地址,我们给 callBar 。它调用卡

  3. callBar 询问JVM的另一栋房子。它创建它,并返回新房子的地址。 callBar 复制此地址我们给它卡

  4. 早在第一种方法中,我们看一下我们原来的,不变卡。去地址在我们的卡上的房子;并在那里做某事。

  1. Ask JVM to build a house. It does, and gives us the address. We copy this address to a card named myFoo.
  2. We invoke callBar. Before we do, we copy the address written on myfoo to a new card, which we give to callBar. It calls that card foo.
  3. callBar asks the JVM for another house. It creates it, and returns the new house's address. callBar copies this address to the card we gave it.
  4. Back in the first method, we look at our original, unchanged card; go to the house whose address is on our card; and do something there.