📜  C# 程序检查指定类是否为抽象类

📅  最后修改于: 2022-05-13 01:54:48.646000             🧑  作者: Mango

C# 程序检查指定类是否为抽象类

抽象是隐藏内部细节并仅显示功能的过程。 abstract 关键字用在类或方法之前,以将类或方法声明为抽象。在本文中,我们将学习如何检查指定的类是否为抽象类。为此,我们使用 Type 类的IsAbstract 属性。该属性用于检查给定的类型(即类名)是否是抽象的,是否必须被覆盖。如果指定的类型(即类名)是抽象的,它将返回 true。否则,返回假。

语法

public bool IsAbstract { get; }

示例 1:

C#
// C# program to check a specified class is
// an abstract class or not
using System;
using System.Reflection;
  
// Declare an abstract class named Geeks
abstract class Geeks 
{
      
    // Abstract method
    public abstract void geeksmethod();
}
  
class GFG{
      
static void Main()
{
      
    // Get the type of class by using typeof() function
    // Check the class is abstract or not by using
    // IsAbstract property
    if (typeof(Geeks).IsAbstract == true) 
    {
        Console.WriteLine("This is abstract");
    }
    else 
    {
        Console.WriteLine("This is not abstract");
    }
}
}


C#
// C# program to check a specified class is
// an abstract class or not
using System;
using System.Reflection;
  
// Declare an abstract class named Geeks
abstract class Geeks1 
{
      
    // Abstract method
    public abstract void geeksmethod();
}
  
// Declare a class named Geeks2
class Geeks2
{
      
    // Method 
    public void gfgfunc()
    {
        Console.WriteLine("This is method");
    }
}
  
// Declare a class named Geeks3
// It implement abstract class
class Geeks3:Geeks1 
{ 
      
    // Method 
    public override void geeksmethod()
    {
        Console.WriteLine("This is method");
    }
}
  
class GFG{
  
// Driver code
static void Main()
{
  
    // Get the type of class by using typeof() function
    // Check the class is abstract or not by using
    // IsAbstract property
    bool res1 = typeof(Geeks1).IsAbstract;
    bool res2 = typeof(Geeks2).IsAbstract;
    bool res3 = typeof(Geeks3).IsAbstract;
      
    Console.WriteLine("Is Geeks1 class is abstract class?" + res1);
    Console.WriteLine("Is Geeks2 class is abstract class?" + res2);
    Console.WriteLine("Is Geeks3 class is abstract class?" + res3);
}
}


输出:

This is abstract

示例 2:

C#

// C# program to check a specified class is
// an abstract class or not
using System;
using System.Reflection;
  
// Declare an abstract class named Geeks
abstract class Geeks1 
{
      
    // Abstract method
    public abstract void geeksmethod();
}
  
// Declare a class named Geeks2
class Geeks2
{
      
    // Method 
    public void gfgfunc()
    {
        Console.WriteLine("This is method");
    }
}
  
// Declare a class named Geeks3
// It implement abstract class
class Geeks3:Geeks1 
{ 
      
    // Method 
    public override void geeksmethod()
    {
        Console.WriteLine("This is method");
    }
}
  
class GFG{
  
// Driver code
static void Main()
{
  
    // Get the type of class by using typeof() function
    // Check the class is abstract or not by using
    // IsAbstract property
    bool res1 = typeof(Geeks1).IsAbstract;
    bool res2 = typeof(Geeks2).IsAbstract;
    bool res3 = typeof(Geeks3).IsAbstract;
      
    Console.WriteLine("Is Geeks1 class is abstract class?" + res1);
    Console.WriteLine("Is Geeks2 class is abstract class?" + res2);
    Console.WriteLine("Is Geeks3 class is abstract class?" + res3);
}
}

输出:

Is Geeks1 class is abstract class?True
Is Geeks2 class is abstract class?False
Is Geeks3 class is abstract class?False