Synchronization of old projects
Part1
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>Lesson9_封装_拓展方法</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,117 @@
|
||||
namespace Lesson9
|
||||
{
|
||||
#region 知识回顾
|
||||
class 类名
|
||||
{
|
||||
#region 特征——成员变量
|
||||
#endregion
|
||||
|
||||
#region 行为——成员方法
|
||||
#endregion
|
||||
|
||||
#region 初始化调用——构造方法
|
||||
#endregion
|
||||
|
||||
#region 释放时调用——析构方法
|
||||
#endregion
|
||||
|
||||
#region 保护成员变量——成员属性
|
||||
#endregion
|
||||
|
||||
#region 索引器
|
||||
#endregion
|
||||
|
||||
#region 静态成员
|
||||
#endregion
|
||||
}
|
||||
#region 静态类和静态构造函数
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#region 知识点一 拓展方法基本概念
|
||||
//概念
|
||||
//为现有的非静态 变量类型 添加 新方法
|
||||
//作用
|
||||
//1.提高程序的拓展性
|
||||
//2.不需要在对象中重新写方法
|
||||
//3.不需要继承来添加方法(继承没学
|
||||
//4.为别人封装的类型写额外的方法
|
||||
//特点
|
||||
//1.一定是写在静态类中的
|
||||
//2.一定是静态函数
|
||||
//3.第一个参数为拓展目标
|
||||
//4.第一个参数用this修饰
|
||||
#endregion
|
||||
|
||||
#region 知识点二 基本语法
|
||||
//访问修饰符 static 返回值 函数名(this 拓展类名 参数名,参数类型 参数名,参数类型 参数名,.....)
|
||||
#endregion
|
||||
|
||||
#region 知识点三 实例
|
||||
static class Tools
|
||||
{
|
||||
//为int拓展一个成员方法
|
||||
//成员方法是需要实例化对象后才能使用的
|
||||
//value 代表 使用该方法的 实例化对象
|
||||
public static void SpeakValue(this int value)
|
||||
{
|
||||
//拓展方法的逻辑
|
||||
Console.WriteLine("拓展int方法" + value);
|
||||
}
|
||||
|
||||
public static void SpeakString(this string value,string value2,string value3)
|
||||
{
|
||||
Console.WriteLine("拓展的string方法");
|
||||
Console.WriteLine("调用方法的对象" + value);
|
||||
Console.WriteLine("传的参数" + value2+value3);
|
||||
}
|
||||
public static void Fun3(this Test t)//为test t拓展的Fun3
|
||||
{
|
||||
Console.WriteLine("tools为test扩展的方法3");
|
||||
}
|
||||
public static void Fun1(this Test t2)//同名了,在test类的实例化对象中无法调用这里的函数
|
||||
{
|
||||
Console.WriteLine("tools为test扩展的方法4");
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 知识点五 为自定义的类型拓展方法
|
||||
class Test
|
||||
{
|
||||
public int i = 10;
|
||||
public void Fun1()
|
||||
{
|
||||
Console.WriteLine("test的fun1");
|
||||
}
|
||||
public void Fun2()
|
||||
{
|
||||
Console.WriteLine("test的fun2");
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
#region 知识点四 使用
|
||||
|
||||
int i = 10;//i是实例化对象
|
||||
i.SpeakValue();//虽然需要一个参数,
|
||||
//但这只是一个规则,可以不写这个value
|
||||
//执行后是:拓展int方法10
|
||||
|
||||
|
||||
string str = "000";
|
||||
str.SpeakString("value2","value3");//鼠标停留在函数名上可以看到需要传哪些参数
|
||||
|
||||
|
||||
Test t = new Test();
|
||||
t.Fun3();//可以调用其他类中为其扩展的方法
|
||||
|
||||
Test t2 = new Test();
|
||||
t2.Fun1();//当两个类中有同名的方法,仅会调用本类中的方法
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user