程式設計programming
13. class與 object 有何不同,說明它們的用法、使用時機及注意事項,並請以程式舉例說明。
答:
1.class與 object 有何不同?
ANS:
class(類別)
1.class(類別):是紅龜糕板、模板(template)、藍圖(Blueprint)、可參考的文件、可說是一種型態,但沒有實體.,他沒有實體 (Instance) 的概念,屬靜態的。
2.類別就是據以產生物件的藍圖,它描述物件內所有的變數及函數。更明確地說,類別才是物件導向程式設計最基本的單元。
3.實際的物件導向程式設計中(例如Java),我們必須先定義類別,然後才能夠透過類別宣告各個屬於該類別下的物件,接著再設定物件的屬性來代表該物件某方面的特性,並使用物件的方法來操作物件。
4.在純物件導向的程式中,程式碼是以類別為基礎,物件只是類別的實體。從由下而上的角度來看,類別是許多同類物件的集合
object(物件)
1.object(物件):是根據它的紅龜糕板、模板(template)、藍圖所產生的紅龜糕,是一個看的到、摸的到的實體,屬於動態的,狀態會隨時改變,但架構與行為不會改變。
2.軟體上的物件是一組變數及其相關函數(或稱方法 method)所組成的軟體單位。變數是記錄物件的狀態(或稱屬性 properties),函數是描述物件的行為(或稱功能 functions)。
3.相同類別的不同物件(同類物件),代表擁有相同靜態屬性(資料變數)及動態方法(函式 )的物件,有著相同的函數,所以一個執行函數的訊息,要指明是那個物件的那個函數。
4.物件的使用必須先有物件的存在,而要產生物件必須先有描述(定義)物件的藍圖─即類別,所以我們必須先談類別的設計,再談物件的使用。
基本上,類別只用來決定物件形成時的樣子,當物件形成時,物件就變成一個記憶體中的空間,記載著物件活動時暫存的資料與狀態,並且當有類別存在時有能力透過方法(Method)執行一些動作。
2.以程式舉例說明它們的用法、使用時機及注意事項
ANS1:程式舉例如下所示:
class Human {
int height;//object-level variable
static int weight;//class-level variable
String name;
void run(){
int a;
}
void eat(){ }
}
public class HumanClient {
public static void main(String[] args) {//class-level method
//Human.height = 170;
Human john = new Human();
john.height = 170;
Human.weight = 50;
Human mary = new Human();
System.out.println("bp1: " + john.weight);
mary.weight = 60;
System.out.println("bp2: " + john.weight);
Human.weight = 70;
System.out.println("bp3: " + john.weight);
}
}
程式說明:
Human john = new Human();
Human: 表class類別
john:表object物件(物件本身要記憶體/存在記憶體);john是Human的reference
=:表reference指涉
new: 表產生物件(產生物件就是new,new是向作業系統要記憶體)
Human():表constructor建構子
ANS2:用法、使用時機
1.public 與private 是界定資料使用範圍。public 是給外部使用,搭配main給外部呼叫用。class是內部的class就不用public。
2.成員變數member variable當new一個object物件時,會自動用預設值把它清掉。
3.new是產生object物件,new在程式上是產生object物件;在記憶體的角度是跟記憶體要記憶體,長的樣子就是Human的樣子。若沒有new,則沒向記憶體到位置。
4.這個class有個方法叫main,這個main是static,代表是class-level method。為何要用static,因在DOS磁碟作業系統中,無法用new這個字,只能呼叫這個class,因一呼叫就直接進入main。
5.Java 寫程式時是class Human,class在執行時會載入class loading,執行的東西叫class loader載入器,在此case案例有佔記憶體,叫Human。使用static時是class變數,記憶體還是有class空間。
ANS3:注意事項
1.成員變數member variable用預設值(整數是0;浮點數是0.0;字串是空null;物件也是空null;布林是false)
2.成員變數member variable當new一個object物件時,會自動用預設值把它清掉。所有的運算在記憶體內。
3.object variable 只能由object使用。
4.class variable 只要有class就可使用。
5.變數與方法一樣,前面皆可加static,若沒有加static,則需有物件。
6.不要用物件去改static。最好是從class去改static。用static方法去改static。class調class變數 (匯率調整由央行調整較佳,若由其他銀行,則會影響他行匯率)
Ref1: 老師講議/上課資料
*****************************************************************************
Ref2: https://blog.miniasp.com/post/2009/08/27/OOP-Basis-What-is-class-and-object.aspx
類別(Class) 與 物件(Object) 是個一體兩面的東西:
簡單解釋:
類別算是一個藍圖、一個範本、一個可參考的文件,他沒有 實體 (Instance) 的概念,屬靜態的。
物件是一個看的到、摸的到的實體,屬於動態的,狀態會隨時改變,但架構與行為不會改變。
比喻一:建築物
類別:設計藍圖
物件:實際蓋好的房子
兩者關係:設計藍圖(類別)決定房子應該怎麼蓋,決定幾台電梯、幾間房間、走道如何設計。實際蓋好的房子(物件)是照著設計藍圖所蓋出來的房子,人只能照設計藍圖的設計使用這間房子。
比喻二:蓋世武功
類別:武林密笈
物件:修練武林密笈而成的武林高手
兩者關係:武林密笈(類別)記載許多各種攻擊與回應的方式,讓武林高手(物件)知道遭遇到什麼攻擊時要用什麼招式回應。
程式設計:每執行到我們用 new 運算子時,等同於將物件產生,也等同於成功得到武林密笈可以開始練功,或是在「建構子」的時候就已經賦予你基本功力。
基本上,類別只用來決定物件形成時的樣子,當物件形成時,物件就變成一個記憶體中的空間,記載著物件活動時暫存的資料與狀態,並且當有類別存在時有能力透過方法(Method)執行一些動作。
*****************************************************************************
Ref3: http://www.programmer-club.com.tw/ShowSameTitleN/oop/356.html
類別(Class):可以說是一種型態,但沒有實體.
物件(Object):是用Class實作出來的東西.
舉個例子來說,有一段程式如下:
class Rectangle
{
int Height;
int Width;
};
//註:宣告了一個名為Rectangle的"類別",但是並沒有實體
void main()
{
Rectangle RecA , RecB ;
}
//宣告 RecA,RecB 為Rectangle型態的"物件",RecA及RecB皆有實體(有佔記憶體空間)
再補充另一個例子說明:
桌上有3顆大小不一的蘋果,它們的名稱分別為 AppleA , AppleB , AppleC . 我們知道這3顆蘋果:AppleA , AppleB , AppleC 都是一顆蘋果 (癈話....=.=") .
那麼我們可以說:
蘋果是類別 ; 而AppleA , AppleB , AppleC是物件.
class就好像是蓋房子的藍圖一樣 ,可以重複使用它蓋很多棟房子 ,
而object則好像是用藍圖蓋出的房子一樣 ,是class的"實作"....
*****************************************************************************
Ref4: https://www.tutorialspoint.com/java/java_object_classes.htm
· Object − Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of a class.
· Class − A class can be defined as a template/blueprint that describes the behavior/state that the object of its type support.
· Objects in Java
· Let us now look deep into what are objects. If we consider the real-world, we can find many objects around us, cars, dogs, humans, etc. All these objects have a state and a behavior.
· If we consider a dog, then its state is - name, breed, color, and the behavior is - barking, wagging the tail, running.
· If you compare the software object with a real-world object, they have very similar characteristics.
· Software objects also have a state and a behavior. A software object's state is stored in fields and behavior is shown via methods.
· So in software development, methods operate on the internal state of an object and the object-to-object communication is done via methods.
· Classes in Java
· A class is a blueprint from which individual objects are created.
· Following is a sample of a class.
public class Dog {
String breed;
int age;
String color;
void barking() {
}
void hungry() {
}
void sleeping() {
}
}
A class can contain any of the following variable types.
· Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.
· Instance variables − Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.
· Class variables − Class variables are variables declared within a class, outside any method, with the static keyword.
A class can have any number of methods to access the value of various kinds of methods. In the above example, barking(), hungry() and sleeping() are methods.
*****************************************************************************
首先,先看底下兩張圖:
這兩者的差異是什麼?一個是靜態的虛擬藍圖,一個是動態的實體,右圖的汽車可以有很多部參考唯一由左圖的汽車設計圖所製而成,而每一部實體的汽車都會有自己的狀態.或者,我們成另外一組圖:
一個Class就好比一個人的DNA(用來定義object),一個Object就好比一個人的真實肉體(不過用人來比喻的話物件導向的世界,我們必須考慮複製人的情況成立XD)
不過要注意的是,我們在考慮某個Object的時候要注意到情境,如果你是在寫程式未編譯之前的時候討論或下指令,比如 ExampleClass A=new ExampleClass(); A.fuction();這裡的Object強調的是假設這個類別已經被實體化之後要做fuction()這個方法,如果我們在程式被編譯之後討論,真正在電腦裡面運作的東西叫做Instance實例而假設會被實體化的東西記做Object,看不太懂的話可以看下面的圖表:
*****************************************************************************
Ref6: http://www.gotop.com.tw/epaper/e0715/AEL004300%E5%85%A7%E5%AE%B9%E8%A9%A6%E9%96%B1.pdf
何謂物件 (Object)?
我們生活四周存在著許多的“物件”,如有形物件的汽車、微波爐等,和無形物件的銀行帳戶、學生成績等。不管那種物件都可用狀態(State)及行為(Behavior) 兩種事物來描述,如汽車的車速、檔數是狀態的描述,加速(減速)、換檔是行為的描述。銀行帳戶的餘額、利息是狀態的描述,存款(提款)、結清是行為的描述。軟體上的物件是一組變數及其相關函數(或稱方法method)所組成的軟體單位。變數是記錄物件的狀態(或稱屬性 properties),函數是描述物件的行為(或稱 功能 functions)。
如下圖是一個描述汽車的軟體物件。它有三個記錄汽車屬性的變數及三個相關的函數。
何謂類別 (Class)?
汽車是根據汽車藍圖(Blueprint)製造出來,物件也是根據它的藍圖所產生,而類別就是據以產生物件的藍圖,它描述物件內所有的變數及函數。如前述汽車的類別:
一張汽車藍圖可以製造出許多同款的汽車,一個類別可以產生任何數量的物件。
類別又如同製作餅乾的壓模,一個壓模可以製作出許多相同形狀的餅乾,類別也能產生許多的物件。
相同類別的不同物件,有著相同的函數,所以一個執行函數的訊息,要指明是那個物件的那個函數。如要執行 changeGear,要指明那部車的changeGear 。
另外變數也是各有一套,分別記錄物件各自的狀態。如不同車子的使用狀態未必相同,你的車開每小時50 公里,我的車開每小時40 公里,所以一個存取變數資料,也要指明是那個物件的那個變數。
類別的設計 (Class Implement)
物件的使用必須先有物件的存在,而要產生物件必須先有描述(定義)物件的藍圖─即類別,所以我們必須先談類別的設計,再談物件的使用。
類別的定義 (The Class Definition)
將前面的汽車藍圖用程式指令來定義,可寫成:
public class Car { // 定義類別名稱,名稱前面一定是public class
// 以下稱實作變數instance variables (或稱變數成員data member)
private int currentSpeed;
private int currentCadence;
public int currentGear;
// 以下稱實作函數instance method (或稱函數成員member function)
public void speed(int intSpeed) {
currentSpeed += intSpeed;
}
public void brake() {
currentSpeed = 0;
}
public void changeGear(int GearNo) {
currentGear = GearNo;
}
}
*****************************************************************************
Ref7: 物件導向設計: 類別與物件
http://dns2.asia.edu.tw/~wzyang/slides/Java/Chen/SE7ch07.pdf
物件(object)
真實世界中的所有具體或抽象的事物,都可 以將之視為一個『物件』。 例如:您可以把一架飛機想像成是一個物件(Object) ;而飛機的零件(例如:座椅、引擎、操縱桿)則是 較小的物件明顯地,這些物件仍舊可以再細分為更小的物件(例如:螺 絲釘)。在程式設計中,Java的物件實際上是一些程式碼和資料的組合,物件必須能夠單獨成為 一個完整單元,也可以組合成更大的物件。例如:一個按鈕或一個表單都是一個物件。而一個應用程式最大的物件就是應用程式的本身。
屬性 (Property) 物件擁有許多特性(attribute),這些特性代表了一個物件的外觀或某些性質 例如:民航機物件的最高速度就代表了該飛機的一種特 性、民航機物件的重量、載重量…等等也都可以用來代 表飛機的某些特性 這些特性在物件導向程式設計中稱之為屬性(Property) 事實上,有的時候在取得物件的某些屬性時,所得到的 也可能是另一個(子)物件(若該屬性的資料型態是一 個類別)。 例如:民航機的引擎也是一個物件,因為它可以由更多更小的零件來 組成,同時引擎物件也存在自己的方法(Method),例如:引擎點火。 在程式設計或執行階段,我們可以藉由改變屬性值來改變 整個物件的某些特性,完成我們想要的物件表示形式。 例如:將民航機物件的機殼漆成藍色,將按鈕背景顏色 設為黃色
在Java語言中,屬性(Property)被稱為Field(有人翻譯為欄位;本書有時也翻譯為成員資料變數),並且被區分為不同的等級某些等級的成員資料變數並不允許外部程式加以修改,而必須透過該物件的方法,才能夠修改這些成員資料變數。因此具備保護資料的功能。
方法 (Method) 每個物件都擁有不同數量的行為(method),這些行為在物件導向程式設計中稱之為方法(Method) 。不同種類的物件所擁有的方法大多不同,但同 類物件擁有的方法則大致相同。所謂方法,也就是為了完成該物件某些目標的處理方式。 例如:飛機類別的物件有許多方法使得飛機變得有些用 途,這些方法如起飛、降落、逃生等等。每個方法都有許多的細節 例如起飛,可能包含『發動引擎、直到拉動操縱桿』,這些就是起飛方法的細節。
在Java中,方法也稱之為method(有些書籍翻譯為方法;本書翻譯為方法或函式,也就是成員函式)。 同時Java的method也和Field一樣,被區分為多種等級 某些等級的成員函式同樣不允許外部程式呼叫執行(只能做為內部成員函式呼叫執行的對象)因此達到封 對於某些由他人完成的物件而言,該物件同樣必須提供許多關於物件的方法,如此一來,我們就不需要了 解這些方法的細節,而能夠快速運用物件來完成工作 。 例如飛機物件提供了起飛方法,我們只要指定執行起飛方法 ,而不需要了解起飛過程的種種細節,飛機就會起飛。 裝物件的功能。
圖7-1 類別與物件關係圖
類別(class)
物件雖然是獨立的個體,不同的物件可以擁有相同的屬性及方法,例如:「一架民航機」和「一架戰鬥機」各是一個物件,但兩者的速度、爬升力、重量、載重量雖不相同,但卻同樣擁有這些屬性以便表達完整物件的各個特性。所以,不同的物件若擁有共同的屬性,但因為屬性內容的不同,因此可以創造出同類性質但卻獨立的不同物件。而同類型的物件,則構成了類別(Class)。
例如每一個按鈕都是一個物件,屬於按鈕類別之下所建造出來的物件實體,但是由於按鈕的名稱不同,因此視為不同且獨立的物件。
更明確地說,類別才是物件導向程式設計最基本的單元
以上例來說,「民航機」和「戰鬥機」都是物件,而『 飛行器』或『飛機』才是類別。不同的只是在建立該類 別的實體物件時,給予不同的屬性而已。 事實上,在實際的物件導向程式設計中(例如 Java),我們必須先定義類別,然後才能夠透過類別宣告各個屬於該類別下的物件,接著再設定物件的屬性來代表該物件某方面的特性,並使用物件的方法來操作物件。
從由下而上的角度來看,類別是許多同類物件的集合
所謂同類物件,代表擁有相同屬性(資料變數)及方法(函式 )的物件 例如:每一台『飛行器』類別下的物件,都包含「速度」、「爬升力 」、「重量」、「載重量」等變數,也包含「起飛」、「降落」、「 逃生」等方法,以便完成工作。由於我們可以將這些變數設定為不同 的屬性值,因此可以在該類別下造就各式各樣的物件 如同「民航機」和「戰鬥機」各是一個物件,而『飛行器』則是類別 。 在純物件導向的程式中 程式碼是以類別為基礎 物件只是類別的實體 就如同汽車是一個類別,它是一個抽象的名詞 而車牌號碼為010、015等的車輛是汽車類別的兩個物件 實際運作時「通常」依靠的是物件。
老師的叮嚀
有些人在說明類別與物件的關係時,會將類別視為一種板模,而物件則是依靠板模建立出來的實體,就如同雞蛋糕板模是類別,而每一個實際做出來可以吃的雞蛋糕是物件。 這種比喻方式在某種程度上說得通,不過仍舊不能完全說明Java類別與物件的實際應用,因為Java也允許在未產生物件實體時,存取變數或執 行方法,也就是靜態變數與靜態方法。
*****************************************************************************
Ref8: https://www.javatpoint.com/object-and-class-in-java
In this page, we will learn about java objects and classes. In object-oriented programming technique, we design a program using objects and classes.
Object is the physical as well as logical entity whereas class is the logical entity only.
An entity that has state and behavior is known as an object e.g. chair, bike, marker, pen, table, car etc. It can be physical or logical (tangible and intangible). The example of intangible object is banking system.
An object has three characteristics:
state: represents data (value) of an object.
behavior: represents the behavior (functionality) of an object such as deposit, withdraw etc.
identity: Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But, it is used internally by the JVM to identify each object uniquely.
For Example: Pen is an object. Its name is Reynolds, color is white etc. known as its state. It is used to write, so writing is its behavior.
Object is an instance of a class. Class is a template or blueprint from which objects are created. So object is the instance(result) of a class.
Object Definitions:
Object is a real world entity.
Object is a run time entity.
Object is an entity which has state and behavior.
Object is an instance of a class.
A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity. It can't be physical.
A class in Java can contain:
fields
methods
constructors
blocks
nested class and interface
1. class <class_name>{
2. field;
3. method;
4. }
In this example, we have created a Student class that have two data members id and name. We are creating the object of the Student class by new keyword and printing the objects value.
Here, we are creating main() method inside the class.
File: Student.java
1. class Student{
2. int id;//field or data member or instance variable
3. String name;
4.
5. public static void main(String args[]){
6. Student s1=new Student();//creating an object of Student
7. System.out.println(s1.id);//accessing member through reference variable
8. System.out.println(s1.name);
9. }
10.}
Output:
0
null
In real time development, we create classes and use it from another class. It is a better approach than previous one. Let's see a simple example, where we are having main() method in another class.
We can have multiple classes in different java files or single java file. If you define multiple classes in a single java source file, it is a good idea to save the file name with the class name which has main() method.
File: TestStudent1.java
1. class Student{
2. int id;
3. String name;
4. }
5. class TestStudent1{
6. public static void main(String args[]){
7. Student s1=new Student();
8. System.out.println(s1.id);
9. System.out.println(s1.name);
10. }
11.}
Output:
0
null
*****************************************************************************
https://blog.miniasp.com/post/2009/08/27/OOP-Basis-What-is-class-and-object.aspx
https://blog.miniasp.com/post/2009/10/01/OOP-Basis-What-is-class-and-object-ANSWER.aspx
http://www.programmer-club.com.tw/ShowSameTitleN/oop/356.html
java class object 搜尋google
類別與物件的基本概念 good
http://programming.im.ncnu.edu.tw/J_Chapter2.htm
https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html
http://www.codedata.com.tw/book/java-basic-source/ch11-5.htm
https://www.tutorialspoint.com/java/java_object_classes.htm
http://teddy-chen-tw.blogspot.tw/2012/01/2object-class-instance.html
https://1fly2sky.wordpress.com/2015/10/17/%E9%A1%9E%E5%88%A5class%E8%88%87%E7%89%A9%E4%BB%B6object%E7%9A%84%E5%B7%AE%E7%95%B0/
http://www.gotop.com.tw/epaper/e0715/AEL004300%E5%85%A7%E5%AE%B9%E8%A9%A6%E9%96%B1.pdf
http://www.kaiching.org/2011/05/java-class.html
https://openhome.cc/Gossip/Java/Class.html
https://openhome.cc/Gossip/JavaGossip-V1/ClassABC.htm
http://dns2.asia.edu.tw/~wzyang/slides/Java/Chen/SE7ch07.pdf
http://eportfolio.lib.ksu.edu.tw/user/4/9/4980E078/repository/bool/Java-982-04.pdf
https://www.javatpoint.com/object-and-class-in-java
https://www.javatpoint.com/object-class
https://openhome.cc/Gossip/JavaGossip-V1/AbstractClass.htm
ftp://Alg@chenlw.idv.tw
http://www.csie.ntnu.edu.tw/~u91029/Algorithm.html
https://zh.wikipedia.org/wiki/%E7%AE%97%E6%B3%95
http://wiki.mbalib.com/zh-tw/%E7%AE%97%E6%B3%95%E7%AD%96%E7%95%A5
http://www.chwa.com.tw/TResource/HS/book2/ch7/7-5.htm
http://www.books.com.tw/products/0010691123