Lecture 1:OOP and UML Class DiagramsOOP with Java
OOP 和 UML 類圖 OOP 與 Java
Object-Oriented Programming 物件導向程式設計
Class Hierarchies 類層次結構
Superclass and subclass
超類和子類
Pillars of Object-Oriented Programming 物件導向程式設計的支柱
- Abstraction
– Modelling attributes and behaviors of real objects, in specific contexts
抽象——對真實物件的屬性和行為進行建模,具體來說
-
Encapsulation
– Hiding parts of an object’s states and behaviors from others, and exposing a limited set of interfaces
– public, private, and protected
– Interfaces and abstract classes
封裝
— 隱藏物件的部分內容和來自他人的狀態和行為,並公開一組有限的介面
– 公共、私有和受保護
– 介面和抽象類 -
Inheritance
– Main benefit: code reuse
繼承
— 主要好處:程式碼重用 -
Polymorphism
– Performing an action in many forms
– A mechanism for detecting the real class of an object and call its implementation
多型性
多種形式執行一個動作
一種檢測物件的真實類並呼叫其實現方法的機制
OOP with Java: Declaring Classes and Creating Objects Java 的 OOP:宣告類和建立物件
-
Class declaration
類宣告
-
Creating objects
建立物件- Declaration, instantiation, initialization
宣告、例項化、初始化
- The reference returned by the new operator does not have to be assigned to a variable
new 運算子返回的引用不必分配給變數
- Declaration, instantiation, initialization
OOP with Java: Access Control Java 的 OOP:訪問控制
- At the top level
在頂層
– public, or package-private (no explicit modifier)
公共或包私有(無顯式修飾符) - At the member level
在成員級別
– public, private, protected, or package-private (no explicit modifier)
public、private、protected 或 package-private(無顯式修飾符)
OOP with Java: Inheritance Java 中的 OOP:繼承
-
Classes can be derived from other classes, inheriting fields and methods
類可以從其他類派生,繼承欄位和方法 -
Definitions
定義
– Subclass (derived class/extended class/child class)
– Superclass (base class/parent class)
– 子類(派生類/擴充套件類/子類)
– 超類(基類/父類) -
Every class has one and only one direct superclass (single inheritance)
每個類都有一個且僅有一個直接超類(單繼承)
– Excepting Object, which has no superclass
除了Object,它沒有超類 -
A subclass inherits all the members (fields, methods, and nested classes) from its superclass
子類繼承其超類的所有成員(欄位、方法和巢狀類)
OOP with Java: What You Can Do in a Subclass Java 的 OOP:在子類中可以做什麼
Use the inherited members as is, replace them, hide them, or supplement them
按原樣使用繼承的成員、替換它們、隱藏它們或補充它們
- Declare a field in the subclass with the same name as the one in the superclass, thus hiding it (NOT recommended)
– 在子類中宣告一個與超類中的欄位同名的欄位,從而隱藏它(不推薦) - Write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it
– 在子類中編寫一個新的例項方法,其簽名與超類中的欄位相同,從而覆蓋它 - Write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it
–在子類中編寫一個新的靜態方法,該方法與超類中的靜態方法具有相同的簽名,從而隱藏它 - Write a subclass constructor that invokes the constructor of the superclass
– 編寫一個呼叫超類建構函式的子類建構函式
How about private members in a superclass?
OOP with Java: Abstract and Final Methods/Classes Java 的 OOP:抽象和最終方法/類
- An abstract class is a class declared abstract: it may or may not include abstract methods
抽象類是宣告為抽象的類:它可能包含也可能不包含抽象方法 - An abstract method is a method declared without an implementation
抽象方法是宣告但沒有實現的方法 - Final methods and classes
最終方法和類
Methods called from constructors should generally be declared final
– 從建構函式呼叫的方法通常應宣告為final
OOP with Java: Interfaces Java 的 OOP:介面
- Interfaces are contracts
介面是契約 - A reference type, containing only constants, method signatures,default methods, static methods, and nested types
引用型別,僅包含常量、方法簽名、預設方法、靜態方法和巢狀型別 - Cannot be instantiated
無法例項化
– They can only be implemented by classes or extended by other interfaces
– 它們只能由類實現或由其他介面擴充套件 - Consisting of modifiers, keyword, interface name, a comma-separated list of parent interfaces (if any), and the interface body
由修飾符、關鍵字、介面名稱、以逗號分隔的父介面列表(如果有)和介面主體組成 - Interface body can contain abstract methods, default methods,and static methods
介面體可以包含抽象方法、預設方法和靜態方法
OOP with Java: Implementing and Using Interfaces 使用 Java 進行 OOP:實現和使用介面
- Include an implements clause in the class declaration
在類宣告中包含 implements 子句
– Your class can implement more than one interface
– 你的類可以實現多個介面 - If you define a reference variable whose type is an interface,any object you assign to it must be an instance of a class that implements the interface
如果定義型別為介面的引用變數,則分配給它的任何物件都必須是實現該介面的類的例項
OOP with Java: Abstract Classes vs. Interfaces Java 中的 OOP:抽象類與介面
-
Consider using abstract classes when
考慮使用抽象類
– You want to share code among several closely related classes
– 您希望在幾個緊密相關的類之間共享程式碼
– You expect that classes extending the abstract class have many common methods or fields, or require access modifiers other than public
– 您希望擴充套件抽象類的類具有許多通用方法或欄位,或者需要除 public 之外的訪問修飾符
– You want to declare non-static or non-final fields
— 您想要宣告非靜態或非最終欄位 -
Consider using interfaces when
考慮使用介面
– You expect that unrelated classes would implement your interface
– 您希望不相關的類實現您的介面
– You want to specify the behavior of a particular data type, but not concerned about who implements its behavior
– 您想要指定特定資料型別的行為,但不關心誰實現其行為
– You want to take advantage of multiple inheritance
– 您想要利用多重繼承