Csharp 继承
abstract 方法,在继承类中必须实现
Inheritance applies only to classes and interfaces. Other type categories (structs, delegates, and enums) do not support inheritance. 只有 class 和 interface 支持继承
One of these members is a parameterless (or default) constructor that is automatically supplied for the SimpleClass type by the C# compiler c# 编译器创建默认构造函数
父类若没有默认构造,子类必须在其构造函数内显式调用父类的带参构造才能保证子类对象的顺利实例化!
using System;
using System.Reflection;
public class SimpleClass
{}
public class Example
{
public static void Main()
{
Type t = typeof(SimpleClass);
BindingFlags flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public |
BindingFlags.NonPublic | BindingFlags.FlattenHierarchy;
MemberInfo[] members = t.GetMembers(flags);
Console.WriteLine($"Type {t.Name} has {members.Length} members: ");
foreach (var member in members) {
string access = "";
string stat = "";
var method = member as MethodBase;
if (method != null) {
if (method.IsPublic)
access = " Public";
else if (method.IsPrivate)
access = " Private";
else if (method.IsFamily)
access = " Protected";
else if (method.IsAssembly)
access = " Internal";
else if (method.IsFamilyOrAssembly)
access = " Protected Internal ";
if (method.IsStatic)
stat = " Static";
}
var output = $"{member.Name} ({member.MemberType}): {access}{stat}, Declared by {member.DeclaringType}";
Console.WriteLine(output);
}
}
}
// The example displays the following output:
// Type SimpleClass has 9 members:
// ToString (Method): Public, Declared by System.Object
// Equals (Method): Public, Declared by System.Object
// Equals (Method): Public Static, Declared by System.Object
// ReferenceEquals (Method): Public Static, Declared by System.Object
// GetHashCode (Method): Public, Declared by System.Object
// GetType (Method): Public, Declared by System.Object
// Finalize (Method): Internal, Declared by System.Object
// MemberwiseClone (Method): Internal, Declared by System.Object
// .ctor (Constructor): Public, Declared by SimpleClass
The following table lists the categories of types that you can create in C# and the types from which they implicitly inherit. Each base type makes a different set of members available through inheritance to implicitly derived types.
Type category | Implicitly inherits from |
---|---|
class | |
struct | |
enum | |
delegate |
##