Synchronization of old projects
Part1
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user