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>Lesson8_封装_静态类和静态构造函数</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,125 @@
|
||||
namespace Lesson8
|
||||
{
|
||||
#region 知识回顾
|
||||
class 类名
|
||||
{
|
||||
#region 特征——成员变量
|
||||
#endregion
|
||||
|
||||
#region 行为——成员方法
|
||||
#endregion
|
||||
|
||||
#region 初始化调用——构造方法
|
||||
#endregion
|
||||
|
||||
#region 释放时调用——析构方法
|
||||
#endregion
|
||||
|
||||
#region 保护成员变量——成员属性
|
||||
#endregion
|
||||
|
||||
#region 索引器
|
||||
#endregion
|
||||
|
||||
#region 静态成员
|
||||
#endregion
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region 知识点一 静态类
|
||||
//概念
|
||||
//用static修饰的类
|
||||
|
||||
//特点
|
||||
//只能包含静态成员
|
||||
//不能被实例化
|
||||
|
||||
//作用
|
||||
//1.将常用的静态成员写在静态类中 方便使用
|
||||
//2.静态类不能被实例化,更能体现工具类的 唯一性
|
||||
//比如 consle就是一个静态类
|
||||
|
||||
static class Tools
|
||||
{
|
||||
public static int testIndex = 0;//静态成员变量
|
||||
|
||||
public static void TestFun() { }
|
||||
public static int TestIndex
|
||||
{
|
||||
get { return testIndex; }
|
||||
set { testIndex = value; }
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region 知识点二 静态构造函数
|
||||
//概念
|
||||
//在构造函数加上 static 修饰
|
||||
|
||||
//特点
|
||||
//1.静态类和普通类都可以有
|
||||
//2.不能使用访问修饰符
|
||||
//3.不能有参数
|
||||
//4.只会自动调用依次
|
||||
|
||||
//作用
|
||||
//在静态构造函数中初始化
|
||||
|
||||
//1.静态类中的静态构造函数
|
||||
static class StaticClass
|
||||
{
|
||||
public static int testInt = 100;
|
||||
public static int testInt2 = 200;
|
||||
public static int testInt3;
|
||||
|
||||
static StaticClass()
|
||||
{
|
||||
Console.WriteLine("静态构造函数");
|
||||
testInt3 = 300;
|
||||
}
|
||||
}
|
||||
//2.普通类中的静态构造函数
|
||||
class NormalClass
|
||||
{
|
||||
public int testInt;
|
||||
public int testInt2 = 200;
|
||||
public static int testInt3;
|
||||
static NormalClass()
|
||||
{
|
||||
testInt3 = 300;
|
||||
Console.WriteLine("静态构造函数");
|
||||
}
|
||||
public NormalClass()
|
||||
{
|
||||
testInt = 100;
|
||||
testInt3 = 300;
|
||||
Console.WriteLine("构造函数");
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
|
||||
Console.WriteLine(StaticClass.testInt);
|
||||
|
||||
Console.WriteLine(StaticClass.testInt2);
|
||||
|
||||
Console.WriteLine(StaticClass.testInt3);
|
||||
|
||||
|
||||
Console.WriteLine(NormalClass.testInt3);
|
||||
NormalClass n1 = new NormalClass();
|
||||
Console.WriteLine(n1.testInt);
|
||||
Console.WriteLine(n1.testInt2);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user