👋 主要概念
其他概念
Applications programming interface (API). 应用程序编程接口
Counter 这是一系列构造器(constructor)和实例化方法,以及对每个效果的非正式描述,比如这个对 Counter 类的 API 描述public class counter {
Counter(String id) // Create a counter named "id"
void increment() // increment the counter by one
int tally() // number of increments since creation
String toString() // string representation
}
Client. A client is a program that uses a data type. 客户端:在这里,客户端是指使用预先构造好的抽象数据类型的程序,而不是网络应用中所谓的客户端。
Implementation. An implementation is the code that implements the data type specified in an API 实现:实现 API 中指定的数据类型的代码
A client does not need to know how a data type is implemented in order to be able to use it. client 不需要知道数据类型如何实现,只需要知道如何使用数据类型
Creating objects. 创建对象
Each data-type value is stored in an object. 任何数据对象的值都存储在对象内部(data-type究竟指什么)
To create (or instantiate) an individual object, we invoke a constructor by using the keyword new. 创建一个独立的对象,需要调用构造器和关键词 new
Each time that a client uses new, the system allocates memory space for the object, initializes its value, and returns a reference to the object. 每当 client 使用 new 时,系统会为对象分配内存空间,初始化它的值,然后返回这个对象的引用

构造器和 new 关键词

Using objects. 使用对象
Declarations give us variable names for objects that we can use in code. 声明为我们提供了可以在代码中使用的对象的变量名称
To use a given data type, we: 要使用给定的数据类型,我们应当:
new to invoke a constructor that creates objects of the type 使用一个关键字 new 来调用构造器创建该类型的一个对象例如 Flips.java 是 Counter.java 的一个 client,它 takes a command-line argument T and simulate T coin flips. 似乎是投针实验?
Assignment statements. 赋值声明
An assignment statement with a reference type creates a copy of the reference (and does not create a new object). 和引用类型一起使用的赋值声明能创造引用的一个副本,但不会创造一个新的对象
This situation is known as aliasing: both variables refer to the same object. 简而言之,两个变量都指向同一个对象,称之为别名。这是现代概念:variable - object 构型
Aliasing is a common source of bugs in Java programs, as illustrated by the following example: 不过,别名是 Java 中常见的 bug 来源,看上去对一个别名的操作会导致变量本身被改变了。
Objects as arguments. 将对象作为参数
You can pass objects as arguments to methods. 你可以向方法传递对象作为参数
Java passes a copy of the argument value from the calling program to the method. Java 会传递一份对象的复制,这个复制由调用程序传递给被调用的方法
This arrangement is known as pass by value. 通常,这种传递行为叫作值传递
If you pass a reference to an object of type Counter, Java passes a copy of that reference. 如果传递对 Counter 类型的对象的引用,Java 将传递该引用的副本。
Thus, the method cannot change the original reference (make it point to a different Counter), but it can change the value of the object, for example by using the reference to call increment(). 这样,方法就不会对原本的引用产生影响,但它会改变对象的状态
Objects as return values. 对象作为返回值
You can also use an object as a return value from a method. 你可以使用对象作为方法的返回值
The method might return an object passed to it as an argument, as in FlipsMax.java, or it might create an object and return a reference to it. 该方法可能会返回作为参数传递给它的对象,如 FlipsMax.java 中,或者它可能创建一个对象并返回对它的引用(这里讲的不清楚,实际上这个生命周期有关)
This capability is important because Java methods allow only one return value—using objects enables us to write code that, in effect, returns multiple values. Java 的这个能力相当重要,有了它,我们才能够返回多个值
Arrays are objects. 数组也是对象
In Java, every value of any nonprimitive type is an object. 在 Java 中,所有非基元类型的数据都是对象
In particular, arrays are objects. 数组也是对象
As with strings, there is special language support for certain operations on arrays: declarations, initialization, and indexing. 和 String 类型一样,对数组操作由特殊的语法支持,比如说声明、初始化和索引
As with any other object, when we pass an array to a method or use an array variable on the right hand side of an assignment statement, we are making a copy of the array reference, not a copy of the array. 和其他对象类似,当我们传递一个数组给方法,或者将数组作为右值使用时,我们使用的是数组的副本而非本身
Arrays of objects. 对象的数组
Array entries can be of any type. 数组元素可以是任意类型
When we create an array of objects, we do so in two steps: 如果要创建一个对象数组,需要分两步
Rolls.java simulates rolling a die, using an array of Counter objects to keep track of the number of occurrences of each possible value. Rools.java 模拟掷骰子,使用 Counter 对象数组来跟踪每个可能值的出现次数