抽象化是物件導向程式設計的重要組成部分。 C# 支援抽象,這是一種隱藏實作細節並僅向使用者提供基本資訊的過程。在C#中,我們可以藉助抽象類別和方法來實作抽象。
以下是有關抽象類別的一些要點:
文法及解釋:
我們可以藉助關鍵字abstract來宣告抽象類別和方法,如下語法所示:
//abstract class abstract class class_name { //abstract method public abstract <return_type> method_name(); }
在上面的語法中,我們可以看到抽象方法沒有任何人。衍生類別提供抽象類別的抽象方法的實作。我們可以透過繼承自抽象類別的類別來存取它。
我們不能在抽象類別之外宣告抽象方法,但是抽象類別可以包含非抽象方法,如下語法所示:
abstract class class_name { //abstract method public abstract <return_type> method_name(); //non-abstract method public <return_type> method_name() { //user code } }
有時,使用者需要在超類別中建立可以由其整個子類別共享的通用形式的方法和屬性,子類別可以根據自己的需求透過相應地提供方法的實作來使用這些方法和屬性。
代碼:
using System; namespace abstraction { //abstract class abstract class Shape { //abstract methods public abstract double calculateArea(); public abstract void displayDetails(double area); } //Rectangle class inheriting Shape class class Rectangle : Shape { //private data members private double length; private double breadth; public Rectangle(double length, double breadth) { this.length = length; this.breadth = breadth; } //overriding abstract methods of Shape class using 'override' keyword public override double calculateArea() { return (length * breadth); } public override void displayDetails(double area) { Console.Write("Length of rectangle: "+length); Console.Write("\nBreadth of rectangle: "+breadth); Console.Write("\nArea of rectangle: "+area); } } //Square class inheriting Shape class class Square : Shape{ //private data members private double side; public Square(double side) { this.side = side; } //overriding abstract methods of Shape class using 'override' keyword public override double calculateArea() { return (side * side); } public override void displayDetails(double area) { Console.Write("Length of a side of square: "+side); Console.Write("\nArea of square: "+area); } } public class AbstractionDemo { public static void Main(string[] args) { double area; //creating reference of Shape class using Rectangle class Shape shapeRec = new Rectangle(5,6); area = shapeRec.calculateArea(); shapeRec.displayDetails(area); Console.WriteLine("\n"); //creating reference of Shape class using Square class Shape shapeSquare = new Square(4); area = shapeSquare.calculateArea(); shapeSquare.displayDetails(area); } } }
輸出:
在上面的範例中,抽象類別「Shape」下定義的兩個方法都是抽象方法。因此,Shape 是一個純抽象基類,而衍生類別必須提供 Shape 類別的兩個方法的實作。
代碼:
using System; abstract class Mobile { public abstract void mobileDetails(); } class Apple : Mobile { public override void mobileDetails() { Console.WriteLine("Model: IphoneX"); Console.WriteLine("Screen size: 5.8 inch"); Console.WriteLine("Processor: A11 Bionic Processor"); } } class Motorola : Mobile { public override void mobileDetails() { Console.WriteLine("Model: G5 plus"); Console.WriteLine("Screen size: 5.2 inch"); Console.WriteLine("Processor: octa-core Snapdragon"); } } public class AbstractionDemo { public static void Main(string[] args) { Mobile mobileApple = new Apple(); mobileApple.mobileDetails(); Console.WriteLine(""); Mobile mobileMoto = new Motorola(); mobileMoto.mobileDetails(); } }
輸出:
抽象類別也可以包含非抽象方法,如下例所示。
代碼:
using System; namespace abstraction { abstract class Birds { //abstract method public abstract void birdDetails(); //non-abstract method public void canFly(bool canFly, string name) { if(canFly) Console.WriteLine(name+" can fly"); else Console.WriteLine(name+" cannot fly"); } } class Parrot : Birds { public override void birdDetails() { Console.WriteLine("Parrots have different colours and size"); } } class Ostrich : Birds { public override void birdDetails() { Console.WriteLine("Ostrich is the largest living bird"); } } public class AbstractionDemo { public static void Main(string[] args) { Birds birdParrot = new Parrot(); birdParrot.birdDetails(); birdParrot.canFly(true, "Parrot"); Console.WriteLine(""); Birds birdOstrich = new Ostrich(); birdOstrich.birdDetails(); birdOstrich.canFly(false, "Ostrich"); } } }
輸出:
除了抽象類別和方法之外,我們還可以使用私有存取修飾符在 C# 中實作抽象,如下例所示。
代碼:
using System; namespace abstraction { public class Bank { private string name; private string branch; private string ifscCode; public string Name { get { return name; } set { name = value; } } public string Branch { get { return branch; } set { branch = value; } } public string IfscCode { get { return ifscCode; } set { ifscCode = value; } } public void bankDetails() { Console.WriteLine("Bank name: " + Name); Console.WriteLine("Branch name: " + Branch); Console.WriteLine("IFSC code: " + IfscCode); } public void bankAddress() { Console.WriteLine("Address: Andheri, Mumbai"); } private void numberOfAccounts() { Console.WriteLine("Account Information"); } private void numberOfLockers() { Console.WriteLine("Locker Information"); } } public class AbstractionDemo { public static void Main(string[] args) { Bank bank = new Bank(); bank.Name = "ABC"; bank.Branch = "XYZ"; bank.IfscCode = "ABC123"; bank.bankDetails(); bank.bankAddress(); //Error -'Bank.numberOfAccounts()' is inaccessible due to its protection level //bank.numberOfAccounts(); //Error - 'Bank.numberOfLockers()' is inaccessible due to its protection level //bank.numberOfLockers(); } } }
輸出:
上面的例子表明,公共方法可以從其他類別訪問,但我們不能從其他類別呼叫私有方法。因此,它透過提供唯一必要的資訊並隱藏其他細節來幫助實現抽象。
物件導向程式設計中的抽象透過隱藏實作細節並僅向使用者提供必要的資訊來提供安全性。我們使用抽象類,在抽象方法的幫助下,我們可以隱藏實作細節。抽像也可以使用介面來實作。
以上是C# 中的抽象的詳細內容。更多資訊請關注PHP中文網其他相關文章!