Java 向没有使用过“类”和“对象”的人定义“类”和“对象”这两个词的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3686647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 03:29:52  来源:igfitidea点击:

What's the best way to define the words "class" and "object" to someone who hasn't used them?

javaoopterminology

提问by Dean J

My neighbor is taking "Intro to Java", and asked me to help explain a few of the first-day concepts. I realized that since I do this everyday, I don't have the beginner's mind, and it's hard to relate some of this stuff from scratch.

我的邻居正在学习“Java 简介”,并要求我帮助解释一些第一天的概念。我意识到,由于我每天都这样做,我没有初学者的头脑,并且很难从头开始关联一些这些东西。

The one that's actually not trivial for me to explain is "what the heck is a class?"

对我来说实际上并不容易解释的是“类到底是什么?”


Best I have so far:


迄今为止我最好的:

  • A variableholds some kind of data; one variable might be a first name, another variable might be your weight in pounds.

  • A methodis a function, it does stuff, and can do stuff with those variables. A method might display your name on screen, or tell you how much weight you should lose to have a good BMI ratio.

  • An objectholds both variables and methods; one object might represent you, a second object might represent me.

  • A classis kind of the blueprint or template that describes the methods and variables that will be in each object. An object is an instantiated (instance of a) class; an object is something, while the class is simply the plans to make that something.

  • 变量持有某种数据; 一个变量可能是名字,另一个变量可能是您的体重(以磅为单位)。

  • 一个方法是一个函数,它的东西,可以用这些变量做的东西。一种方法可能会在屏幕上显示您的姓名,或者告诉您应该减掉多少体重才能获得良好的 BMI 比率。

  • 一个对象同时拥有变量和方法;一个物体可能代表你,第二个物体可能代表我。

  • 是一种描述方法和变量,将在每个对象的蓝图或模板。一个对象是一个实例化的(一个)类的实例;一个对象是某种东西,而类只是制造某种东西的计划。

Continuing the example, we have a Person object, which is instantiated to hold Alice's data, and another Person object instantiated to hold Bob's data, and another for Carol, and so on.

继续这个例子,我们有一个 Person 对象,它被实例化来保存 Alice 的数据,另一个 Person 对象被实例化来保存 Bob 的数据,另一个用于 Carol,依此类推。



How do I tune this example to make more sense, and/or what's a better approach? The word "instantiated" feels too heavy at this point.

我如何调整这个例子以使其更有意义,和/或什么是更好的方法?在这一点上,“实例化”这个词感觉太沉重了。

(I think this is a useful question, but is obviously subjective; marked as community wiki.)

(我认为这是一个有用的问题,但显然是主观的;标记为社区维基。)

采纳答案by McDowell

A class and some class instances:

一个类和一些类实例:

Courtesy of wikipedia

由维基百科提供

(public domain image hosted by wikipedia)

由维基百科托管的公共领域图像

回答by duffymo

class == cookie cutter, object == cookie.

类 == 饼干切割机,对象 == 饼干。

回答by Paul Sasik

Class : Object :: Blueprint : Building

类:对象::蓝图:建筑

回答by T.J. Crowder

"Car" is a class. Mycar, sitting in my driveway, is an instance (object).

“车”是一类。我的车停在我的车道上,是一个实例(对象)。

回答by karim79

Panda DNA is a class. A Panda running around, eating and performing Panda-like activities is an object.

熊猫 DNA 是一个类。一只熊猫跑来跑去,吃东西,进行类似熊猫的活动是一个对象。

回答by epotter

If and only if he is familiar with Plato's Theory of Forms, you can make an analogy where classes are like Plato's forms and objects are like Plato's real world objects.

当且仅当他熟悉柏拉图的形式论时,您才能进行类比,其中类就像柏拉图的形式,对象就像柏拉图的现实世界对象。

See this postfor a full description.

有关完整说明,请参阅此帖子

回答by Colin Hebert

One of the examples I use during my java courses is the Humanclass.

我在 Java 课程中使用的示例之一是Humanclass

Everyone reading this is a Human(I least I hope so !), we all have our differences our resemblances but at the end we're all Human(After all).

阅读本文的每个人都是一个Human(我至少希望如此!),我们都有我们的不同之处,我们的相似之处,但最终我们都是Human(毕竟)。

Each Human(known as an instanceor object) has specific characteristics such as the eyes color or the voice which are the fields(you called that variables, but the right name would be fields). But the values are different from an Humaninstanceto another.

每个Human(称为实例对象)都有特定的特征,例如眼睛颜色或作为字段的声音(你称之为变量,但正确的名称应该是字段)。但是值因Human实例而异。

There is also a common knowledge, shared with the humanity, principles like the "Pythagorean theorem". This knowledge is common, it can be interpreted as a static field(I know it's an exaggeration) which means that this knowledge is not only contained in one human but in the humanity.

还有一个共同的知识,与人类共享,诸如“勾股定理”之类的原理。这种知识是普遍的,可以理解为一个静态的领域(我知道是夸大其词),也就是说,这种知识不仅包含在一个人中,还包含在人性中。

Every Humancan do things such as walking, speaking etc. this is known as method, walking is the same for everyone, but when I walk, not everyone walk. The act of walking only affects the Humaninstancewhich does this, but still it's defined by the Humanclass

每个人都Human可以做一些事情,比如走路,说话等等。这就是所谓的方法,走路对每个人都是一样的,但是当我走路时,不是每个人都走路。行走的行为只影响执行此操作的Human实例,但它仍然由定义Human



If you want to get deeper in OOP, Teaching OOP to non-programmers

如果您想更深入地了解 OOP,请向非程序员教授 OOP

回答by mhshams

class:: Man or Woman

班级:: 男人或女人

object:: me, you ...

对象:我,你...

回答by Bill K

If they are learning to program OO have them use BlueJ. They should get the differences after walking through the first tutorial.

如果他们正在学习编程面向对象,让他们使用BlueJ。他们应该在完成第一个教程后了解差异。

You define the classes and when you instantiate them they actually appear at the bottom of the GUI at which point you can call methods on them.

您定义类,当您实例化它们时,它们实际上出现在 GUI 的底部,此时您可以对它们调用方法。

It really helps get the point through better than any analogy you want to try. Even if you nail the analogy, it doesn't translate into code for someone who hasn't learned OO yet (even though for all of us it seems really natural and all these descriptions make great sense.)

与您想尝试的任何类比相比,它确实有助于更好地理解要点。即使你用这个类比,它也不会为那些还没有学习 OO 的人转化为代码(尽管对我们所有人来说这似乎很自然,所有这些描述都很有意义。)

回答by loxxy

Class: Girl

班级:女孩

Object : that girl, this girl, my girl...umm maybe not.

对象:那个女孩,这个女孩,我的女孩……嗯也许不是。

Yea all girls should have the properties of a Girl (class in this case).

是的,所有女孩都应该具有女孩的属性(在这种情况下是类)。

回答by Mike Baranczak

An object is a thing. A class is a categoryof things.

一个对象就是一个东西。类是事物的类别

"Person" is a class; you are an object, an instance of the Person class. Also, the word "you" can be thought of as a variable, since it refers to a Person, but not always the same Person.

“人”是一个类;你是一个对象,一个 Person 类的实例。此外,“你”这个词可以被认为是一个变量,因为它指的是一个人,但并不总是同一个人。