Capter 4 Classes and objectes
T1 The differce between procedure riented and object oriented
https://blog.csdn.net/weixin_44135121/article/details/88120309
面向過程:
優點:根據事情的目的分解出過程,然後一步步實施;對於不復雜的事件執行效率快。
缺點:只關注眼前事件的實現。
面向對象:
優點:不僅關注眼前的事件實現,也關注未來可能發生的事件,比如要實現‘小明開車送朋友’。只需要在Person類裏面加入sendFriend方法就可以。具有高度的拓展性(體現出對象的多態性)和複用性(對於類似的人,動作,直接new一個對象傳入不同的參數即可),特點是繼承、封裝、多態。
缺點:跟面向過程正好相反,如果只是單一的功能實現,面向對象的設計思路就顯得過於繁瑣。
T2 Classes and encapsulation
- A class is the template or blueprint from which objects are made.
- When you constract an object from a class,you are said to create an instance of classes.
- All code in Java is inside a class.
There are two types of classes.
- using predefined classes:The java library supplies several thousands classes for interface design,dates and calendars,and network programing.
- Nonetheless,in Java you still have to create your own classes to describe the objects of your application’s problem domain.
Encapsulation(information hiding)
A key concept in working with objects.
https://blog.csdn.net/weixin_37861326/article/details/80418597
most important :information hiding
封裝,是面向對象思想的特徵之一。面向對象共有三個特徵:封裝,繼承,多態。
封裝表現:
1、方法就是一個最基本封裝體。
2、類其實也是一個封裝體。
從以上兩點得出結論,封裝的好處:
1、提高了代碼的複用性。
2、隱藏了實現細節,還要對外提供可以訪問的方式。便於調用者的使用。這是核心之一,也可以理解爲就是封裝的概念。3、提高了安全性。
封裝舉例
機箱:
一臺電腦,它是由CPU、主板、顯卡、內存、硬盤、電源等部件組長,其實我們將這些部件組裝在一起就可以使用電腦了,但是發現這些部件都散落在外面,很容造成不安全因素,於是,使用機箱殼子,把這些部件都裝在裏面,並在機箱殼上留下一些插口等,若不留插口,大家想想會是什麼情況。
總結:機箱其實就是隱藏了辦卡設備的細節,對外提供了插口以及開關等訪問內部細節的方式。私有private
Demo:
描述人。Person
屬性:年齡。
行爲:說話:說出自己的年齡。class Person {
int age;
String name;
public void show() {
System.out.println("age=" + age + ",name" + name);
}
}
public class PersonDemo {
public static void main(String[] args) {
// 創建Person對象
Person p = new Person();
p.age = -20; // 給Person對象賦值
p.name = "張三";
p.show(); // 調用Person的show方法
}
}
上述代碼發現,雖然我們用Java代碼把Person描述清楚了,但有個嚴重的問題,就是Person中的屬性的行爲可以任意訪問和使用。這明顯不符合實際需求。
可是怎麼才能不讓訪問呢?需要使用一個Java中的關鍵字也是一個修飾符 private(私有,權限修飾符)。只要將Person的屬性和行爲私有起來,這樣就無法直接訪問。class Person {
private int age;
private String name;
public void show() {
System.out.println("age=" + age + ",name" + name);
}
}
年齡已被私有,錯誤的值無法賦值,可是正確的值也賦值不了,這樣還是不行,那怎麼辦?按照封裝的原理,隱藏後,還需要提供訪問方式。只要對外提供可以訪問的方法,讓其他程序訪問這些方法。同時在方法中可以對數據進行驗證。
一般對成員屬性的訪問動作:賦值(設置 set),取值(獲取 get),因此對私有的變量訪問的方式可以提供對應的 setXxx或者getXxx的方法。class Person {
// 私有成員變量
private int age;
private String name;
// 對外提供設置成員變量的方法
public void setAge(int a) {
// 由於是設置成員變量的值,這裏可以加入數據的驗證
if (a < 0 || a > 130) {
System.out.println(a + "不符合年齡的數據範圍");
return;
}
age = a;
}
// 對外提供訪問成員變量的方法
public void getAge() {
return age;
}
}
總結:
類中不需要對外提供的內容都私有化,包括屬性和方法。
以後再描述事物,屬性都私有化,並提供setXxx getXxx方法對其進行訪問。
注意:私有僅僅是封裝的體現形式而已。
-
information hiding
-
Provide interface
T3 Objects
There are three characterisitics of objects:
- behavior
- state
- identity
T4 OOP
Where to begin?
In a traditional procedural program, you start the process at the top, with the main function.
How to design?
- Item(商品)
- Order(訂單)
- Shipping address(送貨地址)
- Payment(付款)
- Account(賬戶)
- Items are added to orders.
- Orders are shipped or canceled.
- Payments are applied to orders.
- look for nouns
- correspond to verbs.
- you identify the object that has the major responsibility for carrying it out.
T5 Relationships between classes
-
Dependence (“uses–a”)
-
Aggregation (“has–a”)
-
Inheritance (“is–a”)