Synchronization of old projects
Part1
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
Console.WriteLine("Hello, World!");
|
||||
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>C_核心</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,91 @@
|
||||
namespace Lesson1_类和对象
|
||||
{
|
||||
#region 知识回顾 面向对象概念回顾
|
||||
//万物皆对象
|
||||
//用程序来抽象(形容)对象
|
||||
//用面向对象的思路来编程
|
||||
#endregion
|
||||
|
||||
#region 知识点一 什么是类
|
||||
//基本概念
|
||||
//具有相同特征
|
||||
//具有相同行为
|
||||
//一类事务的抽象
|
||||
//类是对象的模板
|
||||
//可以通过类创建出对象来
|
||||
//类的关键词:class
|
||||
#endregion
|
||||
|
||||
#region 知识点二 类声明在哪里
|
||||
//一般声明在namespace里声明(还有内部类不在此处
|
||||
#endregion
|
||||
|
||||
#region 知识点三 类声明的语法
|
||||
//命名:
|
||||
//class前可以加访问修饰符,类名不建议使用中文,帕斯卡命名法
|
||||
//注意:同一个语句块中的不同类 不能重名
|
||||
class 类名
|
||||
{
|
||||
//形容一类对象的:
|
||||
//特征-成员变量
|
||||
//保护特征-成员属性
|
||||
|
||||
//构造函数和析构函数
|
||||
//索引器
|
||||
//运算符重载
|
||||
//静态成员
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 知识点四 类声明实例
|
||||
class Human//用来形容人类的
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
class Machine//用来形容机器的
|
||||
{
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
|
||||
#region 知识点五 什么是(类)对象
|
||||
//基本概念
|
||||
// 类的声明 和 类对象(变量)声明是两个概念
|
||||
// 类的声明 类似 枚举 和 结构体的声明 类的声明相当于声明了一个自定义变量类型
|
||||
// 而对象 是类创建出来的
|
||||
// 相当于声明一个指定类的变量
|
||||
// 类创建对象的过程一般称为实例化对象
|
||||
// 类对象 都是引用类型的(还有数组和string
|
||||
#endregion
|
||||
|
||||
#region 知识点六 实例化对象基本语法
|
||||
//类名 变量名;
|
||||
//类名 变量名 = null; null代表空
|
||||
//类名 变量名 = new 类名(); 会调用类的构造函数
|
||||
#endregion
|
||||
|
||||
#region 知识点七 实例化对象
|
||||
Human h;//在栈上开辟一个空间 来 存储 堆空间 的 内存地址 ,但是现在没放所以栈空间内容是空的
|
||||
Human h2 = null;//和上面一样
|
||||
Human h3 = new Human();//在栈上开辟一个空间来存储堆空间的内存地址,但堆的内容是空的
|
||||
Human h4 = new Human();//假如构造函数中有内容,此刻要注意!
|
||||
//虽然和上面一样都是Human()实例化出来的对象,但其中的内容并不共享
|
||||
|
||||
|
||||
//总结:
|
||||
//类对象和类的声明是两个东西
|
||||
//类的声明是声明对象的模板 用来(抽象)形容实例的
|
||||
//类对象的声明是 用来表示实际对象个体的
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace Lesson1_类和对象练习
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,118 @@
|
||||
namespace Lesson2_成员变量和访问修饰符
|
||||
{
|
||||
#region 知识回顾
|
||||
// 类和对象
|
||||
// 声明类
|
||||
//class 类名
|
||||
//{
|
||||
// //形容一类对象的:
|
||||
// //特征-成员变量//////////////////////////////////本课重点
|
||||
// //保护特征-成员属性
|
||||
|
||||
// //构造函数和析构函数
|
||||
// //索引器
|
||||
// //运算符重载
|
||||
// //静态成员
|
||||
//}
|
||||
// 实例化对象
|
||||
// 类名 变量名;
|
||||
// 类名 变量名 = null;
|
||||
// 类名 变量名 = new 类名();
|
||||
#endregion
|
||||
|
||||
#region 知识点一 成员变量
|
||||
//基本规则
|
||||
//1.声明在类语句块中
|
||||
//2.用来描述对象的特征
|
||||
//3.可以是任意变量类型
|
||||
//4.数量不做限制
|
||||
//5.是否赋值根据需求决定
|
||||
|
||||
//性别
|
||||
enum E_Sex
|
||||
{
|
||||
male = 0,
|
||||
female = 1
|
||||
}
|
||||
//位置
|
||||
struct Position
|
||||
{
|
||||
public int x;
|
||||
public int y;
|
||||
public Position(int x, int y)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
}
|
||||
//宠物
|
||||
class Pet
|
||||
{
|
||||
|
||||
}
|
||||
//人
|
||||
class Human
|
||||
{
|
||||
//特征-成员变量
|
||||
//姓名
|
||||
public string name = "HK";//可以初始化(但意义不大),结构体中不可以
|
||||
//年龄
|
||||
public int age;
|
||||
//性别
|
||||
public E_Sex sex;
|
||||
//伴侣
|
||||
public Human girlfriend;
|
||||
//类中这么写是可以的 因为类会默认为null
|
||||
//结构体不能这么写 因为结构体必须初始化
|
||||
//注意!
|
||||
//在类中声明一个类名相同的成员变量时
|
||||
//不能对其进行实例化(可以为null,但是不能直接初始化new一个,会造成死循环)
|
||||
|
||||
public Human[] friend;
|
||||
|
||||
public Position pos;
|
||||
|
||||
public Pet pet;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 知识点二 访问修饰符
|
||||
// public 公共的 自己(内部)和别人(外部)都能访问和使用
|
||||
// private 私有的 自己(内部)才可以访问和使用 不写 默认为此private
|
||||
// protected 保护的 自己(内部)和子类才能访问和使用
|
||||
// 目前决定类内部的成员 的 访问权限
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region 知识点四
|
||||
#endregion
|
||||
|
||||
#region 知识点五
|
||||
#endregion
|
||||
|
||||
#region 知识点六
|
||||
#endregion
|
||||
|
||||
#region 知识点七
|
||||
#endregion
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
#region 知识点三
|
||||
#endregion
|
||||
|
||||
Human h = new Human();
|
||||
//值类型来说 数字类型的默认值都是0 bool类型都是false
|
||||
//引用类型的 null
|
||||
//看默认值的方法
|
||||
Console.WriteLine(default(int));
|
||||
Console.WriteLine(default(Human));//打印null看不到
|
||||
|
||||
h.age = 10;
|
||||
Console.WriteLine(h.age);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,68 @@
|
||||
namespace Lesson2_成员变量和访问修饰符练习
|
||||
{
|
||||
class Person
|
||||
{
|
||||
public int age,height;
|
||||
public string name,address;
|
||||
}
|
||||
class Student
|
||||
{
|
||||
public string name;
|
||||
public int number, age;
|
||||
public Student deskmate;
|
||||
void Study()
|
||||
{
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
class Cla
|
||||
{
|
||||
public string Major;
|
||||
public int TeacherCapacity,StudentCapacity;
|
||||
|
||||
}
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
//Console.WriteLine("Hello, World!");
|
||||
//Person p = new Person();
|
||||
//Person p2 = new Person();
|
||||
//p.age = 10;
|
||||
//p2.age = 20;
|
||||
////不同对象的存储空间不同,所以值不会相同
|
||||
//Console.WriteLine(p.age);
|
||||
//Console.WriteLine(p2.age);
|
||||
|
||||
Person p = new Person();
|
||||
p.age = 10;
|
||||
Person p2 = p;
|
||||
p2.age = 20;
|
||||
//p2指向了p的内存地址,共享一块地址,所以p2改了,p也会改为20
|
||||
Console.WriteLine(p.age);
|
||||
Console.WriteLine(p2.age);
|
||||
|
||||
//Student s = new Student();
|
||||
//s.age = 10;
|
||||
//int age = s.age;
|
||||
//age = 20;
|
||||
////因为age是值类型,是一个新的空间,所以s.age不受影响
|
||||
//Console.WriteLine(s.age);
|
||||
//Console.WriteLine(age);
|
||||
|
||||
Student s = new Student();
|
||||
s.deskmate = new Student();
|
||||
s.deskmate.age = 10;
|
||||
Student s2 = s.deskmate;
|
||||
s2.age = 20;
|
||||
//s.deskmate虽然new了空间在后面被指向了s2的地址
|
||||
//所以s.deskmate的地址是和s2共享的 s.deskmate就是s2
|
||||
//s2.age变了s.deskmate.age也会变
|
||||
Console.WriteLine(s.deskmate.age);
|
||||
Console.WriteLine(s2.age);
|
||||
|
||||
Cla c = new Cla();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>Lesson3_封装_成员方法</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,77 @@
|
||||
namespace Lesson3_封装_成员方法
|
||||
{
|
||||
#region 知识点一 成员方法声明
|
||||
//基本概念
|
||||
//1.声明在类语句块中
|
||||
//2.是用来描述对象的行为的
|
||||
//3.规则和函数声明规则相同
|
||||
//4.收到访问修饰符规则影响
|
||||
//5.返回值参数不做限制
|
||||
//6.方法数量不做限制
|
||||
|
||||
//注意:
|
||||
//1.成员方法不要加static关键字
|
||||
//2.成员方法 必须实例化出对象 再通过对象来使用 详单与该对象执行了某个行为
|
||||
//3.成员方法 收到访问修饰符限制
|
||||
|
||||
class Person
|
||||
{
|
||||
public string name;
|
||||
public int age;
|
||||
|
||||
public void Speak(string str)
|
||||
{
|
||||
Console.WriteLine("{0}说{1}",name,str);
|
||||
}
|
||||
|
||||
public bool IsAdult()
|
||||
{
|
||||
return age >= 18;
|
||||
}
|
||||
public Person[] friend;
|
||||
/// <summary>
|
||||
/// 加朋友的方法
|
||||
/// </summary>
|
||||
/// <param name="p">p是传入的新朋友</param>
|
||||
public void AddFriend(Person p)
|
||||
{
|
||||
if (friend == null)
|
||||
{
|
||||
friend = new Person[] { p };
|
||||
}
|
||||
else
|
||||
{
|
||||
Person[] temp = new Person[friend.Length];
|
||||
for (int i = 0; i < friend.Length; i++)
|
||||
{
|
||||
temp[i] = friend[i];
|
||||
}
|
||||
temp[temp.Length -1] = p;
|
||||
friend = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
#region 知识点二 成员方法的使用
|
||||
Person p = new Person();
|
||||
p.name = "HK";
|
||||
p.age = 18;
|
||||
p.Speak("hello");
|
||||
Console.WriteLine(p.IsAdult() ? "成年了" : "没成年");
|
||||
Person p2 = new Person();
|
||||
p2.name = "KH";
|
||||
p2.age = 18;
|
||||
p.AddFriend(p2);
|
||||
for (int i = 0; i < p.friend.Length; i++)
|
||||
{
|
||||
Console.WriteLine("{0}是{1}的朋友",p.friend[i].name,p.name);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>Lesson3_封装_成员方法练习</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,66 @@
|
||||
namespace Lesson3_封装_成员方法练习
|
||||
{
|
||||
enum E_Occupation
|
||||
{
|
||||
Student = 0,
|
||||
Teacher = 1,
|
||||
}
|
||||
class Person
|
||||
{
|
||||
public string name;
|
||||
public string age;
|
||||
|
||||
|
||||
public void Speak(string str)
|
||||
{
|
||||
Console.WriteLine("{0}说了{1}", name, str);
|
||||
}
|
||||
public void Walk()
|
||||
{
|
||||
Console.WriteLine("{0}走了路", name);
|
||||
}
|
||||
public void Eat(Food food)
|
||||
{
|
||||
Console.WriteLine("{0}吃了{1},共计{2}卡路里", name, food.name, food.Calorie);
|
||||
}
|
||||
}
|
||||
class Student
|
||||
{
|
||||
public string name;
|
||||
public void Study()
|
||||
{
|
||||
Console.WriteLine("{0}学了习", name);
|
||||
}
|
||||
public void Eat(Food food)
|
||||
{
|
||||
Console.WriteLine("{0}吃了{1},共计{2}卡路里", name, food.name, food.Calorie);
|
||||
}
|
||||
}
|
||||
class Food
|
||||
{
|
||||
public string name;
|
||||
public int Calorie;
|
||||
|
||||
}
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Person p = new Person();
|
||||
p.name = "Test";
|
||||
p.Speak("hello");
|
||||
p.Walk();
|
||||
//p.Eat();
|
||||
Student s = new Student();
|
||||
s.name = "Test2";
|
||||
s.Study();
|
||||
//s.Eat();
|
||||
|
||||
Food bread = new Food();
|
||||
bread.name = "Bread";
|
||||
bread.Calorie = 200;
|
||||
s.Eat(bread);
|
||||
p.Eat(bread);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>Lesson4_封装_构造函数和析构函数</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,165 @@
|
||||
namespace Lesson4
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
#region 知识点一 构造函数
|
||||
//基本概念
|
||||
//在实例化对象时 会调用的用于初始化的函数
|
||||
//如果不屑 默认存在一个无参的构造函数
|
||||
|
||||
//构造函数的写法
|
||||
//1.没有返回值
|
||||
//2.函数名和类名必须相同
|
||||
//3.没有特殊需求是 一般都是public
|
||||
//class Person
|
||||
//{
|
||||
// public string name;
|
||||
// public int age;
|
||||
|
||||
// //类允许自己声明无参构造函数(结构体不允许
|
||||
// public Person()
|
||||
// {
|
||||
// name = "HK";
|
||||
// age = 18;
|
||||
// }
|
||||
// public Person(string name, int age)//当使用Person p2 = new Person("HK",19);时调用的就是这里的函数
|
||||
// {
|
||||
// this.name = name;//将 外部 传进来的name和age 传给 类 下面的name和age
|
||||
// this.age = age;
|
||||
// }
|
||||
// //此时有参构造函数已经写了,如果我没写上面的无参构造函数,原本不写就默认有的无参构造函数会无效
|
||||
//}
|
||||
|
||||
//4.构造函数可以被重载
|
||||
//5.this代表当前调用该函数的对象自己
|
||||
|
||||
//注意:
|
||||
//如果不自己实现无参构造函数而实现了有参构造函数
|
||||
//会失去默认的无参构造函数
|
||||
#endregion
|
||||
|
||||
#region 知识点二 构造函数特殊写法
|
||||
//可以通过this 宠用构造函数代码
|
||||
//访问修饰符 构造函数名(参数列表):this(参数1,参数2.....)
|
||||
//class Person
|
||||
//{
|
||||
// public string name;
|
||||
// public int age;
|
||||
|
||||
// public Person()
|
||||
// {
|
||||
// name = "HK";
|
||||
// age = 18;
|
||||
// }
|
||||
// public Person(string name)
|
||||
// {
|
||||
// this.name = name;
|
||||
// }
|
||||
// public Person(int age)
|
||||
// {
|
||||
// this.age = age;
|
||||
// }
|
||||
// public Person(string name, int age): this(name) //写了this()
|
||||
// //就先调用无参构造函数(类似默认赋值)
|
||||
// //this()中如果是空的
|
||||
// //直接执行本有参构造函数
|
||||
// //this()中如果写了name,就会调用只有一个name的有参构造函数
|
||||
// //然后再执行本有参构造函数
|
||||
// {
|
||||
// Console.WriteLine("Person两个参数构造函数调用");
|
||||
// }
|
||||
//}
|
||||
#endregion
|
||||
|
||||
#region 知识点三 析构函数(了解
|
||||
//基本概念
|
||||
//当引用类型的堆内存被回收时,会调用该函数
|
||||
//对于需要手动管理内存的语言(比如C++),需要在析构函数中做一些垃圾回收处理
|
||||
//但是C#中存在自动垃圾回收机制GC
|
||||
//所以我们几乎不会怎么使用析构函数。除非你想在某一个对象被垃圾回收时,做一些特殊处理
|
||||
//注意:
|
||||
//在Unity开发中析构函数几乎不会使用,所以该知识点只做了解即可
|
||||
|
||||
//基本语法
|
||||
//~类名()
|
||||
//{
|
||||
//}
|
||||
|
||||
class Person
|
||||
{
|
||||
public string name;
|
||||
public int age;
|
||||
|
||||
public Person()
|
||||
{
|
||||
name = "HK";
|
||||
age = 18;
|
||||
}
|
||||
public Person(string name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
public Person(int age)
|
||||
{
|
||||
this.age = age;
|
||||
}
|
||||
public Person(string name, int age) : this(name)
|
||||
{
|
||||
Console.WriteLine("Person两个参数构造函数调用");
|
||||
}
|
||||
//引用类型堆内存被回收时才会执行
|
||||
~Person() { }//析构函数
|
||||
//当垃圾 真正被回收时 才会被调用
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 知识点四 垃圾回收机制
|
||||
//垃圾回收,英文简写GC(Garbage Collector)
|
||||
//垃圾回收的过程是在遍历堆(Heep)上动态分配的所有对象
|
||||
//通过识别他们是否被引用来确定哪些对象时垃圾,哪些对象仍要被使用
|
||||
//所谓的垃圾就是没有被任何变量,对象引用的内容
|
||||
//垃圾就需要被回收释放
|
||||
|
||||
//垃圾回收有很多种算法,比如
|
||||
//引用计数(Reference Counting)
|
||||
//标记清除(Mark Sweep)
|
||||
//标记整理(Mark Compact)
|
||||
//复制集合(Copy Collection)
|
||||
|
||||
//注意:
|
||||
//GC只负责堆(Heap)内存垃圾的回收
|
||||
//引用类型都是存在堆(Heap)中的,所以他的分配和释放都通过垃圾回收机制来管理
|
||||
|
||||
//栈(Stack)上的内存是由系统自动管理的
|
||||
//值类型是在栈(Stack)中分配内存的,他们有自己的声明周期,不用对他们进行管理,会自动分配和释放
|
||||
|
||||
//C#中的内存回收机制的大概原理
|
||||
//0代内存 1代内存 2代内存
|
||||
//代的概念:
|
||||
//代是垃圾回收机制使用的一种算法(分代算法)
|
||||
//新分配的对象都会被配置在第0代内存中
|
||||
//每次分配都可能会进行垃圾回收以及内存释放(0代内存满时)
|
||||
|
||||
//在依次内存回收过程开始时,垃圾回收器会认为堆中全是垃圾,会进行以下两步
|
||||
//1.标记对象 从根(静态字段、方法参数)开始检查引用对象,标记后为可达对象,未标记为不可达对象
|
||||
// 不可达对象就被认为是垃圾
|
||||
//2.搬迁对象压缩堆(挂起执行托管代码线程)释放未标记的对象 搬迁可达对象 修改引用地址
|
||||
|
||||
//大对象总被人为是第二代内存
|
||||
//不会对大对象进行搬迁压缩 85000字节(83kb)以上的对象为大对象
|
||||
|
||||
#endregion
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
Person p = new Person();
|
||||
Console.WriteLine(p.age);//自己默认的无参构造函数,数值为18
|
||||
Person p2 = new Person("HK",19);
|
||||
Console.WriteLine(p2.age);//上面实例化的19
|
||||
|
||||
GC.Collect();//手动回收垃圾
|
||||
//一般都是在加载或场景切换时才使用
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>Lesson4_封装_构造函数和析构函数练习</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,101 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Lesson4_封装_构造函数和析构函数练习
|
||||
{
|
||||
class Person
|
||||
{
|
||||
public string name;
|
||||
public int age;
|
||||
public Person(string name, int age)
|
||||
{
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
}
|
||||
class Clas
|
||||
{
|
||||
public string name, major;
|
||||
Person[] student;
|
||||
public Clas(string name, string major, Person[] student)
|
||||
{
|
||||
this.name = name;
|
||||
this.major = major;
|
||||
this.student = student;
|
||||
}
|
||||
public void PrintInfo(Person[] student)
|
||||
{
|
||||
Console.WriteLine("{0}班级中学生信息",name);
|
||||
for (int i = 0; i < student.Length; i++)
|
||||
{
|
||||
Console.WriteLine("student的姓名:" + student[i].name + "student的年龄:" + student[i].age);
|
||||
}
|
||||
}
|
||||
}
|
||||
class Ticket
|
||||
{
|
||||
public uint distant;
|
||||
|
||||
public Ticket()
|
||||
{
|
||||
distant = 0;
|
||||
}
|
||||
public Ticket(uint distant)
|
||||
{
|
||||
this.distant = distant;
|
||||
}
|
||||
public void GetDistant()
|
||||
{
|
||||
Console.WriteLine(distant);
|
||||
}
|
||||
public void GetPrice()
|
||||
{
|
||||
double price = 0d;
|
||||
if (distant <= 100)
|
||||
{
|
||||
price = distant;
|
||||
}
|
||||
else if (distant <= 200)
|
||||
{
|
||||
price = Convert.ToDouble(distant * 0.95);
|
||||
}
|
||||
else if (distant <= 300)
|
||||
{
|
||||
price = Convert.ToDouble(distant * 0.9);
|
||||
}
|
||||
else
|
||||
{
|
||||
price = Convert.ToDouble(distant * 0.8);
|
||||
}
|
||||
Console.WriteLine(price);
|
||||
|
||||
}
|
||||
}
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Person p1 = new Person("张三", 3);
|
||||
Person p2 = new Person("李四", 4);
|
||||
|
||||
Console.WriteLine("P1姓名:" + p1.name + "P1年龄" + p1.age);
|
||||
Console.WriteLine("P2姓名:" + p2.name + "P2年龄" + p2.age);
|
||||
Person[] student = { p1, p2 };
|
||||
Console.WriteLine("把P1和P2放到Person的student之后");
|
||||
Console.WriteLine("student[0]的姓名:"+ student[0].name+ "student[0]的年龄:" + student[0].age);
|
||||
Console.WriteLine("student[1]的姓名:" + student[1].name + "student[1]的年龄:" + student[1].age);
|
||||
|
||||
|
||||
Clas c1 = new Clas("三年一班","计算机",student);
|
||||
c1.PrintInfo(student);
|
||||
|
||||
Ticket t = new Ticket();
|
||||
Console.Write("默认情况下的距离:");
|
||||
t.GetDistant();
|
||||
Console.Write("赋值初始化后的距离:");
|
||||
Ticket t2 = new Ticket(300);
|
||||
t2.GetDistant();
|
||||
Console.Write("赋值初始化后的价格计算:");
|
||||
t2.GetPrice();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>Lesson5_封装_成员属性</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,165 @@
|
||||
namespace Lesson5
|
||||
{
|
||||
#region 知识回顾
|
||||
//class Person
|
||||
//{
|
||||
// //特征——成员变量
|
||||
// private int age;
|
||||
// private string name;
|
||||
// //金钱
|
||||
// private int money;
|
||||
// //行为——成员方法
|
||||
// public void Speak()
|
||||
// {
|
||||
|
||||
// }
|
||||
// //初始化调用——构造函数
|
||||
// public Person(int age) { }
|
||||
// public Person(int age, string name) { }
|
||||
// //释放时调用——析构函数
|
||||
// ~Person() { }
|
||||
//}
|
||||
#endregion
|
||||
|
||||
#region 知识点一 成员属性的基本概念
|
||||
//基本概念
|
||||
//1.用于保护乘员变量
|
||||
//2.称为成员属性获取和赋值添加逻辑处理
|
||||
//3.解决3p的局限性
|
||||
// public_内外访问
|
||||
// private_内外访问
|
||||
// protected_内部和子类访问
|
||||
// 属性可以让成员变量在外部
|
||||
// 只能获取 不能修改 或者 只能修改 不能获取
|
||||
#endregion
|
||||
|
||||
#region 知识点二 成员属性的基本语法
|
||||
// 访问修饰符 属性类型 属性名
|
||||
// {
|
||||
// get{}
|
||||
// set{}
|
||||
// }
|
||||
class Person
|
||||
{
|
||||
private string name;
|
||||
private int age;
|
||||
private int money;
|
||||
private bool sex;
|
||||
|
||||
public Person()
|
||||
{
|
||||
sex = true;
|
||||
}
|
||||
|
||||
//属性一般用帕斯卡命名法
|
||||
public string Name
|
||||
{
|
||||
get//必须要有一个返回值
|
||||
{
|
||||
//可以在返回之前添加一些逻辑规则
|
||||
//意味着 这个属性可以获取的内容
|
||||
return name;
|
||||
}
|
||||
set
|
||||
{
|
||||
//可以在设置之前添加一些逻辑
|
||||
// value关键字 用于 表示外部传入的值
|
||||
name = value;
|
||||
}
|
||||
}
|
||||
//public int Money
|
||||
//{
|
||||
// get
|
||||
// {
|
||||
// return Value + 5;//通过加减来对数据进行加密
|
||||
// }
|
||||
// set
|
||||
// {
|
||||
// money = value - 5;
|
||||
// }
|
||||
//}
|
||||
public void TrueGetMoney()
|
||||
{
|
||||
Console.WriteLine(money);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 知识点四 成员属性中 get和set钱可以加访问修饰符
|
||||
// 注意
|
||||
// 1.默认不加 会使用属性声明时的访问权限
|
||||
// 2.记得访问修饰符要地狱属性的访问权限
|
||||
// 3.不能让get和set的访问权限都低于属性的权限
|
||||
public int Money
|
||||
{
|
||||
get
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value < 0)
|
||||
{
|
||||
Console.WriteLine("钱不能少于0,被设为0");
|
||||
value = 0;
|
||||
}
|
||||
money = value;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region 知识点五 get和set可以只有一个
|
||||
public bool Sex
|
||||
{
|
||||
get
|
||||
{
|
||||
return sex;//可以得不能改
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 知识点六 自动属性
|
||||
//作用:外部能得不能改的特征
|
||||
//如果类中有一个特征时希望外部能得到不能改 又没什么特殊处理
|
||||
//那么可以直接使用自动属性
|
||||
|
||||
public float Height//没有height成员变量,但是用Height属性来代替了成员变量(
|
||||
//自动生成一个地址来存放成员变量,我们也不知道成员变量名是什么,反正用这个属性包裹起来了
|
||||
//作用是
|
||||
//可以在get和set前设置访问修饰符,直接修改 设置和访问成员变量 的权限不用再去声明一个变量在写多几行代码
|
||||
//但是get和set中没法对数据进行处理(加密等)
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
#region 知识点三 成员属性的使用
|
||||
Person p = new Person();
|
||||
p.Name = "Test";
|
||||
Console.WriteLine(p.Name);
|
||||
p.Money = 100;
|
||||
Console.WriteLine(p.Money);
|
||||
p.TrueGetMoney();
|
||||
p.Money = -100;
|
||||
p.TrueGetMoney();
|
||||
Console.WriteLine(p.Sex);//只能得,不能p.Sex = False来改
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
//总结
|
||||
//1.成员属性概念:一般是用来保护成员变量的
|
||||
//2.成员属性的使用和变量一样 外部用对象点出
|
||||
//3.get中需要return内容;set中用value表示传入的内容
|
||||
//4.get和set语句块中可以加逻辑处理
|
||||
//5.get和set可以加访问修饰符,但是要按照一定的规则进行添加
|
||||
//6.get和set可以只有一个
|
||||
//7.自动属性是属性语句块中只有get和set,一般用于 外部能得不饿能改这种情况
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>Lesson5_封装_成员属性练习</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,129 @@
|
||||
using System.Runtime.Intrinsics.Arm;
|
||||
|
||||
namespace Lesson5_封装_成员属性练习
|
||||
{
|
||||
class Student
|
||||
{
|
||||
private int age,csharpscore,unityscore;
|
||||
private string sex;
|
||||
public Student() { }
|
||||
public Student(string name,int age,string sex,int csharp,int unity)
|
||||
{
|
||||
this.Name = name;
|
||||
this.Sex = sex;
|
||||
this.Age = age;
|
||||
this.CsharpScore = csharp;
|
||||
this.UnityScore = unity;
|
||||
}
|
||||
public string Name
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
public string Sex
|
||||
{
|
||||
get { return sex; }
|
||||
set
|
||||
{
|
||||
if (value == "男" || value == "女")
|
||||
{
|
||||
sex = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
sex = "不男不女";
|
||||
}
|
||||
}
|
||||
}
|
||||
public int Age
|
||||
{
|
||||
get
|
||||
{
|
||||
return age;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value < 0)
|
||||
{
|
||||
age = 0;
|
||||
Console.WriteLine("年龄不在范围内");
|
||||
}else if (value > 150)
|
||||
{
|
||||
age = 150;
|
||||
Console.WriteLine("年龄不在范围内");
|
||||
}else
|
||||
{
|
||||
age = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
public int CsharpScore
|
||||
{
|
||||
get
|
||||
{
|
||||
return csharpscore;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value < 0 || value > 100)
|
||||
{
|
||||
csharpscore = 0;
|
||||
Console.WriteLine("分数不在范围内");
|
||||
}
|
||||
else
|
||||
{
|
||||
csharpscore = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
public int UnityScore
|
||||
{
|
||||
get
|
||||
{
|
||||
return unityscore;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value < 0 || value > 100)
|
||||
{
|
||||
unityscore = 0;
|
||||
Console.WriteLine("分数不在范围内");
|
||||
}
|
||||
else
|
||||
{
|
||||
unityscore = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void Greet()
|
||||
{
|
||||
Console.WriteLine("我叫{0}, 今年{1}岁,是{2}生", Name, Age, Sex);
|
||||
}
|
||||
public void ShowTotalScore()
|
||||
{
|
||||
int total = CsharpScore + UnityScore;
|
||||
float avg = total / 2f;
|
||||
Console.WriteLine("你的总分是:"+total+"平均分是:"+avg);
|
||||
}
|
||||
}
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Test1:");
|
||||
Student s1 = new Student();
|
||||
s1.Name = "Test1";
|
||||
s1.Sex = "男";
|
||||
s1.Age = 123;
|
||||
s1.CsharpScore = 60;
|
||||
s1.UnityScore = 60;
|
||||
s1.Greet();
|
||||
s1.ShowTotalScore();
|
||||
Console.WriteLine("Test2:");
|
||||
Student s2 = new Student("test2",666,"超人",60,666);
|
||||
s2.Greet();
|
||||
s2.ShowTotalScore();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>Lesson6_封装_索引器</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,130 @@
|
||||
namespace Lesson6
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
#region 知识回顾
|
||||
class 类名
|
||||
{
|
||||
#region 特征——成员变量
|
||||
#endregion
|
||||
|
||||
#region 行为——成员方法
|
||||
#endregion
|
||||
|
||||
#region 初始化调用——构造方法
|
||||
#endregion
|
||||
|
||||
#region 释放时调用——析构方法
|
||||
#endregion
|
||||
|
||||
#region 保护成员变量——成员属性
|
||||
#endregion
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 知识点一 索引器的基本概念
|
||||
//基本概念
|
||||
//让对象可以像数组一样通过索引器访问其中元素,使程序看起来更只管,更容易编写
|
||||
//(不用数组了)
|
||||
#endregion
|
||||
|
||||
#region 知识点二 索引器基本语法
|
||||
//访问修饰符 返回值 this[参数类型 参数名,参数类型 参数名......]
|
||||
//{
|
||||
// 内部的写法可规则和索引器相同
|
||||
// get{}
|
||||
// set{}
|
||||
//}
|
||||
class Person
|
||||
{
|
||||
private string name;
|
||||
private int age;
|
||||
private Person[] friends;
|
||||
private int[,] array;
|
||||
|
||||
#region 知识点五 索引器可以重载
|
||||
//重载的概念是————函数名相同 但 参数类型、数量、顺序不同
|
||||
#endregion
|
||||
public int this[int i, int j]
|
||||
{
|
||||
get
|
||||
{
|
||||
return array[i, j];
|
||||
}
|
||||
set
|
||||
{
|
||||
array[i, j] = value;
|
||||
}
|
||||
}
|
||||
public string this[string str]
|
||||
{
|
||||
get
|
||||
{
|
||||
switch (str)
|
||||
{
|
||||
case "name":
|
||||
return this.name;
|
||||
case "age":
|
||||
return this.age.ToString();
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
set
|
||||
{
|
||||
this[str] = value;
|
||||
}
|
||||
}
|
||||
public Person this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
#region 知识点四 索引器中可以写逻辑
|
||||
#endregion
|
||||
if (friends == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else if (friends.Length - 1 < index)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return friends[index];
|
||||
}
|
||||
}
|
||||
set
|
||||
{
|
||||
if (friends == null)
|
||||
{
|
||||
friends = new Person[] { value };
|
||||
}else if(index > friends.Length -1)
|
||||
{
|
||||
friends = new Person[] { value };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
|
||||
#region 知识点六
|
||||
#endregion
|
||||
|
||||
#region 知识点七
|
||||
#endregion
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
#region 知识点三
|
||||
Person p = new Person();
|
||||
p[0] = new Person();
|
||||
Console.WriteLine(p[0]);
|
||||
p[0, 0] = 10;
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>Lesson6_封装_索引器练习</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,145 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Lesson6_封装_索引器练习
|
||||
{
|
||||
class IntArray
|
||||
{
|
||||
private int[] array;
|
||||
private int used, capacity;
|
||||
public IntArray()
|
||||
{
|
||||
capacity = 5;
|
||||
array = new int[capacity];
|
||||
used = 0;
|
||||
}
|
||||
//增
|
||||
public void Add(int value)
|
||||
{
|
||||
if (used < capacity)
|
||||
{
|
||||
array[used] = value;
|
||||
used++;
|
||||
}
|
||||
else
|
||||
{
|
||||
capacity += 1;
|
||||
int[] newArray = new int[capacity];
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
newArray[i] = array[i];
|
||||
}
|
||||
array = newArray;
|
||||
|
||||
array[used] = value;
|
||||
used++;
|
||||
}
|
||||
}
|
||||
//删
|
||||
public void DelValue(int target)
|
||||
{
|
||||
for (int i = 0; i < used; i++)
|
||||
{
|
||||
if(target == array[i])
|
||||
{
|
||||
DelIndex(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
Console.WriteLine("没找到");
|
||||
}
|
||||
public void DelIndex(int target)
|
||||
{
|
||||
if (target > array.Length - 1)
|
||||
{
|
||||
Console.WriteLine("当前数组只有{0}长,溢出",used);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = target; i < used-1; i++)
|
||||
{
|
||||
array[i] = array[i + 1];
|
||||
}
|
||||
for (int i = used - 1; i < used; i++)
|
||||
{
|
||||
array[i] = 0;
|
||||
}
|
||||
}
|
||||
--used;
|
||||
|
||||
}
|
||||
public int this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (index >= capacity)
|
||||
{
|
||||
Console.WriteLine("已溢出,当前index:"+index+'\n'+"当前已用值:"+(used)+"当前容量:"+(capacity));
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return array[index];
|
||||
}
|
||||
|
||||
}
|
||||
set
|
||||
{
|
||||
Console.WriteLine(index);
|
||||
array[index] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
IntArray a = new IntArray();
|
||||
a.Add(1);
|
||||
a.Add(2);
|
||||
a.Add(3);
|
||||
a.Add(4);
|
||||
a.Add(5);
|
||||
a.Add(6);
|
||||
a.Add(7);
|
||||
a.Add(8);
|
||||
a.Add(9);
|
||||
a.Add(10);
|
||||
Console.WriteLine("dfdf" + a[0]);
|
||||
Console.WriteLine("dfdf" + a[1]);
|
||||
Console.WriteLine("dfdf" + a[2]);
|
||||
Console.WriteLine("dfdf" + a[3]);
|
||||
Console.WriteLine("dfdf" + a[4]);
|
||||
Console.WriteLine("dfdf" + a[5]);
|
||||
Console.WriteLine("dfdf" + a[6]);
|
||||
Console.WriteLine("dfdf" + a[7]);
|
||||
Console.WriteLine("dfdf" + a[8]);
|
||||
Console.WriteLine("dfdf" + a[9]);
|
||||
Console.WriteLine("越界测试" + a[10]);
|
||||
Console.WriteLine();
|
||||
a.DelIndex(3);
|
||||
Console.WriteLine("dfdf" + a[0]);
|
||||
Console.WriteLine("dfdf" + a[1]);
|
||||
Console.WriteLine("dfdf" + a[2]);
|
||||
Console.WriteLine("dfdf" + a[3]);
|
||||
Console.WriteLine("dfdf" + a[4]);
|
||||
Console.WriteLine("dfdf" + a[5]);
|
||||
Console.WriteLine("dfdf" + a[6]);
|
||||
Console.WriteLine("dfdf" + a[7]);
|
||||
Console.WriteLine("dfdf" + a[8]);
|
||||
Console.WriteLine("dfdf" + a[9]);
|
||||
Console.WriteLine();
|
||||
a.DelValue(7);
|
||||
Console.WriteLine("dfdf" + a[0]);
|
||||
Console.WriteLine("dfdf" + a[1]);
|
||||
Console.WriteLine("dfdf" + a[2]);
|
||||
Console.WriteLine("dfdf" + a[3]);
|
||||
Console.WriteLine("dfdf" + a[4]);
|
||||
Console.WriteLine("dfdf" + a[5]);
|
||||
Console.WriteLine("dfdf" + a[6]);
|
||||
Console.WriteLine("dfdf" + a[7]);
|
||||
Console.WriteLine("dfdf" + a[8]);
|
||||
Console.WriteLine("dfdf" + a[9]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,96 @@
|
||||
namespace Lesson7
|
||||
{
|
||||
#region 知识点一 静态成员基本概念
|
||||
//静态关键字 static
|
||||
//用static 修饰的 成员变量、方法、属性等
|
||||
//称为静态成员
|
||||
|
||||
//静态成员的特点是:直接用类名点出使用
|
||||
#endregion
|
||||
|
||||
#region 知识点二 早已出现的静态成员
|
||||
//console的writeline
|
||||
#endregion
|
||||
|
||||
#region 知识点三 自定义静态成员
|
||||
class test
|
||||
{
|
||||
//静态成员变量
|
||||
public static float PI = 3.1415926f;
|
||||
|
||||
public int testInt = 100;
|
||||
|
||||
public static float CalcCircle(float r)
|
||||
{
|
||||
#region 知识点六 静态函数中不能使用非静态成员
|
||||
//成员变量只能将对象实例化出来后 才能点出来使用 不能无中生有
|
||||
//不能直接使用非静态成员,否则会报错
|
||||
//Console.WriteLine(testInt);
|
||||
////如果要使用,必须先实例化(分配出一个内存)
|
||||
//test t = new test();
|
||||
////然后再调用
|
||||
//Console.WriteLine(t.testInt);
|
||||
#endregion
|
||||
return r * r * PI;
|
||||
}
|
||||
|
||||
public void TestFun()
|
||||
{
|
||||
#region 知识点七 非静态函数可以使用静态成员
|
||||
//也是因为生命周期造成的(已经分配内存了
|
||||
Console.WriteLine(test.PI);
|
||||
#endregion
|
||||
Console.WriteLine("test123");
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 知识点五 为什么可以直接点出来使用
|
||||
//记住!
|
||||
//程序中是不能无中生有的
|
||||
//我们使用的对象、变量、函数都是要在内存中分配空间的
|
||||
//之所以要实例化对象,目的就是分配内存空间,在程序中产生一个抽象的对象
|
||||
|
||||
//静态成员的特点
|
||||
//程序开始运行时 就会分配内存空间,所以我们就能直接使用
|
||||
//静态成员和程序同生共死
|
||||
//只要使用了他,知道程序结束时内存才会被释放
|
||||
//所以一个静态成员就会有自己唯一的一个“内存小房间”
|
||||
//这让静态成员就有了唯一性
|
||||
//在仍和地方使用都是用的小房间里的内容,改变了它也是改变小房间里的内容
|
||||
#endregion
|
||||
|
||||
#region 知识点八 静态成员对于我们的作用
|
||||
//静态变量:
|
||||
//1.常用唯一的变量 比如 重力加速度
|
||||
//2.方便别人获取的对象声明 比如 PI
|
||||
//静态方法:
|
||||
//常用的唯一的方法声明 比如 相同规则的数学计算相关函数
|
||||
#endregion
|
||||
|
||||
#region 知识点九 常量和静态常量
|
||||
//const(常量)可以理解为特殊的static(静态)
|
||||
//相同点
|
||||
//他们都可以通过类名点出使用
|
||||
//不同点
|
||||
//1.const必须初始化,不能修改 static没有这个规则
|
||||
//2.const只能修饰变量,static可以修饰类、方法等
|
||||
//3.const一定是写在访问修饰符后面的,static没有这个要求
|
||||
#endregion
|
||||
|
||||
internal class Program
|
||||
{
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
#region 知识点四 静态成员的使用
|
||||
Console.WriteLine(test.PI);
|
||||
Console.WriteLine(test.CalcCircle(2));
|
||||
|
||||
test t = new test();
|
||||
Console.WriteLine(t.testInt);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,38 @@
|
||||
namespace Lesson7_静态成员练习
|
||||
{
|
||||
//一个类对象,在整个应用程序的生命周期中,有且只有一个该对象的存在
|
||||
//不能在外部实例化,直接通过该类名就能得到位的的对象
|
||||
class test
|
||||
{
|
||||
private static test t = new test();//2.new一个对象,就可以用test.t来的到这个类对象
|
||||
//这样就有且只有一个该对象的存在
|
||||
//4.private来让外部不能设置null
|
||||
public int testInt = 10;
|
||||
public static test T//5.写一个成员属性来定义设置和取值的方法
|
||||
//这样就可以只让读,不让写
|
||||
{
|
||||
get
|
||||
{
|
||||
return t; //让t来包裹test.T,那么test.T.testInt和t.testInt就是相同的
|
||||
}
|
||||
set
|
||||
{
|
||||
Console.WriteLine("你可不能随便动");
|
||||
}
|
||||
}
|
||||
public test()//1.默认无参构造函数定义为private
|
||||
//这样不能在外部实例化 即在类外部test t = new test();
|
||||
//3.但是虽然不能new一个对象,可以在外部设置为null
|
||||
{
|
||||
}
|
||||
}
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
Console.WriteLine(test.T.testInt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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,48 @@
|
||||
namespace Lesson8_封装_静态类和静态构造函数练习
|
||||
{
|
||||
static class Calc
|
||||
{
|
||||
private static float PI = 3.1415926f;
|
||||
public static float CalcCirclePerimeter(int r)
|
||||
{
|
||||
return PI * r * 2;
|
||||
}
|
||||
public static float CalcCircleArea(int r)
|
||||
{
|
||||
return PI * r * r;
|
||||
}
|
||||
public static int CalcRectanglePerimeter(int h,int w)
|
||||
{
|
||||
return (h + w) * 2;
|
||||
}
|
||||
public static int CalcRectangleArea(int h, int w)
|
||||
{
|
||||
return h * w;
|
||||
}
|
||||
public static int Absolute(int num)
|
||||
{
|
||||
if (num < 0)
|
||||
{
|
||||
return -num;
|
||||
}
|
||||
else
|
||||
{
|
||||
return num;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
Console.WriteLine(Calc.CalcRectanglePerimeter(5,10));
|
||||
Console.WriteLine(Calc.CalcRectangleArea(5, 10));
|
||||
Console.WriteLine(Calc.CalcCirclePerimeter(3));
|
||||
Console.WriteLine(Calc.CalcCircleArea(3));
|
||||
Console.WriteLine(Calc.Absolute(123));
|
||||
Console.WriteLine(Calc.Absolute(-321));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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,67 @@
|
||||
namespace Lesson9_封装_拓展方法练习
|
||||
{
|
||||
static class Tools
|
||||
{
|
||||
public static int IntSqrt(this int i)
|
||||
{
|
||||
return i*i;
|
||||
}
|
||||
public static void Suicide(this player p)
|
||||
{
|
||||
Console.WriteLine("{0}自杀了",p.name);
|
||||
}
|
||||
}
|
||||
class player
|
||||
{
|
||||
public string name;
|
||||
public int hp, atk, def;
|
||||
public player()
|
||||
{
|
||||
name = "null";
|
||||
hp = 0;
|
||||
atk = 0;
|
||||
def = 0;
|
||||
}
|
||||
public player(string name,int hp,int atk,int def)
|
||||
{
|
||||
this.name =name;
|
||||
this.hp = hp;
|
||||
this.atk = atk;
|
||||
this.def = def;
|
||||
}
|
||||
public void Attack()
|
||||
{
|
||||
Console.WriteLine("{0}攻击了,攻击值{1}",name,atk);
|
||||
}
|
||||
public void Defense()
|
||||
{
|
||||
Console.WriteLine("{0}防御了,防御值{1}",name,def);
|
||||
}
|
||||
public void Move()
|
||||
{
|
||||
Console.WriteLine("{0}移动了",name);
|
||||
}
|
||||
public void GetHurt()
|
||||
{
|
||||
Console.WriteLine("{0}受伤了,剩余{1}hp",name,hp);
|
||||
}
|
||||
}
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
player p = new player();
|
||||
p.Attack();
|
||||
p.Defense();
|
||||
p.Move();
|
||||
p.GetHurt();
|
||||
p.Suicide();
|
||||
player p2 = new player("HK",100,100,100);
|
||||
p2.Attack();
|
||||
p2.Defense();
|
||||
p2.Move();
|
||||
p2.GetHurt();
|
||||
p2.Suicide();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user