Using Inheritance Calculator 12 using System namespace Using
- Slides: 46
Using. Inheritance. Calculator (1/2) using System; namespace Using. Inheritance { /* * 計算器 * 3/17/2007 */ class Calculator { public int Add(int a, int b) { int result = a + b; return result; } 2
Using. Inheritance. 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; return result; } public int Divide(int a, int b) { int result = a / b; return result; } { { } } 3
Using. Inheritance. Program (1/7) using System; namespace Using. Inheritance { /* * 示範繼承的使用 * 4/23/2007 */ class Program { static void Main(string[] args) { Advanced. Calculator calculator = new Advanced. Calculator(); 4
Using. Inheritance. Program (2/7) int op = 0; int operand 1 = 0; int operand 2 = 0; int result; double angle = 0. 0; double function. Value; do { Console. Write( "指定運算: 0. 結束; 1. 加; 2. 減; 3. 乘; 4. 除; " + " n 5. 正弦函數; 6. 餘弦函數; 7. 正切函數: "); op = int. Parse(Console. Read. Line()); if (op == 0) break; if (op > 7) continue; 5
Using. Inheritance. Program (3/7) if (op < 5) { Console. Write("輸入第一個數字: "); operand 1 = int. Parse(Console. Read. Line()); Console. Write("輸入第二個數字: "); operand 2 = int. Parse(Console. Read. Line()); } else { Console. Write("輸入角度(度)"); angle = double. Parse(Console. Read. Line()); } switch (op) { 6
Using. Inheritance. Program (4/7) case 1: result = calculator. Add(operand 1, operand 2); Console. Write. Line("{0} + {1} = {2} ", operand 1, operand 2, result); break; case 2: result = calculator. Subtract(operand 1, operand 2); Console. Write. Line("{0} - {1} = {2} ", operand 1, operand 2, result); break; case 3: result = calculator. Multiply(operand 1, operand 2); 7
Using. Inheritance. Program (5/7) Console. Write. Line("{0} * {1} = {2} ", operand 1, operand 2, result); break; case 4: result = calculator. Divide(operand 1, operand 2); Console. Write. Line("{0} / {1} = {2} ", operand 1, operand 2, result); break; case 5: function. Value = calculator. Get. Sine(angle); Console. Write. Line( "Sine of {0} (deg) = {1}", angle, function. Value); break; 8
Using. Inheritance. Program (6/7) case 6: function. Value = calculator. Get. Cosine(angle); Console. Write. Line( "Cosine of {0} (deg) = {1}", angle, function. Value); break; case 7: function. Value = calculator. Get. Tangent(angle); Console. Write. Line( "Tangent of {0} (deg) = {1}", angle, function. Value); break; 9
Using. Inheritance. Program (7/7) default: Console. Write. Line( "Should not see this message. Debug!!!"); break; } } while (true); } } } 10
Using. Inheritance. Advanced. Calculator (1/2) using System; namespace Using. Inheritance { /* * 可計算三角函數之計算器 * 4/23/2007 */ public class Advanced. Calculator { public double Get. Sine( double { angle *= Math. PI/180. 0; return Math. Sin(angle); } : Calculator angle ) 11
Using. Inheritance. Advanced. Calculator (2/2) public double Get. Cosine( double angle ) { angle *= Math. PI/180. 0; return Math. Cos(angle); } public double Get. Tangent( double angle ) { angle *= Math. PI/180. 0; return Math. Tan(angle); } } } 12
表示繼承的UML類別圖 Calculator Add() Subtraction() Multiply() Divide() Advanced. Calculator Get. Sine() Get. Cosine() Get. Tangent() 13
類別繼承之階層關係 class A { public int data 1; public int data 2; //…other members are methods }; class B : public A { public int data 3; //…other members are methods }; class C : public B { public int data 1; public int data 4; //…other members are methods }; A B C 14
物件記憶體分配模型 A B C a; b; c; a data 1 data 2 c A. data 1 data 2 data 3 b data 1 data 2 data 1 data 4 data 3 15
練習 • 實作並測試下列繼承關係 Student reg. No : string name: string Reg. No Name Graduate. Student advisor: string Advisor 16
Using. Protected. Program (1/2) using System; namespace Using. Protected { /* 示範protected成員之使用 * 4/23/2007 */ class Program { static void Main(string[] args) { DC d = new DC(); d. set. X(3); //Console. Write. Line( d. get. X() ) ; // d. x = 77; // Error! d. add 2(); } } // Error! 17
Using. Protected. Program (2/2) public class BC { public void set. X( int x ) { this. x = x; } protected int get. X() { return x; } private int x; } public class DC : BC { public void add 2() { int c = get. X(); set. X( c+2 ); } } } 18
限制繼承 sealed class SClass {. . . } 19
物件導向的關鍵技術 • 封裝(packaging) • 繼承(inheritance) • 多型(polymorphism) 20
Using. Override. Program (1/3) using System; namespace Using. Override { /* 示範方法函式之覆寫(overriding)與多型 * 5/2/2007 */ class Program { static void Main(string[] args) { Console. Write. Line( "選擇列印尺寸規格: 1. B 4 ; 2. A 3 ; 其他: 標準 (A 4)"); 22
Using. Override. Program (2/3) int report. Type = int. Parse(Console. Read. Line()); Console. Write. Line(); Printer pr; switch (report. Type) { case 1: pr = new Printer. B 4(); pr. Print(); break; case 2: pr = new Printer. A 3(); pr. Print(); break; default: pr = new Printer(); pr. Print(); break; } 23
Using. Override. Program (3/3) Console. Write. Line(); Console. Read. Line(); } 24
Using. Override. Printer (1/2) using System; namespace Using. Override { Printer /* * 模擬標準印表機 * 5/1/2007 */ Print() class Printer { public Printer() { Console. Write. Line("印表機備便"); } virtual public void Print() { Console. Write. Line("列印標準尺寸"); Finish("標準"); } 25
Using. Override. Printer (2/2) protected void Finish(string type) { Console. Write. Line("{0}報表列印完成", type); } } } 26
Using. Override. Printer. B 4 using System; namespace Using. Override { /* * 模擬B 4印表程式 * 5/1/2007 */ class Printer. B 4 : Printer { public Printer. B 4() { Console. Write. Line("B 4尺寸列印程式備便"); } override public void Print() { Console. Write. Line("列印B 4尺寸"); Finish("B 4"); } } } 27
Using. Override. Printer. A 3 using System; namespace Using. Override { /* * 模擬A 3印表程式 * 5/1/2007 */ class Printer. A 3 : Printer { public Printer. A 3() { Console. Write. Line("A 3尺寸列印程式備便"); } override public void Print() { Console. Write. Line("列印A 3尺寸"); Finish("A 3"); } } } 28
Using. Constructors. For. Inheritance. Program (1/4) using System; namespace Using. Constructors. For. Inheritance { /* * 示範繼承架構中的建構式呼叫 * 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. Read. Line(); } } 30
Using. Constructors. For. Inheritance. Program (2/4) class Animal { public Animal() { Console. Write. Line("Animal()"); species = "Animal"; } public Animal( string s ) { Console. Write. Line("Animal("+ s +")"); species = s; } private string species; } 31
Using. Constructors. For. Inheritance. Program (3/4) class Primate : Animal { public Primate() : base() { Console. Write. Line( "Primate()" ); } public Primate( int n ) : base( "Primate" ) { Console. Write. Line("Primate(" + n +")"); heart. Cham = n; } private int heart. Cham; } 32
Using. Constructors. For. Inheritance. Program (4/4) class Human : Primate { public Human() : base( 4 ) { Console. Write. Line( "Human()" ); } } } 33
衍生物件產生流程 Primate human = new Primate( 4 ); public Primate( int n ) : base( "Primate" ) {. . . } public Animal( string s ) {. . . } 34
Using. Base. Program (1/6) using System; namespace Using. Base { /* 示範base與new關鍵字的用法 * Ref: C# 程式設計人員參考 * 瞭解使用override 和new 關鍵字的時機(C# 程式設計手冊) * mshelp: //MS. VSExpress. CC. v 80/MS. NETFramework. v 20. cht/ dv_csref/html/323 db 184 -b 136 -46 fc-8839007886 e 7 e 8 b 0. htm * 5/2/2007 */ class Program { static void Main(string[] args) { 35
Using. Base. Program (2/6) // new and override make no differences here Car car 1 = new Car(); car 1. Describe. Car(); System. Console. Write. Line("-----"); Convertible. Car car 2 = new Convertible. Car(); car 2. Describe. Car(); System. Console. Write. Line("-----"); Minivan car 3 = new Minivan(); car 3. Describe. Car(); System. Console. Write. Line("-----"); 36
Using. Base. Program (3/6) // they are different in polymorphysm Car[] cars = new Car[3]; cars[0] = new Car(); cars[1] = new Convertible. Car(); cars[2] = new Minivan(); foreach (Car vehicle in cars) { System. Console. Write. Line("Car object: " + vehicle. Get. Type()); vehicle. Describe. Car(); System. Console. Write. Line("-----"); } } } 37
Using. Base. Program (4/6) // Define the base class Car { public virtual void Describe. Car() { System. Console. Write. Line( "Four wheels and an engine. "); } } 38
Using. Base. Program (5/6) // Define the derived classes class Convertible. Car : Car { public new virtual void Describe. Car() { base. Describe. Car(); System. Console. Write. Line( "A roof that opens up. "); } } 39
Using. Base. Program (6/6) class Minivan : Car { public override void Describe. Car() { base. Describe. Car(); System. Console. Write. Line( "Carries seven people. "); } } } 40
預設類別System. Object • 一致化型別 • 成員函式 – Equals – Get. Hash. Code – Get. Type – Reference. Equals – To. String – Finalize 42
Inheriting. Object. Program (1/2) using System; namespace Inheriting. Object { /* * 示範Object類別 * 5/2/2007 */ class Program { static void Main(string[] args) { Test t 1 = new Test(); Test t 2 = new Test(); bool is. Equal = t 1. Equals(t 2); Console. Write. Line(t 1. To. String()); Console. Write. Line("t 1 與t 2 相等為" + is. Equal); Console. Read. Line(); } } 43
Inheriting. Object. Program (2/2) class Test { override public string To. String() { return "覆寫Inheriting. Object. Test"; } } } 44
Boxing 與 Unboxing int x = 10; Object obj = (Object) x; // boxing obj = 20; int j = (int)obj; // unboxing 45
練習 • 利用偵錯器體驗了解程式 Using. Constructors. For. Inheritance、 Using. Base、Inheriting. Object 46
- Interface calculator public int add(int a int b) class test
- #include iostream using namespace std class base public
- Using namespace std
- Include iostream using namespace std
- #include iostream using namespace std
- Iostream c++
- #include string
- Fungsi using namespace std
- Using namespace std
- #include iostream using namespace std
- Using namespace std
- Using namespace std c++
- #include iostream using namespace std
- System.collections.generic namespace
- Bank account java program using inheritance
- Savings account java
- System collections generic
- Mifflin formula
- Namespace python
- Namespace std;
- C#10 namespace
- Examples of domain name
- Namespace in c++
- Namespace in c++
- Namespace in c++
- Javax.xml.bind.jaxbcontext not present
- The domain section uses two character country abbreviations
- Xml root
- Simplify radical expressions using conjugates calculator
- Volume using cross sections calculator
- Lex yacc example
- Modeling using variation
- Calculator using rmi in java
- Reverse percentage calculator excel
- Prisma model inheritance
- Objek diagram
- Priority inheritance
- Inheritance scala