Introduction to Constructor, Overloading, and Accessibility CS340100, NTHU Yoshi.

Slides:



Advertisements
Similar presentations
 Overloading Methods  The Scope of Variables  The Math Class  Floating point Format [Sample code] TestMethodOverloading.java 、 AmbiguousOverloading.java.
Advertisements

Introduction to Java Programming Lecture 10 Method Benefits, Declaring, and Calling Methods.
1 生物計算期末作業 暨南大學資訊工程系 2003/05/13. 2 compare f1 f2  只比較兩個檔案 f1 與 f2 ,比完後將結果輸出。 compare directory  以兩兩比對的方式,比對一個目錄下所有檔案的相 似程度。  將相似度很高的檔案做成報表輸出,報表中至少要.
Divide-and-Conquer. 什麼是 divide-and-conquer ? Divide 就是把問題分割 Conquer 則是把答案結合起來.
Introduction to Java Programming Lecture 13 Classes I OO Programming.
第 10 章 Java 的執行緒 10-1 執行緒的基礎 10-2 Java 執行緒的使用方法 10-3 Java 執行緒的同步 10-4 管道串流的執行緒.
學習C++的基本語法 認識關鍵字與識別字的不同 學習程式碼偵錯的流程 學習如何提高程式的可讀性
Reference, primitive, call by XXX 必也正名乎 誌謝 : 部份文字取於前輩 TAHO 的文章.
如何將數字變成可用之資訊 現代化資料處理與應用概念. 如何將數字變成可用之資訊 人最容易接受的訊息是圖像化資訊。 在一堆數字中,要進行比較分析,一般會使用表格形 式計算與分析。 所以一般我們會將數字依關聯性, 轉換成表格計算與分析。 此表格一般稱試算表或稱表格。 再將結果轉換為圖表,進行比較與分析。
Course Introduction CLASS III. Outline “equals” and “==“, the equality problem The root of the class hierarchy Modifier Nested classes –Instance nested.
Course Introduction CLASS II. 1-2 Previous studies What is constructor? How to use this()? –Where I can call this()? –Can I call this() twice in one constructor?
What is static?. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1; this.instanceX =
Reference, primitive, call by XXX 必也正名乎 誌謝 : 部份文字取於前輩 TAHO 的文章 CS340100, NTHU Yoshi.
Course Introduction Class. 1-2 軟體中的類別 (Class ) 類別就是物件的藍圖 (blueprint) 或原型 (prototype) , 裡面定義著物件的變數與方法。 類別 : 具有共同特性的一組物件.
在 Ad-hoc 網路中實現點對 點發送訊息與廣播訊息. 檔案下載  範例程式可在下列網址取得  DEMO 程式可在下列網址取得
: Factstone Benchmark ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: : Factstone Benchmark 解題者:鐘緯駿 解題日期: 2006 年 06 月 06 日 題意: 假設 1960.
建立靜態資料成員 關鍵字: static 靜態資料成員不屬於特定物件,而是該類物件 所共同擁有的,可做為類別範圍裡的靜態變數。
JAVA 程式設計與資料結構 第二章 JAVA 程式基本概念及操作. 第一個程式 /* * 這是第一個程式 (FirstP.java) */ class FirstP{ public static void main(String args[]){ System.out.println("Whatever.
第 13 章 常數、靜態成員與朋友關 係 13-1 常數物件與成員 13-1 常數物件與成員 13-2 物件成員與巢狀類別 13-2 物件成員與巢狀類別 13-3 靜態成員資料與函數 13-3 靜態成員資料與函數 13-4 朋友關係 13-4 朋友關係.
JAVA 程式設計與資料結構 第十四章 Linked List. Introduction Linked List 的結構就是將物件排成一列, 有點像是 Array ,但是我們卻無法直接經 由 index 得到其中的物件 在 Linked List 中,每一個點我們稱之為 node ,第一個 node.
第 18 章 名稱空間與例外處理 18-1 名稱空間 18-1 名稱空間 18-2 例外處理 18-2 例外處理.
第 7 章 程序與函數 7-1 模組化程式設計 7-1 模組化程式設計 7-2 VB.NET 的程序與函數 7-2 VB.NET 的程序與函數 7-3 程序與函數的變數範圍 7-3 程序與函數的變數範圍 7-4 VB.NET 常用的內建函數 7-4 VB.NET 常用的內建函數 7-5 遞迴函數 7-5.
Introduction to Java Programming Lecture 5: Using Java Classes : String & Math Spring 2009.
Introduction to pointers in C/C++. Pointers 特殊變數 存放變數在記憶體中的位址 MinGW C++ 中佔用 4 bytes 間接定址取執法 位址 指標變數 變數內容 變數.
Introduction to Java Programming Lecture 15 Objects and Classes.
Chapter 13 塑模靜態觀點:物件圖 Static View : Object Diagram.
Introduction to Java Programming Lecture 17 Abstract Classes & Interfaces.
第九章 結構. 結構的宣告 結構 它是由許多不同 ( 或相同 ) 資料型態的變數所組成的 集合,通常利用結構標籤稱呼此集合 struct student { char *name; int score; }; struct 為保留字,表示結構的宣告開始 結構項目需定義於大括號「 { } 」內 結尾需加上分號.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Introduction to Java Programming Lecture 10 Array I Declaring, Creating, and Initializing Arrays.
Chapter 20 塑模動態觀點:狀態圖 Statechart Diagram. 學習目標  說明狀態圖的目的  定義狀態圖的基本記號  展示狀態圖的建構  定義活動、內部事件及遞延事件的狀態 圖記號.
Hung-Hsiang WuWindows Processing Design1 Chapter 3 基本觀念 變數宣告與型態 特殊運算子符號 字串與數值的轉換 類別與物件的觀念 建立新的專案 WinMain 程式進入點 Include Header File.
類別函式庫. Java Bean 一個單獨的 AWT 元件都是一個 bean. Java Bean 也有一個 bean 的 class 可繼承, 但是並不強 迫一定要繼承. Bean 的結構 Properties 屬性 : 描述 bean 的屬性 如顏色, 大小,label 等. 透過一對 methods.
第 13 章 檔案與資料夾處理 13-1 檔案與資料夾操作 13-1 檔案與資料夾操作 13-2 循序檔案的文字檔案讀寫 13-2 循序檔案的文字檔案讀寫 13-3 隨機檔案的處理 13-3 隨機檔案的處理 13-4 二進位檔案的讀寫 13-4 二進位檔案的讀寫 13-5 檔案對話方塊 13-5 檔案對話方塊.
資料結構實習-一 參數傳遞.
6-2 認識元件庫與內建元件庫 Flash 的元件庫分兩種, 一種是每個動畫專 屬的元件庫 (Library) ;另一種則是內建元 件庫 (Common Libraries), 兩者皆可透過 『視窗』功能表來開啟, 以下即為您說明。
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2008.
Windows Processing Design1 Chapter 1 C/C++ 概論 畫面輸出與鍵盤輸入 程式流程 程式流程的迴圈 函數的基礎 指標與陣列 字串 類別 利用 BCB 開發程式.
第 17 章 樣板 17-1 樣板的基礎 17-1 樣板的基礎 17-2 樣板函數 17-2 樣板函數 17-3 樣板類別 17-3 樣板類別 17-4 非型態參數的樣板類別 17-4 非型態參數的樣板類別 17-5 樣板類別的繼承 17-5 樣板類別的繼承 17-6 特化樣板與靜態成員 17-6 特化樣板與靜態成員.
OBJECTS AND CLASSES Outline Object-Oriented Programming 類別的宣告 & 物件的實作 Defining Classes for Objects 為什麼要物件導向 ? 物件導向的好處 Constructors.
Chapter 10 m-way 搜尋樹與B-Tree
第12章 多重表單與模組的應用程式 12-1 VB.NET的方案與專案 12-2 模組化VB.NET應用程式 12-3 多重表單的應用程式
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2009.
1 第二十章 大型程式的發展 使用名稱空間 熟悉大型程式的開發方式 條件式編譯指令的撰寫. 2 名稱空間 名稱空間就像是一個管理介面,可以將定義的識別 字放在名稱空間之下,而不會有相互衝突的發生 名稱空間的語法如下 把變數 var 放在名稱空間 name1 內 要存取使用名稱空 間 name1 的變數.
5 重複迴圈 5.1 增減運算符號 增量運算符號 減量運算符號
第 6 章 迴圈結構 6-1 計數迴圈 6-1 計數迴圈 6-2 條件迴圈 6-2 條件迴圈 6-3 巢狀迴圈 6-3 巢狀迴圈 6-4 While/End While 迴圈 6-4 While/End While 迴圈 6-5 跳出迴圈 6-5 跳出迴圈 6-6 VB.NET 的錯誤處理 6-6 VB.NET.
Introduction to Java Programming Lecture 12 Method Benefits, Declaring, and Calling Methods.
函式 Function 東海大學物理系‧資訊教育 施奇廷. 函式簡介 當程式越來越大、越複雜時,程式的維護、 除錯會變得更困難,此時必須引入函式來 簡化程式或將程式分段,將程式重複的部 分改寫為函式,將程式「模組化」 這種作法有下列優點:節省程式發展的時 間、邏輯容易瞭解、程式容易除錯、可分 工合作完成程式.
-Artificial Neural Network- Matlab操作介紹 -以類神經網路BPN Model為例
Visual C++重點複習.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2008.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2010.
7.4 程式範例 在螢幕上寫出“Hello” 的程式 (參考 code/pep-p200.odc).
What is static? CS340100, NTHU Yoshi. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1;
Microsoft Excel.
Introduction to Java Programming Lecture 16 Super and Sub Classes.
: Finding Paths in Grid ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11486: Finding Paths in Grid 解題者:李重儀 解題日期: 2008 年 10 月 14 日 題意:給一個 7 個 column.
著作權所有 © 旗標出版股份有限公司 第 14 章 製作信封、標籤. 本章提要 製作單一信封 製作單一郵寄標籤.
6 使用者函數 6.1 函數定義 宣告函數 呼叫函數 呼叫多個函數 6-6
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2009.
VHDL語法(3).
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 肆 資料分析與表達.
物件導向實習 極高的忘記答題率 … AB 卷都有的題目 : 4(1). Define method overloading 明明寫出了方法, 卻不回答老師的題目 (1)( 只要 寫出定義就好了 ) 另外, 讀題一定要仔細 : Two overloaded methods average.
Effective C# 50 Specific Way to Improve Your C# Item 6, 7 Sephiroth.Wang2012/08/01.
Java Tutorial /10/21. Java Resource Java SDK –Download from –Install Jdk windows-i586.exe –
物件導向系統實務. * 複習物件導向程式設計 * 如何與使用者互動?何謂傾聽者 (Listener) * 範例程式 : 剪刀石頭布遊戲.
Sun Educational Service 第九單元 實作封裝與建構式. Sun Educational Service Module 9, slide 2 of 43 單元概論 單元目標: – 利用封裝 (encapsulation) 來保護資料 – 設計建構式來初始化物件 問題與討論.
> Polymorphism. Polymorphism - 2 多型的功能 多型提供了「介面與實作分離」的一個重 要性,能將 what (是什麼)自 how (怎麼 做)之中抽離。 多型( polymorphism )除去型別之間的耦 合關係。
Speaker: Liu Yu-Jiun Date: 2009/4/15
Presentation transcript:

Introduction to Constructor, Overloading, and Accessibility CS340100, NTHU Yoshi

Outline Review blueprints and instances Constructor Overloading Accessibility 1-2

Class and Instances Review blueprint and instances Encapsulation 1-3

Class and Instances (2) Architect writes class Class instantiates objects 1-4

Encapsulation and Modularity Easy for Maintenance and Integration – Lead to low coupling & high cohesion 1-5

Constructor The same name as the class Set the initial states and run the initial behaviors while constructing No return type 1-6 class Vehicle {// 定義的類別 private int wheel;// 定義一個實體變數 public Vehicle() {// 類別的建構子 wheel = 4; }

Constructor (cont’d) Each class has at least one constructor – If you don’t give one, the default constructor will be created 1-7

Wrong Program (Why?) 1-8 // 建立一個 Vehicle 的類別 class Vehicle { Vehicle(String x){ System.out.println(“Vehicle’s Constructor”); } … public class App { public static void main(String[] args){ Vehicle obj = new Vehicle(); }

How to fix it! 1-9 // 建立一個 Vehicle 的類別 class Vehicle { Vehicle() { System.out.println(“Vehicle’s Constructor”); } Vehicle(String x){ System.out.println(“Vehicle’s Constructor”); } public void drive(){ System.out.println(“I’m driving”); } Any other way?

Overloading Also known as static polymorphism May be applied to – Constructors – Normal methods 1-10

Overloading 物件是依接收的訊息類型來執行不同的方法 – 名稱重用 – 只需訊息不同,足以讓物件辨識,一樣可以執行同 名的方法 例如:執行 Utility 物件的 max() 方法的訊息,如 下所示: Utility.max(23, 45); Utility.max(23, 45, 87); Utility.max(‘a’, ‘z’); 1-11

Constructor with Parameters A class may have multiple constructors, and decided according to the parameters 1-12 class Vehicle {// 定義的類別 private int wheel;// 定義一個實體變數 Vehicle() {// 類別的建構子 wheel = 4; } Vehicle(int n) {// 定義有參數的建構子 wheel = n; } Vehicle newCar1 = new Vehicle();// 實體化一個類別 Vehicle newCar2 = new Vehicle(6);// 實體化一個類別,並傳入參數

this 在建構子中呼叫另一個建構子,以避免重複撰 寫初始資料成員的程式碼 。 – 使用「 this() 」的方式呼叫另一個建構子的 語法只能使用於建構子中 – 不可以在建構子中重複的呼叫「 this() 」 1-13

Example public class Flower { int petalCount = 0; String s = new String("null"); //String=“null”; Flower(int petals) { petalCount = petals; System.out.println("petalCount= "+ petalCount); } Flower(String ss) { System.out.println( "s=" + ss); s = ss; } 1-14

Example (cont’d) Flower(String ss, int petals) { this(petals); //this(s); // Can't call two this()! s = ss; System.out.println("String & int args"); } Flower() { this("hi", 47); System.out.println("default constructor (no args)"); } void print() { //this(11); // Not inside non-constructor! System.out.println("petalCount = " + petalCount + " s = "+ s); } public static void main(String[] args) { Flower x = new Flower(); x.print(); } 1-15

Information Hiding Protection – Some properties are never modified Interface to programmers – A programmer does not need to know the details, but use the interactive interfaces 1-16

建立資料存取的方法 避免直接存取內部變數 Setter : 設定資料成員的值 public void setWheel(int n){// 設定 wheel 的值 wheel = n; } Getter : 取出資料成員的值 public int getWheel(){// 傳回 wheel 的值 return wheel; } Can you give me a good setter/getter example? 1-17

Accessibility 位置 private(default)protectedpublic 同一類別  同一套件中的子類別  同一套件,但不是子 類別  不同套件的子類別 ( 之 instance)  不同套件,也不是子 類別  1-18

1-19 Modifier public – 每各 class 皆可存取 (Default) – 同一個 package 的 class 才可以存取 protected – 同一個 package 的 class 才可以存取 – 不同 package 但是如果有繼承也可存取 可以存取 “ 繼承下來的 ” private – 同一個 class 才能存取

Tips Use the most restrictive access level that makes sense for a particular member. – That is, use private unless you have a good reason not to Avoid public fields except for constants 1-20

Example: Account public class Account { private int money; public void setMoney(int m) { if(m < 0) System.out.println("Error"); else money = m; } public void foo(Account otherAccount) { //private 的保護是 class 層級 otherAccount.money = -1000; } 1-21

Summary We have learnt – Constructor – Overloading – Encapsulation and information hiding – Accessibility 1-22

References javaOO/accesscontrol.html javaOO/accesscontrol.html 1-23