From 577e25c7e84d18d3a2187b8ffc9114d0c4f836d1 Mon Sep 17 00:00:00 2001 From: bicijinlian Date: Wed, 30 Jun 2021 17:05:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- InterfaceStudy.Core/接口学习.md | 11 +++- InterfaceStudy.Core/接口新功能学习.md | 20 ++++++ InterfaceStudy.Examine/ClassA.cs | 26 ++++++++ InterfaceStudy.Examine/ClassB.cs | 16 +++++ InterfaceStudy.Examine/ClassC.cs | 16 +++++ InterfaceStudy.Examine/I.cs | 11 ++++ .../InterfaceStudy.Examine.csproj | 8 +++ InterfaceStudy.Examine/Program.cs | 17 +++++ InterfaceStudy.Examine/考题.md | 65 +++++++++++++++++++ InterfaceStudy.sln | 12 +++- 10 files changed, 196 insertions(+), 6 deletions(-) create mode 100644 InterfaceStudy.Core/接口新功能学习.md create mode 100644 InterfaceStudy.Examine/ClassA.cs create mode 100644 InterfaceStudy.Examine/ClassB.cs create mode 100644 InterfaceStudy.Examine/ClassC.cs create mode 100644 InterfaceStudy.Examine/I.cs create mode 100644 InterfaceStudy.Examine/InterfaceStudy.Examine.csproj create mode 100644 InterfaceStudy.Examine/Program.cs create mode 100644 InterfaceStudy.Examine/考题.md diff --git a/InterfaceStudy.Core/接口学习.md b/InterfaceStudy.Core/接口学习.md index 0ee3244..b778ce4 100644 --- a/InterfaceStudy.Core/接口学习.md +++ b/InterfaceStudy.Core/接口学习.md @@ -1,18 +1,23 @@ # 接口学习 +注意点:C#8.0对接口有较大改动,之前的教程不再适用。比如:接口可以有默认实现,可以有static静态对象。 + ## 接口定义 -> 接口是一种契约和规范;是指定一组对象的定义而非实现的引用类型;是C#实现多继承的机制;也是多态的一种常用实现方式。 ++ 微软:An interface is a reference type that defines a set of members. ++ 译文:接口是定义一组特定对象(属性、方法、索引器、委托与事件)的引用类型。从 C# 8.0(.NET Core 3.0及以上,但.net framework不支持)开始,接口可以为成员定义默认实现,也可以定义static方法,这些方法必须有一个实现。 ++ **组合式定义**:接口是包含一组非抽象类或结构必须实现的相关功能定义的引用类型;接口是一种编程契约和规范;也是C#实现多继承的机制;又是多态的一种常用实现方式。 ## 接口特点 1. 接口可以包含属性、方法、索引器、委托与事件等对象,但不能包含字段,也不能包含构造或析构函数(对象的创建与销毁,应该在实现类所关心的,接口只关心需要实现的功能约定) 1. 接口中的对象都是公开权限的,否则接口无意义:不能使用范围修饰符(public private internal protected) -1. 不能有 static abstract virtual override sealed 等违反接口意义的修饰符 -1. 接口只包含对象的定义,不能包含对象的实现,也不能直接实例化接口 +1. 不能有 static abstract virtual override sealed 等违反接口意义的修饰符,C#8.0之后static被允许。 +1. 接口只包含对象的定义,不能包含对象的实现,也不能直接实例化接口. C#8.0之后,可以有默认实现,可以直接调用接口静态方法。 1. 接口的实现类必须实现接口中的所有成员,除非实现类本身是抽象类(通过具体的可执行代码实现接口抽象成员的操作);接口实现子类中,可用new关键字隐藏父接口中的方法。 1. 接口可以多继承,可用来解决C#类的单继承问题 1. 接口名称一般都以“I”作为首字母,以与实现类区分 +1. 接口声明也适用于 “分部类”与“分部方法” ## 接口的隐式实现与显式实现 diff --git a/InterfaceStudy.Core/接口新功能学习.md b/InterfaceStudy.Core/接口新功能学习.md new file mode 100644 index 0000000..a5da5e0 --- /dev/null +++ b/InterfaceStudy.Core/接口新功能学习.md @@ -0,0 +1,20 @@ +# C#8 接口新功能学习 + +**.NET Core 3.0及以上版本** 和 **.NET Standard 2.1** 支持 C#8.0,详情参阅 [C#语言版本控制](https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/configure-language-version) + +C# 8的许多最新功能需要 .NET Core 3.x 中引入的库和运行时功能 ++ 默认接口实现需要使用 .NET Core 3.0 CLR 中的新功能 ++ 异步流需要使用新类型 System.IAsyncDisposable、System.Collections.Generic.IAsyncEnumerable 和 System.Collections.Generic.IAsyncEnumerator ++ 索引和范围需要使用新类型 System.Index 和 System.Range ++ 可为 null 的引用类型利用几个特性来提供更准确的警告。 这些特性是在 .NET Core 3.0 中添加的。 其他目标框架并未使用这些特性中的任何一种进行批注。 这意味着可为 null 的警告可能无法准确反映潜在问题。 + +## 接口新功能条件 ++ C# 8.0及以上 ++ **.NET Core3.0及以上** 或者 **.NET Standard 2.1** (.net framework不支持) ++ Visual Studio 2019 16.1 及以上 + +## 默认实现 + +## 静态方法支持 + +## \ No newline at end of file diff --git a/InterfaceStudy.Examine/ClassA.cs b/InterfaceStudy.Examine/ClassA.cs new file mode 100644 index 0000000..ad70613 --- /dev/null +++ b/InterfaceStudy.Examine/ClassA.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace InterfaceStudy.Examine +{ + public class ClassA : I + { + public int i = 1; + + public ClassA() + { + ++ i; + } + + void I.Print() + { + Console.WriteLine(i); + } + + public virtual void Print() + { + Console.WriteLine(++i); + } + } +} diff --git a/InterfaceStudy.Examine/ClassB.cs b/InterfaceStudy.Examine/ClassB.cs new file mode 100644 index 0000000..f47563b --- /dev/null +++ b/InterfaceStudy.Examine/ClassB.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace InterfaceStudy.Examine +{ + public class ClassB : ClassA, I + { + int i = -1; + + public override void Print() + { + Console.WriteLine(--i); + } + } +} diff --git a/InterfaceStudy.Examine/ClassC.cs b/InterfaceStudy.Examine/ClassC.cs new file mode 100644 index 0000000..a4ac1b0 --- /dev/null +++ b/InterfaceStudy.Examine/ClassC.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace InterfaceStudy.Examine +{ + public class ClassC : ClassA + { + int i = -1; + + public override void Print() + { + Console.WriteLine(--i); + } + } +} diff --git a/InterfaceStudy.Examine/I.cs b/InterfaceStudy.Examine/I.cs new file mode 100644 index 0000000..03145f0 --- /dev/null +++ b/InterfaceStudy.Examine/I.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace InterfaceStudy.Examine +{ + public interface I + { + void Print(); + } +} diff --git a/InterfaceStudy.Examine/InterfaceStudy.Examine.csproj b/InterfaceStudy.Examine/InterfaceStudy.Examine.csproj new file mode 100644 index 0000000..c73e0d1 --- /dev/null +++ b/InterfaceStudy.Examine/InterfaceStudy.Examine.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/InterfaceStudy.Examine/Program.cs b/InterfaceStudy.Examine/Program.cs new file mode 100644 index 0000000..b9ac647 --- /dev/null +++ b/InterfaceStudy.Examine/Program.cs @@ -0,0 +1,17 @@ +using System; + +namespace InterfaceStudy.Examine +{ + class Program + { + static void Main(string[] args) + { + I i; + i = new ClassB(); + i.Print(); + + i = new ClassC(); + i.Print(); + } + } +} diff --git a/InterfaceStudy.Examine/考题.md b/InterfaceStudy.Examine/考题.md new file mode 100644 index 0000000..5bca861 --- /dev/null +++ b/InterfaceStudy.Examine/考题.md @@ -0,0 +1,65 @@ +# 考题 + +不运行代码,20分钟内,给出程序输出结果(两个数字) + +## 请写出下面代码的输出 +``` csharp + public interface I + { + void Print(); + } + + public class ClassA : I + { + public int i = 1; + + public ClassA() + { + ++ i; + } + + void I.Print() + { + Console.WriteLine(i); + } + + public virtual void Print() + { + Console.WriteLine(++i); + } + } + + public class ClassB : ClassA, I + { + int i = -1; + + public override void Print() + { + Console.WriteLine(--i); + } + } + + public class ClassC : ClassA + { + int i = -1; + + public override void Print() + { + Console.WriteLine(--i); + } + } + + class Program + { + static void Main(string[] args) + { + I i; + i = new ClassB(); + i.Print(); + + i = new ClassC(); + i.Print(); + } + } + +``` \ No newline at end of file diff --git a/InterfaceStudy.sln b/InterfaceStudy.sln index edc2537..ce159ed 100644 --- a/InterfaceStudy.sln +++ b/InterfaceStudy.sln @@ -3,11 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.31410.357 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InterfaceStudy.Core", "InterfaceStudy.Core\InterfaceStudy.Core.csproj", "{2F771F8A-B472-485D-816A-BB5B5C828DE2}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InterfaceStudy.Core", "InterfaceStudy.Core\InterfaceStudy.Core.csproj", "{2F771F8A-B472-485D-816A-BB5B5C828DE2}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InterfaceStudy.App", "InterfaceStudy.App\InterfaceStudy.App.csproj", "{5578DC19-F9A7-4FE0-AB74-6A583A30E99B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InterfaceStudy.App", "InterfaceStudy.App\InterfaceStudy.App.csproj", "{5578DC19-F9A7-4FE0-AB74-6A583A30E99B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InterfaceStudy.Test", "InterfaceStudy.Test\InterfaceStudy.Test.csproj", "{B7B58056-A189-4628-BE74-A16165B04BF7}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InterfaceStudy.Test", "InterfaceStudy.Test\InterfaceStudy.Test.csproj", "{B7B58056-A189-4628-BE74-A16165B04BF7}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InterfaceStudy.Examine", "InterfaceStudy.Examine\InterfaceStudy.Examine.csproj", "{EF414F01-F39F-42BC-93E5-6B32B7EA3872}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -27,6 +29,10 @@ Global {B7B58056-A189-4628-BE74-A16165B04BF7}.Debug|Any CPU.Build.0 = Debug|Any CPU {B7B58056-A189-4628-BE74-A16165B04BF7}.Release|Any CPU.ActiveCfg = Release|Any CPU {B7B58056-A189-4628-BE74-A16165B04BF7}.Release|Any CPU.Build.0 = Release|Any CPU + {EF414F01-F39F-42BC-93E5-6B32B7EA3872}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EF414F01-F39F-42BC-93E5-6B32B7EA3872}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EF414F01-F39F-42BC-93E5-6B32B7EA3872}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EF414F01-F39F-42BC-93E5-6B32B7EA3872}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE