Object Creation and Storage · 对象的创建与存储
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| constructor/kənˈstrʌktə/ | 构造函数 | gòu zào hán shù |
| instantiation/ˌɪnstænʃɪˈeɪʃn/ | 实例化 | shí lì huà |
Making a new object
- To create an object, use the
newkeyword and a constructor 构造函数. Scanner sc = new Scanner(System.in);builds a fresh Scanner object.- Instantiation 实例化 is the act of creating an object from a class.
- The constructor sets up the object's starting state.
造一个新对象
- 要创建对象,使用
new关键字和一个构造函数。 Scanner sc = new Scanner(System.in);造出一个全新的 Scanner 对象。- 实例化是从一个类创建对象的行为。
- 构造函数设置对象的初始状态。
Constructor arguments
- A constructor can take arguments to configure the new object.
new Random(42)seeds a random generator;new String("hi")copies text.- The arguments match the constructor's parameters, by position and type.
- Different constructors (overloaded) let you build the same class different ways.
构造函数的实参
- 构造函数可以接受实参来配置新对象。
new Random(42)给随机生成器设种子;new String("hi")复制文本。- 实参按位置和类型匹配构造函数的形参。
- 不同的(重载的)构造函数让你用不同方式造同一个类。
Storing the reference
newreturns a reference to the object; store it in a variable of the right type.Scanner sc = new Scanner(System.in);—scnow refers to that object.- The object lives in memory;
scis just the label pointing at it. - Lose the reference (no variable holds it) and the object becomes unreachable.
存储引用
new返回对象的一个引用;把它存进一个类型正确的变量。Scanner sc = new Scanner(System.in);——sc现在引用那个对象。- 对象活在内存里;
sc只是指向它的标签。 - 丢失引用(没有变量持有它),对象就变得不可达。
Two variables, one object
- Assigning one object variable to another copies the reference, not the object.
Scanner sc2 = sc;— nowscandsc2point to the same Scanner.- A change through
sc2is visible throughsc— it's one object. - This reference-copy behavior is a defining feature (and trap) of objects.
两个变量,一个对象
- 把一个对象变量赋给另一个是复制引用,而非对象。
Scanner sc2 = sc;——现在sc和sc2指向同一个 Scanner。- 通过
sc2的改变,透过sc也能看到——它是一个对象。 - 这种引用复制的行为是对象的一个决定性特征(也是陷阱)。
new creates ONE object and hands back a reference to it. Assigning that variable to another (b = a;) copies the reference, so both names point to the same object — not a second copy. Contrast primitives (int y = x; copies the value). Forgetting this leads to "I changed b, why did a change too?" surprises.
new 创建一个对象并交还它的一个引用。把那个变量赋给另一个(b = a;)复制的是引用,所以两个名字指向同一个对象——而非第二份副本。对比基本类型(int y = x; 复制的是值)。忘了这一点会导致“我改了 b,为什么 a 也变了?”的意外。
Instantiation and aliasing:
Random r = new Random();→rrefers to a new Random object.Random r2 = r;→r2points at the same object (no new object made).- Both
randr2now share one generator's state.
实例化与别名:
Random r = new Random();→r引用一个新的 Random 对象。Random r2 = r;→r2指向同一个对象(没有造新对象)。- 现在
r和r2共享一个生成器的状态。
Create an object with new plus a constructor (instantiation): new Scanner(System.in). new returns a reference you store in a variable. Assigning one object variable to another copies the reference — both then refer to the same object (unlike primitives, which copy the value).
用 new 加一个构造函数创建对象(实例化):new Scanner(System.in)。new 返回你存进变量的一个引用。把一个对象变量赋给另一个是复制引用——两者随后引用同一个对象(不同于基本类型,那复制的是值)。
new makes one object; = copies the reference · new 造一个对象;= 复制引用
r and r2 share one object; x and y are independent copies. · r 和 r2 共享一个对象;x 和 y 是独立的副本。
Which keyword creates a new object? · 哪个关键字创建一个新对象?
new plus a constructor instantiates an object. · new 加一个构造函数把对象实例化。
The special method called to build a new object is a ___ (one word). · 为创建新对象而调用的特殊方法是 ___(填英文一词 constructor)。
The constructor sets up the new object. · 构造函数设置新对象。
After Random r2 = r; how many Random objects exist? · 在 Random r2 = r; 之后,存在几个 Random 对象?
Assigning copies the reference, not the object — still one object. · 赋值复制引用,而非对象——仍是一个对象。
Assigning one object variable to another (b = a) makes a second, independent object. · 把一个对象变量赋给另一个(b = a)会造出第二个独立的对象。
It copies the reference — both point to the same object. · 它复制引用——两者指向同一个对象。
For primitives, int y = x; copies the value, so x and y are independent. · 对基本类型,int y = x; 复制值,所以 x 和 y 是独立的。
Primitives copy the value; objects copy the reference. · 基本类型复制值;对象复制引用。