170 likes | 183 Views
This program demonstrates the use and implementation of abstract classes and interfaces in C#. It includes examples of shapes, multi-interface implementation, and casting.
E N D
抽象類別與介面(Abstract Classes and Interfaces) 鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所
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());
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(); }
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; } }
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; } } }
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());
UsingInterface.Program (2/4) Circle c = new Circle(a); Console.WriteLine("圓形c之面積為" + c.Area()); Console.ReadLine(); } } interface Shape { double Area(); }
UsingInterface.Program (3/4) public class Square : Shape { double a; public Square(double a) { this.a = a; } public double Area() { return a * a; } }
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; } } }
抽象類別與介面 • 修飾語 • 欄位變數 • 建構式 • 函式方法覆寫與實作 • 多重繼承
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(); } }
MultiInterface.Program (2/3) interface Plane { void Fly(); } interface Ship { void Sail(); }
MultiInterface.Program (3/3) public class SeaPlane : Plane, Ship { public SeaPlane() { Console.WriteLine("建立水上飛機"); } public void Sail() { Console.WriteLine("水上滑行"); } public void Fly() { Console.WriteLine("空中飛行"); } } }
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() );
CastMultiInterfaces.Program (2/4) if( sq is Rectangle ) { Rectangle rec = (Rectangle) sq; Console.WriteLine( "sq的面積以矩形公式計算得"+rec.Area() ); } Console.ReadLine(); } } interface Rectangle { double Area(); }
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; }
CastMultiInterfaces.Program (4/4) double Rectangle.Area() { return a * a; } double Rhombus.Area() { return 0.5 * d * d; } } }