抽象是面向对象编程的重要组成部分。 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中文网其他相关文章!