^-^
There comes a time when you realize that everything is a dream and only those things can be preserved by writting have any possibility to be real.
AlgorithmLog
  1. 1.2.1 Object-oriented programming
  2. 1.2.2 Using abstract data types
  3. 1.2.3 Examples of abstract data types
  4. 1.2.4 Implementing abstract data types
  5. 1.2.5 Designing abstract data types

1.2.1 Object-oriented programming

  1. Programming in Java is largely based on building data types. Java 编程很大程度上依赖于构造数据类型
  2. This style of programming is known as object-oriented programming, as it revolves around the concept of an object, an entity that holds a data type value. 这种编程风格被称为面向对象编程,因为它围绕着“对象”这个概念。对象是指一种封装了数据类型的实体
  3. With Java’s primitive types we are largely confined to programs that operate on numbers, 使用 Java 的基元类型进行编程,我们在很大程度上会被局限在进行一些数字操作
    1. but with reference types we can write programs that operate on strings, pictures, sounds, or any of hundreds of other abstractions 如果使用引用类型,我们就能够写出操作字符串、图片、声音及其他数以百计的抽象对象的程序
    2. that are available in Java’s standard libraries or on our booksite. 这些抽象对象存放在 Java 标准库中
  4. Even more significant than libraries of predefined data types is that the range of data types available in Java programming is open-ended, because you can define your own data types. 比预先定义在标准库中的数据类型更重要的是,Java 编程是开放的,你可以创造你想要的数据类型

👋 主要概念

  • 数据类型:数据类型是一组值和定义在值上的一组操作的统称。
  • 抽象数据类型:抽象数据类型是其内部表示形式对客户端隐藏的数据类型。
  • 对象
    • An object is an entity that can take on a data-type value. 对象是一个???的数据实体
    • Objects are characterized by three essential properties: 对象的特征基于三个必要性质
    • The state of an object is a value from its data type; 实例的状态是其数据类型中的值
    • the identity of an object distinguishes one object from another; 实例的标识符区分了该实例和另一个实例
    • the behavior of an object is the effect of data-type operations. 实例的行为是数据类型操作的结果
    • In Java, a reference is a mechanism for accessing an object. 在 Java 中,引用是访问对象的一种方式

其他概念

  • Applications programming interface (API). 应用程序编程接口

    • To specify the behavior of an abstract data type, we use an application programming interface (API) 为了定义抽象数据类型的行为,我们使用一套应用程序编程接口
    • which is a list of constructors and instance methods (operations), with an informal description of the effect of each, as in this API for : 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 中指定的数据类型的代码

1.2.2 Using abstract data types

  1. A client does not need to know how a data type is implemented in order to be able to use it. client 不需要知道数据类型如何实现,只需要知道如何使用数据类型

  2. Creating objects. 创建对象

  3. Each data-type value is stored in an object. 任何数据对象的值都存储在对象内部(data-type究竟指什么)

  4. To create (or instantiate) an individual object, we invoke a constructor by using the keyword new. 创建一个独立的对象,需要调用构造器和关键词 new

  5. 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 关键词

构造器和 new 关键词

  1. Invoking instance methods. 调用实例方法
  2. The purpose of an instance method is to operate on data-type values. 实例方法的用途是对数据类型值进行操作
  3. Instance methods have all of the properties of static methods: arguments are passed by value, method names can be overloaded, they may have a return value, and they may cause side effects. 实例方法具有静态方法的所有性质:参数按值传递,方法名称可以重载,它们可能具有返回值,并且可能附带 side effects
  4. They have an additional property that characterizes them: each invocation is associated with an object.它们有一个额外的属性来表征它们:每个调用都与一个对象相关联,所以被称为(实例方法

instance-method.png

  1. Using objects. 使用对象

  2. Declarations give us variable names for objects that we can use in code. 声明为我们提供了可以在代码中使用的对象的变量名称

  3. To use a given data type, we: 要使用给定的数据类型,我们应当:

    1. Declare variables of the type, for use in referring to objects 声明变量的类型,用于引用对象
    2. Use the keyword new to invoke a constructor that creates objects of the type 使用一个关键字 new 来调用构造器创建该类型的一个对象
    3. Use the object name to invoke instance methods, either as statements or within expressions 使用对象名称调用实例方法,不论是在语句还是在表达式中
  4. 例如 Flips.java 是 Counter.java 的一个 client,它 takes a command-line argument T and simulate T coin flips. 似乎是投针实验?

  5. Assignment statements. 赋值声明

  6. An assignment statement with a reference type creates a copy of the reference (and does not create a new object). 和引用类型一起使用的赋值声明能创造引用的一个副本,但不会创造一个新的对象

  7. This situation is known as aliasing: both variables refer to the same object. 简而言之,两个变量都指向同一个对象,称之为别名。这是现代概念:variable - object 构型

  8. Aliasing is a common source of bugs in Java programs, as illustrated by the following example: 不过,别名是 Java 中常见的 bug 来源,看上去对一个别名的操作会导致变量本身被改变了。

  9. Objects as arguments. 将对象作为参数

  10. You can pass objects as arguments to methods. 你可以向方法传递对象作为参数

  11. Java passes a copy of the argument value from the calling program to the method. Java 会传递一份对象的复制,这个复制由调用程序传递给被调用的方法

  12. This arrangement is known as pass by value. 通常,这种传递行为叫作值传递

  13. If you pass a reference to an object of type Counter, Java passes a copy of that reference. 如果传递对 Counter 类型的对象的引用,Java 将传递该引用的副本。

  14. 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(). 这样,方法就不会对原本的引用产生影响,但它会改变对象的状态

  15. Objects as return values. 对象作为返回值

  16. You can also use an object as a return value from a method. 你可以使用对象作为方法的返回值

  17. 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 中,或者它可能创建一个对象并返回对它的引用(这里讲的不清楚,实际上这个生命周期有关)

  18. 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 的这个能力相当重要,有了它,我们才能够返回多个值

  19. Arrays are objects. 数组也是对象

  20. In Java, every value of any nonprimitive type is an object. 在 Java 中,所有非基元类型的数据都是对象

  21. In particular, arrays are objects. 数组也是对象

  22. As with strings, there is special language support for certain operations on arrays: declarations, initialization, and indexing. 和 String 类型一样,对数组操作由特殊的语法支持,比如说声明、初始化和索引

  23. 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. 和其他对象类似,当我们传递一个数组给方法,或者将数组作为右值使用时,我们使用的是数组的副本而非本身

  24. Arrays of objects. 对象的数组

  25. Array entries can be of any type. 数组元素可以是任意类型

  26. When we create an array of objects, we do so in two steps: 如果要创建一个对象数组,需要分两步

    1. create the array, using the bracket syntax for array constructors; 创造一个数组,使用括号语法来调用数组构造器
    2. create each object in the array, using a standard constructor for each. 创建数组中的每一个对象,这需要对每一个元素都调用标准的构造器
  27. 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 对象数组来跟踪每个可能值的出现次数

1.2.3 Examples of abstract data types

1.2.4 Implementing abstract data types

1.2.5 Designing abstract data types