Objects: Instances of Classes · 对象:类的实例
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| class/klæs/ | 类 | lèi |
| object/ˈɒbdʒekt/ | 对象 | duì xiàng |
| blueprint/ˈbluːprɪnt/ | 蓝图 | lán tú |
| instance/ˈɪnstəns/ | 实例 | shí lì |
Classes and objects
- A class 类 is a blueprint 蓝图 — it describes what a kind of thing has and can do.
- An object 对象 is a concrete instance 实例 built from that blueprint.
- One
Stringclass; many String objects ("cat","dog","Ada"). - The class defines the pattern; each object is one filled-in copy.
类与对象
- 类是一张蓝图——它描述某一类东西拥有什么、能做什么。
- 对象是从那张蓝图造出的一个具体实例。
- 一个
String类;许多 String 对象("cat"、"dog"、"Ada")。 - 类定义模式;每个对象是一份被填好的副本。
Blueprint vs. instance
- Think cookie cutter (class) vs. cookies (objects).
- The cutter shapes them; each cookie is a separate, real thing.
- Every object has its own data, even though they share the class's design.
- Changing one object doesn't change the others.
蓝图对实例
- 想成饼干模具(类)对饼干(对象)。
- 模具塑形它们;每块饼干是一个独立、真实的东西。
- 每个对象都有它自己的数据,尽管它们共享类的设计。
- 改变一个对象不会改变其他对象。
State and behavior
- An object bundles state (its data — instance variables) and behavior (its methods).
- A
Stringobject's state is its characters; its behavior includeslength(),substring(). - You interact with an object by calling its methods.
- This bundling of data + methods is the heart of object-oriented programming.
状态与行为
- 一个对象把状态(它的数据——实例变量)和行为(它的方法)捆在一起。
- 一个
String对象的状态是它的字符;它的行为包括length()、substring()。 - 你通过调用对象的方法来与它交互。
- 这种数据 + 方法的捆绑是面向对象编程的核心。
Class or object? · 类还是对象?
A class is the blueprint; an object is a specific instance. Sort each. · 类是蓝图,对象是特定实例。对每一项进行分类。
Objects have reference types
- An object is stored via a reference — the variable holds its address, not the object.
String s = "hi";—srefers to a String object somewhere in memory.- Two references can point to the same object.
- Reference types behave differently from primitives — a theme of the next lessons.
对象是引用类型
- 对象通过一个引用来存储——变量保存它的地址,而非对象本身。
String s = "hi";——s引用内存中某处的一个 String 对象。- 两个引用可以指向同一个对象。
- 引用类型的行为与基本类型不同——这是后面几课的主题。
A class is a type/blueprint; an object is one instance of it — don't conflate them. String is the class; "cat" is an object. Each object carries its own state, so two String objects are independent. And an object variable holds a reference (an address), not the object's bytes directly — this matters when you copy or compare them later.
类是类型/蓝图;对象是它的一个实例——别把二者混为一谈。String 是类;"cat" 是对象。每个对象带着它自己的状态,所以两个 String 对象是独立的。而对象变量保存一个引用(地址),而非对象的字节本身——这在后面复制或比较它们时很重要。
The class/object relationship:
- Class:
String(the blueprint — every String can dolength(),substring(), …). - Objects:
"cat","dog"— two separate instances, each with its own characters. "cat".length()is3;"dog".length()is3— same behavior, independent state.
类/对象的关系:
- 类:
String(蓝图——每个 String 都能做length()、substring()……)。 - 对象:
"cat"、"dog"——两个独立的实例,各有自己的字符。 "cat".length()是3;"dog".length()是3——相同行为,独立状态。
A class is a blueprint describing state (instance variables) and behavior (methods); an object is a concrete instance of a class. Many objects can share one class's design while each holds its own state. Objects are reference types — a variable stores the object's address, not the object itself.
类是描述状态(实例变量)和行为(方法)的蓝图;对象是一个类的具体实例。许多对象可以共享一个类的设计,而各自持有它自己的状态。对象是引用类型——变量存储对象的地址,而非对象本身。
The relationship between a class and an object is like... · 类和对象之间的关系就像...
A class is the blueprint; an object is an instance built from it. · 类是蓝图;对象是由其构建的实例。
A specific object built from a class is called an ___ of that class (one word). · 由类构建的特定对象被称为该类的___(一个词)。
Objects are instances of a class. · 对象是类的实例。
Two objects of the same class each have their own independent state. · 同一类的两个对象各自拥有独立的独立状态。
They share the design but hold separate data. · 它们共享设计但持有独立的数据。
An object variable actually stores... · 对象变量实际上存储的是...
Objects are reference types — the variable holds an address. · 对象是引用类型——变量持有地址。
In 'String s = "cat";', String is the ___ and "cat" is the . · 在'String s = "cat";'中,String是,"cat"是___。
String is the class (blueprint); "cat" is an object (instance). · String是类(蓝图);"cat"是对象(实例)。