面向对象的编程(Object-Oriented Programming (OOP))
考点:小的知识点,多看例子,都是纠错程序
基础概念Basic concepts: object, class, attribute, and method
类成员、class methods类方法、instance methods实例方法、实例成员变量
共享栈与新建栈的区别
所有和类中的对象相关的数据(即使是基本数据类型)都存在堆里;方法里的基本数据类型存在栈里–考
下图不考
接口Interface
– Interface和Class: 定义和实现ADT;也可以不需要接口直接使用类作为ADT,既 有ADT定义也有ADT实现;实际中更倾向于使用接口来定义变量
– 接口之间可以继承与扩展
– 一个类可以实现多个接口(从而具备了多个接口中的方法)现在支持接口中的方法
– 一个接口可以有多种实现类
!java不允许实现多重继承的类,但是接口可以实现多重继承
Set :接口
继承:B中有的操作A中一定都要有,A包含B----程序员自己注意,没法检查
多态:1、重写2、重载3、泛型
设计一个好的类Designing good classes
Advantages of immutable classes
immutable对象可以多个引用中share(反正改变不了其它的引用值)
§ Simplicity
§ Inherently Thread-Safe
§ Can be shared freely
§ No need for defensive copies
§ Excellent building blocks
How to write an immutable class
§ Don’t provide any mutators
§ Ensure that no methods may be overridden
§ Make all fields final
§ Make all fields private
§ Ensure security of any mutable components (avoid rep exposure)
§ Implement toString(), hashCode(), clone(), equals(), etc.
When to make classes immutable什么时候要immutable类型的类
§ Always, unless there’s a good reason, not to经常会改变值的类就设置成mutable
§ Always make small “value classes” immutable!
– Examples: Color, PhoneNumber, Unit
– Date and Point were mistakes!
– Experts often use long instead of Date
When to make classes mutable什么时候要mutable类型的类
§ Class represents entity whose state changes
– Real-world - BankAccount, TrafficLight
– Abstract - Iterator, Matcher, Collection
– Process classes - Thread, Timer
§ If class must be mutable, minimize mutability
– Constructors should fully initialize instance
– Avoid reinitialize methods
Summary
§ Criteria of Object-Orientation
§ Basic concepts: object, class, attribute, method, and interface
§ Distinct features of OOP
Encapsulation and information hiding
Inheritance and overriding
Polymorphism, subtyping and overloading
Static and Dynamic dispatch
§ Some important Object methods in Java
§ To write an immutable class
§ History of OOP
§ Summary
tant Object methods in Java
§ To write an immutable class
§ History of OOP
§ Summary