什么是类、引用和对象? [英] What are classes, references and objects?

查看:28
本文介绍了什么是类、引用和对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编程 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 行话)的每个房屋都是一个对象,也称为实例.
  • 当然,每个房子也有一个地址.如果你想告诉别人房子在哪里,你可以给他们一张写有地址的卡片.该卡片是对象的引用.
  • 如果您想参观房子,请查看卡片上写的地址.这称为解除引用.
  • 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.

您可以随意复制该参考,但只有一所房子——您只是复制上面有地址的卡片,而不是房子本身.

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中,不能直接访问对象,只能使用引用.Java 不会相互复制或分配对象.但是您可以复制和分配对变量的引用,以便它们引用同一个对象.Java 方法总是按值传递,但值可以是对象的引用.所以,如果我有:

In Java, you can not access objects directly, you can only use references. Java does not copy or assign objects to each other. But you can copy and assign references to variables so they refer to the same object. 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 中的 foomyFoo 引用的copy.把它想象成给 callBar 一张自己的卡片,上面有房子的地址.callBar 用这张卡做什么?它要求建造一座新房子,然后用你给它的卡片写下新房子的地址.请注意,callBar 现在无法到达第一所房子(我们在第 1 行中建造的房子),但是由于过去在卡片上有其地址的事实,该房子没有改变,现在上面有其他房子的地址.
  4. 回到第一个方法,我们取消引用 myFoo 以在其上调用一个方法 (doSomething()).这就像看卡片,去卡片上地址的房子,然后在那房子里做一些事情.请注意,带有 myFoo 地址的卡片没有被 callBar 方法改变——记住,我们给了 callBar 一个 copy 我们的参考.
  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.它调用那张卡片 foo.
  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.

这篇关于什么是类、引用和对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆