ADT and OOP 复习总结(一)

ADT and OOP 复习总结(一)

软件构造的理论基础——ADT(抽象数据类型)

软件构造的技术基础——OOP(面向对象编程)

章节目标

1. Get to know basic knowledge about data type, and static and 
dynamic type checking in programming language, especially in Java. 
——静态/动态类型检查
2. Understand mutability and mutable objects ——可变/不变的数据类型
3. Identify aliasing and understand the dangers of mutability 
——可变数据类型的危险性
4. Use immutability to improve correctness, clarity and changeability 
——不变数据类型的优越性
5. Use snapshot diagram to demonstrate the state of specific time 
during a program’s execution. ——用Snapshot图理解数据类型
6. Use Arrays, Collections and Enum to deal with complex data types 
——用集合类表达复杂数据类型
7. Know the harm of Null references and avoid it
——了解空引用的危害,并避免使用它

1.Types in java

基本数据类型
– int (for integers like 5 and -200, but limited to the range ± 2^31, or 
roughly ± 2 billion)
– long (for larger integers up to ± 2^63)
– boolean (for true or false)
– double (for floating-point numbers, which represent a subset of the real 
numbers)
– char (for single characters like 'A' and '$' )
对象引用数据类型
– String represents a sequence of characters.
– BigInteger represents an integer of arbitrary size.

根据Java约定,基础数据类型为小写,而对象类型以大写字母开头。

基础数据类型 对象引用类型
int, long, byte, short, char,float, double, boolean Classes, interfaces, arrays, enums,annotations
只有值,没有ID (与其他值无法区分) 既有ID,也有值
Immutable 不可变的 Some mutable, some not 可变/不可变
On stack, exist only when in use 在栈中分配内存 On heap, garbage collected 在堆中分配内存
无法实现表达的统一 Unity of expression with generics
Dirt cheap 代价低 More costly代价昂贵
将基本数据类型封装为对象类型

Boolean, Integer, Short, Long, Character, Float, Double

通常是在定义容器类型的时候使用它们(容器类型操作的元素要求是

对象类型,所以需要对基本数据类型进行包装,转换为对象类型),一般情况下,尽量避免使用(会降低性能)

操作符
 - Assignment: = 
 – Addition: + 
 – Subtraction: - 
 – Multiplication: * 
 – Division: /

java不支持操作符重载,虽然java里的String可以使用"+"进行连接,貌似进行了运算符重载,但其实不是,这是JVM进行的处理,JVM编译的时候会构建一个StringBuilder,然后调用了append方法将字符串连接起来,这和C++的运算符重载有本质的不同

2.静态与动态数据类型检查

java是静态类型语言,类型检查在运行前就进行了,在像Python这样的动态类型语言中,这种检查会被推迟到运行时(当程序运行时)

检查类型
静态检查 动态检查 不检查
在程序运行之前就会自动发现这个错误 执行代码时会自动发现错误 该语言根本不能帮助您查找错误。你必须自己注意,否则就会得到错误的答案
可在编译阶段发现错误,避免了将错误带入到运行阶段,可提高程序正确性/健壮性
静态检查往往是关于类型的,这些错误与变量所具有的特定值无关。 关于“值”的检查
检查类容:Syntax errors ,Wrong names,Wrong number of arguments ,Wrong argument types,Wrong return types ,Illegal argument values,Unrepresentable return values ,Out-of-range indexes,Calling a method on a null object reference

3.可变性和不可变性

改变一个变量:将该变量指向另一个值的存储空间。

改变一个变量的值:将该变量当前指向的值的存储空间中写入一个新的值。

重要设计原则:不变性,变化是“罪恶”,但程序不能没有变化尽可能避免变化,以避免副作用

不变数据类型:一旦被创建,其值不能改变

如果是引用类型,也可以是不变的:一旦确定其指向的对象,不能再被改变指向其他对象

4.快照图

  1. 用于描述程序运行时的内部状态
  2. 便于程序员之间的交流
  3. 便于刻画各类变量随时间变化
  4. 便于解释设计思路

具体看课件上的图,怎么画解释的很清楚,这里不多赘述。

5.复杂数据类型

int[] a = new int[100];
– indexing: a[2]
– assignment: a[2]=0
– length: a.length
List<Integer> list = new ArrayList<Integer>();
– indexing: list.get(2)
– assignment: list.set(2, 0)
– length: list.size()
Note 1: List is an interface.列表是一个接口
Note 2: members in a Listmust be an object.列表的成员必须是对象数据类型    
集合是包含零或多个唯一对象的无序集合。

– s1.contains(e) test if the set contains an element
– s1.containsAll(s2)test whether s1 ⊇ s2
– s1.removeAll(s2) remove s2 from s1

Set is an abstract interface 集合是一个抽象接口
A Map is similar to a dictionary (key-value)
– map.put(key, val) add the mapping key → val
– map.get(key) get the value for a key
– map.containsKey(key) test whether the map has a key
– map.remove(key) delete a mapping
 Map is an abstract interface

Implementations of List , Set , and Map : 列表、集合和图的实现

– List: ArrayList and LinkedList

– Set: HashSet

– Map: HashMap

List<String> firstNames = new ArrayList<String>();
List<String> lastNames = new LinkedList<String>();
List<String> firstNames = new ArrayList<>();
List<String> lastNames = new LinkedList<>();
Set<Integer> numbers = new HashSet<>();
Map<String,Turtle> turtles = new HashMap<>();

迭代器

迭代器是一个对象,它遍历一组元素并逐个返回元素 ,for(…:…)形式的遍历,调用的是被遍历对象所实现的迭代器

迭代器(iterator)的两个方法:

– next() returns the next element in the collection — this is a mutator method!

– hasNext() tests whether the iterator has reached the end of the collection

用法如下:

List<String> lst = new ArrayList<String>();
Iterator iter = lst.iterator();
while(iter.hasNext()){
    String str = iter.next();
    System.out.println(str);
}

空引用的危害

  • 在Java中,对对象和数组的引用也可以接受特殊值Null,这意味着该引用不指向对象。空值是Java类型系统中一个不幸的漏洞。

  • 基本数据类型不能为空,编译器将拒绝这样的带有静态错误的尝试:

    int size = null; //illegal

  • 无法调用任何方法或使用具有这些引用之一的任何字段(抛出Null指针异常)

  • null与空字符串“”或空数组不相同

  • 在参数和返回值中隐式地不允许使用空值。如果一个方法允许一个参数的空值,它应该显式地声明它,或者如果它可能因此返回一个空值,它应该显式地声明它。但这些通常都不是什么好主意。避免设置为空。

上一篇:java面向对象(OOP)


下一篇:1.面向对象 oop