C#中的抽象是隐藏内部细节并仅显示功能的过程。 abstract修饰符指示未完成的实现。在类或方法之前使用关键字abstract将类或方法声明为抽象。另外, abstract修饰符可与indexer ,events和properties一起使用。
例子:
public abstract void geek();
// this indicates the method 'geek()' is abstract
abstract class gfg
// this indicates the class 'gfg' is abstract
抽象方法:声明为抽象的方法,没有“ body”,仅在抽象类内部声明。必须使用override关键字在所有非抽象类中实现抽象方法。重写后,抽象方法位于非Abstract类中。我们可以在另一个类中派生该类,并且再次可以用它重写相同的抽象方法。
句法:
public abstract void geek();
// the method 'geek()' is abstract
抽象类:这是在C#中实现抽象的方法。从不打算直接实例化Abstract类。此类必须包含至少一个abstract方法,该方法在类定义中由关键字或修饰符abstract标记。 Abstract类通常用于在类层次结构中定义基类。换句话说,抽象类是一个不完整的类或特殊类,我们无法实例化。抽象类的目的是为派生类提供一个蓝图,并设置一些规则,以使派生类在继承抽象类时必须实现。我们可以将抽象类用作基类,并且所有派生类都必须实现抽象定义。
句法:
abstract class gfg{}
// class 'gfg' is abstract
重要事项:
- 通常,我们在继承时使用抽象类。
- 用户必须在子类中声明为抽象的方法之前使用override关键字,该抽象类用于在子类中进行继承。
- 抽象类不能被结构继承。
- 它可以包含构造函数或析构函数。
- 它可以使用非Abstract方法实现功能。
- 它不能支持多重继承。
- 它不能是静态的。
示例1:演示抽象类工作的程序
C#
// C# program to show the
// working of abstract class
using System;
// abstract class 'GeeksForGeeks'
public abstract class GeeksForGeeks {
// abstract method 'gfg()'
public abstract void gfg();
}
// class 'GeeksForGeeks' inherit
// in child class 'Geek1'
public class Geek1 : GeeksForGeeks
{
// abstract method 'gfg()'
// declare here with
// 'override' keyword
public override void gfg()
{
Console.WriteLine("class Geek1");
}
}
// class 'GeeksForGeeks' inherit in
// another child class 'Geek2'
public class Geek2 : GeeksForGeeks
{
// same as the previous class
public override void gfg()
{
Console.WriteLine("class Geek2");
}
}
// Driver Class
public class main_method {
// Main Method
public static void Main()
{
// 'g' is object of class
// 'GeeksForGeeks' class '
// GeeksForGeeks' cannot
// be instantiate
GeeksForGeeks g;
// instantiate class 'Geek1'
g = new Geek1();
// call 'gfg()' of class 'Geek1'
g.gfg();
// instantiate class 'Geek2'
g = new Geek2();
// call 'gfg()' of class 'Geek2'
g.gfg();
}
}
C#
// C# program to calculate the area
// of a Square using abstract class
// and abstract method
using System;
// declare class 'AreaClass'
// as abstract
abstract class AreaClass
{
// declare method
// 'Area' as abstract
abstract public int Area();
}
// class 'AreaClass' inherit
// in child class 'Square'
class Square : AreaClass
{
int side = 0;
// constructor
public Square(int n)
{
side = n;
}
// the abstract method
// 'Area' is overridden here
public override int Area()
{
return side * side;
}
}
class gfg {
// Main Method
public static void Main()
{
Square s = new Square(6);
Console.WriteLine("Area = " + s.Area());
}
}
C#
// C# program to show the working of
// the non-abstract method in the
// abstract class
using System;
abstract class AbstractClass {
// Non abstract method
public int AddTwoNumbers(int Num1, int Num2)
{
return Num1 + Num2;
}
// An abstract method which
// overridden in the derived class
public abstract int MultiplyTwoNumbers(int Num1, int Num2);
}
// Child Class of AbstractClass
class Derived : AbstractClass {
// implementing the abstract
// method 'MultiplyTwoNumbers'
// using override keyword,
public override int MultiplyTwoNumbers(int Num1, int Num2)
{
return Num1 * Num2;
}
}
// Driver Class
class geek {
// Main Method
public static void Main()
{
// Instance of the derived class
Derived d = new Derived();
Console.WriteLine("Addition : {0}\nMultiplication :{1}",
d.AddTwoNumbers(4, 6),
d.MultiplyTwoNumbers(6, 4));
}
}
C#
// C# program to show the working
// of abstract class with the
// get and set accessors
using System;
abstract class absClass {
protected int myNumber;
public abstract int numbers
{
get;
set;
}
}
class absDerived : absClass {
// Implementing abstract properties
public override int numbers
{
get
{
return myNumber;
}
set
{
myNumber = value;
}
}
}
// Driver Class
class geek {
// Main Method
public static void Main()
{
absDerived d = new absDerived();
d.numbers = 5;
Console.WriteLine(d.numbers);
}
}
输出:
class Geek1
class Geek2
示例2:使用抽象类和抽象方法计算正方形面积的程序
C#
// C# program to calculate the area
// of a Square using abstract class
// and abstract method
using System;
// declare class 'AreaClass'
// as abstract
abstract class AreaClass
{
// declare method
// 'Area' as abstract
abstract public int Area();
}
// class 'AreaClass' inherit
// in child class 'Square'
class Square : AreaClass
{
int side = 0;
// constructor
public Square(int n)
{
side = n;
}
// the abstract method
// 'Area' is overridden here
public override int Area()
{
return side * side;
}
}
class gfg {
// Main Method
public static void Main()
{
Square s = new Square(6);
Console.WriteLine("Area = " + s.Area());
}
}
输出:
Area = 36
以下是有关C#中抽象类的一些重要发现
1)抽象类并不意味着它仅包含抽象方法。 Abstract类也可以包含非抽象方法。
句法:
abstract class gfg
{
public void geek()
{
Console.WriteLine("'geek()' is non-abstract method");
}
}
例子:
C#
// C# program to show the working of
// the non-abstract method in the
// abstract class
using System;
abstract class AbstractClass {
// Non abstract method
public int AddTwoNumbers(int Num1, int Num2)
{
return Num1 + Num2;
}
// An abstract method which
// overridden in the derived class
public abstract int MultiplyTwoNumbers(int Num1, int Num2);
}
// Child Class of AbstractClass
class Derived : AbstractClass {
// implementing the abstract
// method 'MultiplyTwoNumbers'
// using override keyword,
public override int MultiplyTwoNumbers(int Num1, int Num2)
{
return Num1 * Num2;
}
}
// Driver Class
class geek {
// Main Method
public static void Main()
{
// Instance of the derived class
Derived d = new Derived();
Console.WriteLine("Addition : {0}\nMultiplication :{1}",
d.AddTwoNumbers(4, 6),
d.MultiplyTwoNumbers(6, 4));
}
}
输出:
Addition : 10
Multiplication :24
2)抽象类也可以与get和set访问器一起使用。
例子:
C#
// C# program to show the working
// of abstract class with the
// get and set accessors
using System;
abstract class absClass {
protected int myNumber;
public abstract int numbers
{
get;
set;
}
}
class absDerived : absClass {
// Implementing abstract properties
public override int numbers
{
get
{
return myNumber;
}
set
{
myNumber = value;
}
}
}
// Driver Class
class geek {
// Main Method
public static void Main()
{
absDerived d = new absDerived();
d.numbers = 5;
Console.WriteLine(d.numbers);
}
}
输出:
5