1 / 17

Abstract Classes and Interfaces - Application and Implementation

This program demonstrates the use and implementation of abstract classes and interfaces in C#. It includes examples of shapes, multi-interface implementation, and casting.

criggs
Download Presentation

Abstract Classes and Interfaces - Application and Implementation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 抽象類別與介面(Abstract Classes and Interfaces) 鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所

  2. AbstractClass.Program (1/4) using System; namespace AbstractClass { /* * 示範抽象類別之應用 * skj 5/7/2007 */ class Program { static void Main(string[] args) { double a = 5.0; Square sq = new Square(a); Console.WriteLine("正方形sq之面積為" + sq.Area());

  3. AbstractClass.Program (2/4) Circle c = new Circle(a); Console.WriteLine("圓形c之面積為" + c.Area()); Console.ReadLine(); } } public abstract class Shape { private string shape; public Shape(string shape) { this.shape = shape; Console.WriteLine("建立" + shape); } abstract public double Area(); }

  4. AbstractClass.Program (3/4) public class Square : Shape { double a; public Square(double a) : base("正方形") { this.a = a; } public override double Area() { return a * a; } }

  5. AbstractClass.Program (4/4) public class Circle : Shape { double r; public Circle(double r) : base("圓形") { this.r = r; } public override double Area() { return Math.PI * r * r; } } }

  6. UsingInterface.Program (1/4) using System; namespace UsingInterface { /* * 示範介面之應用 * 5/7/2007 */ class Program { static void Main(string[] args) { double a = 5.0; Square sq = new Square(a); Console.WriteLine("正方形sq之面積為" + sq.Area());

  7. UsingInterface.Program (2/4) Circle c = new Circle(a); Console.WriteLine("圓形c之面積為" + c.Area()); Console.ReadLine(); } } interface Shape { double Area(); }

  8. UsingInterface.Program (3/4) public class Square : Shape { double a; public Square(double a) { this.a = a; } public double Area() { return a * a; } }

  9. UsingInterface.Program (4/4) public class Circle : Shape { double r; public Circle(double r) { this.r = r; } public double Area() { return Math.PI * r * r; } } }

  10. 抽象類別與介面 • 修飾語 • 欄位變數 • 建構式 • 函式方法覆寫與實作 • 多重繼承

  11. MultiInterface.Program (1/3) using System; namespace MultiInterface { /* 示範多重介面之實作 * skj 5/8/2007 */ class Program { static void Main(string[] args) { SeaPlane sp = new SeaPlane(); sp.Sail(); sp.Fly(); Console.ReadLine(); } }

  12. MultiInterface.Program (2/3) interface Plane { void Fly(); } interface Ship { void Sail(); }

  13. MultiInterface.Program (3/3) public class SeaPlane : Plane, Ship { public SeaPlane() { Console.WriteLine("建立水上飛機"); } public void Sail() { Console.WriteLine("水上滑行"); } public void Fly() { Console.WriteLine("空中飛行"); } } }

  14. CastMultiInterfaces.Program (1/4) using System; namespace CastMultiInterfaces { class Program { static void Main(string[] args) { double a = 5.0; Square sq = new Square(a); Rhombus rhomb = sq as Rhombus; Console.WriteLine( "sq的面積以菱形公式計算得"+rhomb.Area() );

  15. CastMultiInterfaces.Program (2/4) if( sq is Rectangle ) { Rectangle rec = (Rectangle) sq; Console.WriteLine( "sq的面積以矩形公式計算得"+rec.Area() ); } Console.ReadLine(); } } interface Rectangle { double Area(); }

  16. CastMultiInterfaces.Program (3/4) interface Rhombus { double Area(); } public class Square : Rectangle, Rhombus { private double a; private double d; public Square(double a) { this.a = a; d = Math.Sqrt(2.0) * a; }

  17. CastMultiInterfaces.Program (4/4) double Rectangle.Area() { return a * a; } double Rhombus.Area() { return 0.5 * d * d; } } }

More Related