Download presentation
Presentation is loading. Please wait.
1
鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所
繼承 (Inheritance) 鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所
2
UsingInheritance.Program (1/7)
using System; namespace UsingInheritance { /* * 示範繼承的使用 * 4/23/2007 */ class Program static void Main(string[] args) AdvancedCalculator calculator = new AdvancedCalculator();
3
UsingInheritance.Program (2/7)
int op = 0; int operand1 = 0; int operand2 = 0; int result; double angle = 0.0; double functionValue; do { Console.Write( "指定運算: 0. 結束; 1. 加; 2. 減; 3. 乘; 4. 除; " + " \n 5. 正弦函數; 6. 餘弦函數; 7. 正切函數: "); op = int.Parse(Console.ReadLine()); if (op == 0) break; if (op > 7) continue;
4
UsingInheritance.Program (3/7)
if (op < 5) { Console.Write("輸入第一個數字: "); operand1 = int.Parse(Console.ReadLine()); Console.Write("輸入第二個數字: "); operand2 = int.Parse(Console.ReadLine()); } else Console.Write("輸入角度(度)"); angle = double.Parse(Console.ReadLine()); switch (op)
5
UsingInheritance.Program (4/7)
case 1: result = calculator.Add(operand1, operand2); Console.WriteLine("{0} + {1} = {2} ", operand1, operand2, result); break; case 2: result = calculator.Subtract(operand1, Console.WriteLine("{0} - {1} = {2} ", case 3: result = calculator.Multiply(operand1,
6
UsingInheritance.Program (5/7)
Console.WriteLine("{0} * {1} = {2} ", operand1, operand2, result); break; case 4: result = calculator.Divide(operand1, operand2); Console.WriteLine("{0} / {1} = {2} ", case 5: functionValue = calculator.getSine(angle); Console.WriteLine( "Sine of {0} (deg) = {1}", angle, functionValue);
7
UsingInheritance.Program (6/7)
case 6: functionValue = calculator.getCosine(angle); Console.WriteLine( "Cosine of {0} (deg) = {1}", angle, functionValue); break; case 7: calculator.getTangent(angle); "Tangent of {0} (deg) = {1}",
8
UsingInheritance.Program (7/7)
default: Console.WriteLine( "Should not see this message. Debug!!!"); break; } } while (true);
9
UsingInheritance.AdvancedCalculator (1/2)
using System; namespace UsingInheritance { /* * 可計算三角函數之計算器 * 4/23/2007 */ public class AdvancedCalculator : Calculator public double getSine( double angle ) angle *= Math.PI/180.0; return Math.Sin(angle); }
10
UsingInheritance.AdvancedCalculator (2/2)
public double getCosine( double angle ) { angle *= Math.PI/180.0; return Math.Cos(angle); } public double getTangent( double angle ) return Math.Tan(angle);
11
UsingInheritance.Calculator (1/2)
using System; namespace UsingInheritance { /* * 計算器 * 3/17/2007 */ class Calculator public int Add(int a, int b) int result = a + b; return result; }
12
UsingInheritance.Calculator (2/2)
public int Subtract(int a, int b) { int result = a - b; return result; } public int Multiply(int a, int b) { int result = a * b; public int Divide(int a, int b) { int result = a / b;
13
表示繼承的UML類別圖 Calculator Add() Subtraction() Multiply() Divide()
AdvancedCalculator getSine() getCosine() getTangent()
14
類別繼承之階層關係 A B C class A { public int data1; public int data2;
//…other members are methods }; class B : public A { public int data3; class C : public B { public int data4; A B C
15
物件記憶體分配模型 data1 data2 data3 a c A.data1 data2 data3 data1 b data4 A a;
B b; C c; data1 data2 data3 a c A.data1 data2 data3 data1 b data4
16
UsingProtected.Program (1/2)
using System; namespace UsingProtected { /* 示範protected成員之使用 * 4/23/2007 */ class Program static void Main(string[] args) DC d = new DC(); d.setX(3); //Console.WriteLine( d.getX() ) ; // Error! // d.x = 77; // Error! d.add2(); }
17
UsingProtected.Program (2/2)
public class BC { public void setX( int x ) { this.x = x; } protected int getX() { return x; } private int x; } public class DC : BC public void add2() int c = getX(); setX( c+2 );
18
限制繼承 sealed class SClass { }
19
物件導向的關鍵技術 封裝(packaging) 繼承(inheritance) 多型(polymorphism)
20
多型 編譯時連結(compile-time binding)與執行時連結(run-time binding) 多型之要求 多型之優缺點
靜態連結(static binding)與動態連結(dynamic binding) 多型之要求 繼承階層體系 虛擬(virtual)與覆寫(override) 基礎類別之參考 多型之優缺點
21
UsingOverride.Program (1/3)
using System; namespace UsingOverride { /* 示範方法函式之覆寫(overriding)與多型 * 5/2/2007 */ class Program static void Main(string[] args) Console.WriteLine( "選擇列印尺寸規格: 1. B4 ; 2. A3 ; 其他: 標準(A4)");
22
UsingOverride.Program (2/3)
int reportType = int.Parse(Console.ReadLine()); Console.WriteLine(); Printer pr; switch (reportType) { case 1: pr = new PrinterB4(); pr.Print(); break; case 2: pr = new PrinterA3(); default: pr = new Printer(); }
23
UsingOverride.Program (3/3)
Console.WriteLine(); Console.ReadLine(); }
24
UsingOverride.Printer (1/2)
using System; namespace UsingOverride { /* * 模擬標準印表機 * 5/1/2007 */ class Printer public Printer() Console.WriteLine("印表機備便"); } virtual public void Print() Console.WriteLine("列印標準尺寸"); Finish("標準"); Printer Print()
25
UsingOverride.Printer (2/2)
protected void Finish(string type) { Console.WriteLine("{0}報表列印完成", type); }
26
UsingOverride. PrinterB4
using System; namespace UsingOverride { /* * 模擬B4印表程式 * 5/1/2007 */ class PrinterB4 : Printer { public PrinterB4() { Console.WriteLine("B4尺寸列印程式備便"); } override public void Print() { Console.WriteLine("列印B4尺寸"); Finish("B4");
27
UsingOverride. PrinterA3
using System; namespace UsingOverride { /* * 模擬A3印表程式 * 5/1/2007 */ class PrinterA3 : Printer { public PrinterA3() { Console.WriteLine("A3尺寸列印程式備便"); } override public void Print() { Console.WriteLine("列印A3尺寸"); Finish("A3");
28
練習 撰寫類別Shape,定義虛擬方法Draw為輸出”畫形狀”字樣。其次撰寫類別Rectangle及Circle,分別覆寫Draw為輸出字串”畫長方形”及”畫圓形” 。寫一程式測試此一繼承結構之多型機制。
29
UsingBase.Program (1/6) using System; namespace UsingBase {
/* 示範base與new關鍵字的用法 * Ref: C# 程式設計人員參考 * 瞭解使用Override 和New 關鍵字的時機(C# 程式設計手冊) * ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.cht/dv_csref/html/323db184-b136-46fc e7e8b0.htm * 5/2/2007 */ class Program static void Main(string[] args)
30
UsingBase.Program (2/6) // new and override make no differences here
Car car1 = new Car(); car1.DescribeCar(); System.Console.WriteLine(" "); ConvertibleCar car2 = new ConvertibleCar(); car2.DescribeCar(); Minivan car3 = new Minivan(); car3.DescribeCar();
31
UsingBase.Program (3/6) // they are different in polymorphysm
Car[] cars = new Car[3]; cars[0] = new Car(); cars[1] = new ConvertibleCar(); cars[2] = new Minivan(); foreach (Car vehicle in cars) { System.Console.WriteLine("Car object: " + vehicle.GetType()); vehicle.DescribeCar(); System.Console.WriteLine(" "); }
32
UsingBase.Program (4/6) // Define the base class class Car {
public virtual void DescribeCar() System.Console.WriteLine( "Four wheels and an engine."); }
33
UsingBase.Program (5/6) // Define the derived classes
class ConvertibleCar : Car { public new virtual void DescribeCar() base.DescribeCar(); System.Console.WriteLine( "A roof that opens up."); }
34
UsingBase.Program (6/6) class Minivan : Car {
public override void DescribeCar() base.DescribeCar(); System.Console.WriteLine( "Carries seven people."); }
35
覆寫與隱藏 覆寫: override 隱藏: new 主要用於Polymorphism (多型) 執行時連結
只是取代基底類別同名之成員變數與方法 仍是編譯時連結
36
UsingConstructorsForInheritance.Program (1/4)
using System; namespace UsingConstructorsForInheritance { /* * 示範繼承架構中的建構式呼叫 * 5/2/2007 */ class Program { static void Main(string[] args) { Animal slug = new Animal(); Animal tweety = new Animal( "canary" ); Primate godzilla = new Primate(); Primate human = new Primate( 4 ); Human jill = new Human(); Console.ReadLine(); }
37
UsingConstructorsForInheritance.Program (2/4)
class Animal { public Animal() Console.WriteLine("Animal()"); species = "Animal"; } public Animal( string s ) Console.WriteLine("Animal("+ s +")"); species = s; private string species;
38
UsingConstructorsForInheritance.Program (3/4)
class Primate : Animal { public Primate() : base() Console.WriteLine( "Primate()" ); } public Primate( int n ) : base( "Primate" ) Console.WriteLine("Primate(" + n +")"); heartCham = n; private int heartCham;
39
UsingConstructorsForInheritance.Program (4/4)
class Human : Primate { public Human() : base( 4 ) Console.WriteLine( "Human()" ); }
40
預設類別System.Object 一致化型別 成員函式 Equals GetHashCode GetType
ReferenceEquals ToString Finalize
41
InheritingObject.Program (1/2)
using System; namespace InheritingObject { /* * 示範Object類別 * 5/2/2007 */ class Program { static void Main(string[] args) { Test t1 = new Test(); Test t2 = new Test(); bool isEqual = t1.Equals(t2); Console.WriteLine(t1.ToString()); Console.WriteLine("t1 與t2 相等為" + isEqual); Console.ReadLine(); }
42
InheritingObject.Program (2/2)
class Test { override public string ToString() return "覆寫InheritingObject.Test"; }
43
Boxing 與 Unboxing int x = 10; Object obj = (Object) x; // boxing
int j = (int)obj; // unboxing
Similar presentations