Synchronization of old projects

Part1
This commit is contained in:
2025-09-30 16:46:01 +08:00
parent 116b65164b
commit 4e1548abe2
167 changed files with 8668 additions and 43 deletions
@@ -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
}
}
}