• 【C#】抽象类和接口的区别


    namespace cs {
        public partial class Program {
            public abstract class A {
                // static members
                // abstract and virtual modifiers are not allowed on fields
                public static int P1SF;
                protected internal static int PISF;
                internal static int InSF;
                protected static int P2SF;
                private static int P3SF;
                // You cannot use abstract / virtual modifier on static properties of an abstract class
                public static int P1S_P { get; set; }
                protected internal static int PIS_P { get; set; }
                internal static int InS_P { get; set; }
                protected static int P2S_P { get; set; }
                private static int P3S_P { get; set; }
                // You cannot use abstract / virtual modifier on static methods of an abstract class
                public static void P1S_M() { }
                protected internal static void PIS_M() { }
                internal static void InS_M() { }
                protected static void P2S_M() { }
                private static void P3S_M() { }
                // instance members
                // abstract and virtual modifiers are not allowed on fields
                public int P1_F;
                protected internal int PI_F;
                internal int In_F;
                protected int P2_F;
                private int P3_F;
                public int P1__P { get; set; }
                protected internal int PI__P { get; set; }
                internal int In__P { get; set; }
                protected int P2__P { get; set; }
                private int P3__P { get; set; }
                public abstract int P1_AP { get; set; }
                protected internal abstract int PI_AP { get; set; }
                internal abstract int In_AP { get; set; }
                protected abstract int P2_AP { get; set; }
                // abstract members cannot be private
                public virtual int P1_VP { get; set; }
                protected internal virtual int PI_VP { get; set; }
                internal virtual int In_VP { get; set; }
                protected virtual int P2_VP { get; set; }
                // virtual members cannot be private
                public void P1__M() { }
                protected internal void PI__M() { }
                internal void In__M() { }
                protected void P2__M() { }
                private void P3__M() { }
                public abstract void P1_AM();
                protected internal abstract void PI_AM();
                internal abstract void In_AM();
                protected abstract void P2_AM();
                // abstract members cannot be private
                public virtual void P1_VM() { }
                protected internal virtual void PI_VM() { }
                internal virtual void In_VM() { }
                protected virtual void P2_VM() { }
                // virtual members cannot be private
            }
            public interface I {
                // static members
                // abstract and virtual modifiers are not allowed on fields
                public static int P1SF;
                protected internal static int PISF;
                internal static int InSF;
                protected static int P2SF;
                private static int P3SF;
                // In C# 11 (.NET 7), you can use abstract / virtual modifier on static properties of an interface
                public static int P1S_P { get; set; }
                protected internal static int PIS_P { get; set; }
                internal static int InS_P { get; set; }
                protected static int P2S_P { get; set; }
                private static int P3S_P { get; set; }
                public static abstract int P1SAP { get; set; }
                protected internal static abstract int PISAP { get; set; }
                internal static abstract int InSAP { get; set; }
                protected static abstract int P2SAP { get; set; }
                public static virtual int P1SVP { get; set; }
                protected internal static virtual int PISVP { get; set; }
                internal static virtual int InSVP { get; set; }
                protected static virtual int P2SVP { get; set; }
                // In C# 11 (.NET 7), you can use abstract / virtual modifier on static methods of an interface
                public static void P1S_M() { }
                protected internal static void PIS_M() { }
                internal static void InS_M() { }
                protected static void P2S_M() { }
                private static void P3S_M() { }
                public static abstract void P1SAM();
                protected internal static abstract void PISAM();
                internal static abstract void InSAM();
                protected static abstract void P2SAM();
                public static virtual void P1SVM() { }
                protected internal static virtual void PISVM() { }
                internal static virtual void InSVM() { }
                protected static virtual void P2SVM() { }
                // instance members
                // interfaces cannot have instance fields
                public int P1__P { get; set; }
                protected internal int PI__P { get; set; }
                internal int In__P { get; set; }
                protected int P2__P { get; set; }
                private int P3__P { get { return 0; } set { } }
                public abstract int P1_AP { get; set; }
                protected internal abstract int PI_AP { get; set; }
                internal abstract int In_AP { get; set; }
                protected abstract int P2_AP { get; set; }
                // abstract members cannot be private
                public virtual int P1_VP { get { return 0; } set { } }
                protected internal virtual int PI_VP { get { return 0; } set { } }
                internal virtual int In_VP { get { return 0; } set { } }
                protected virtual int P2_VP { get { return 0; } set { } }
                // virtual members cannot be private
                public void P1__M() { }
                protected internal void PI__M() { }
                internal void In__M() { }
                protected void P2__M() { }
                private void P3__M() { }
                public abstract void P1_AM();
                protected internal abstract void PI_AM();
                internal abstract void In_AM();
                protected abstract void P2_AM();
                // abstract members cannot be private
                public virtual void P1_VM() { }
                protected internal virtual void PI_VM() { }
                internal virtual void In_VM() { }
                protected virtual void P2_VM() { }
                // virtual members cannot be private
            }
            public class C : A {
                public override int P1_AP { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                protected override int P2_AP { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                protected internal override int PI_AP { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                internal override int In_AP { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                public override void P1_AM() => throw new NotImplementedException();
                protected override void P2_AM() => throw new NotImplementedException();
                protected internal override void PI_AM() => throw new NotImplementedException();
                internal override void In_AM() => throw new NotImplementedException();
            }
            public class D : I {
                static int I.P1SAP { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                static int I.PISAP { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                static int I.InSAP { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                static int I.P2SAP { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                int I.P1__P { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                int I.PI__P { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                int I.In__P { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                int I.P2__P { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                int I.P1_AP { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                int I.PI_AP { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                int I.In_AP { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                int I.P2_AP { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                static void I.InSAM() => throw new NotImplementedException();
                static void I.P1SAM() => throw new NotImplementedException();
                static void I.P2SAM() => throw new NotImplementedException();
                static void I.PISAM() => throw new NotImplementedException();
                void I.In_AM() => throw new NotImplementedException();
                void I.P1_AM() => throw new NotImplementedException();
                void I.P2_AM() => throw new NotImplementedException();
                void I.PI_AM() => throw new NotImplementedException();
            }
            public class E : A, I {
                public override int P1_AP { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                protected override int P2_AP { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                protected internal override int PI_AP { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                internal override int In_AP { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                public override void P1_AM() => throw new NotImplementedException();
                protected override void P2_AM() => throw new NotImplementedException();
                protected internal override void PI_AM() => throw new NotImplementedException();
                internal override void In_AM() => throw new NotImplementedException();
                static int I.P1SAP { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                static int I.PISAP { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                static int I.InSAP { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                static int I.P2SAP { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                int I.PI__P { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                int I.In__P { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                int I.P2__P { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                int I.PI_AP { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                int I.In_AP { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                int I.P2_AP { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
                static void I.P1SAM() => throw new NotImplementedException();
                static void I.InSAM() => throw new NotImplementedException();
                static void I.P2SAM() => throw new NotImplementedException();
                static void I.PISAM() => throw new NotImplementedException();
                void I.In_AM() => throw new NotImplementedException();
                void I.P2_AM() => throw new NotImplementedException();
                void I.PI_AM() => throw new NotImplementedException();
            }
            public static void Main(string[] args) {
                C c = new();
                D d = new();
                E e = new();
                Console.WriteLine(c);
                Console.WriteLine(d);
                Console.WriteLine(e);
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199

    相同点

    • abstract 和 virtual 关键字均不可修饰字段,但可以修饰属性和方法。
    • abstract 和 virtual 成员不能是 private 访问控制。
    • 对于每个成员,abstract 和 virtual 关键字只能使用一个,或者都不使用。
    • 均不可实例化。

    不同点

    • abstract 和 virtual 关键字均不可修饰抽象类的静态属性和方法,但自 C# 11 起,可以修饰接口的静态属性和方法。故而,接口的非 private 静态抽象属性和非 private 静态抽象方法均必须实现,否则编译不通过。
    • 在抽象类中,不被 abstract 修饰但省略了方法体的方法会令编译报错(要求添加 abstract,extern 或 partial 关键字)。而在接口中,不被 abstract 修饰但省略了方法体(不具有默认实现)的方法本身不会令编译报错,但必须提供这些方法的实现。
    • 在抽象类中,实现 abstract 属性和 abstract 方法时,需要使用 override 关键字。但在接口中,实现属性和方法时,不需要使用 override 关键字,即便该方法被 abstract 修饰。
    • 抽象类可以含有实例字段,但接口不可含有实例字段。
    • 抽象类的非 private 一般实例属性(非 abstract 且非 virtual)不要求必须实现,而接口的非 private 一般实例属性必须提供实现,否则编译不通过。
    • 抽象类的 private 一般实例属性及 virtual 的实例属性可以使用默认的 get accessor 和 set accessor,但接口的 private 一般实例属性及 virtual 的实例属性不可使用默认的 get accessor 和 set accessor。
    • 一个类只能继承单一的父类(包括抽象类),但可以实现多个接口。
    • 虽然在很多时候将抽象类和接口互换并不影响程序的行为,然而习惯上,抽象类及其子类描述一个实体,例如System.IO.Stream, Microsoft.AspNetCore.Mvc.Controller;而接口要求其实现类具有某个行为或性质,例如:System.Collections.IEnumerable, System.IComparable
  • 相关阅读:
    web入门(1)---6.10
    【论文笔记】—低光图像增强—Zero-reference—ZeroDCE—2020-CVPR
    GreenPlum/PostGreSQL表锁处理
    分布式事务最终一致性的方案
    AMBA总线协议之AHB学习记录(2)—ahb_bus的测试(附testbench代码)
    Kotlin 协程 (7/7篇) - 在Android中的使用
    Docker安装、卸载
    aspnetcore使用websocket实时更新商品信息
    【接口加密】接口加密的未来发展与应用场景
    二刷力扣--二叉树(2)
  • 原文地址:https://blog.csdn.net/COFACTOR/article/details/133951794