2025-09-10 14:43:03 +08:00
|
|
|
namespace Lesson14_继承_继承中的构造函数
|
|
|
|
|
{
|
|
|
|
|
#region 知识回顾
|
|
|
|
|
//构造函数
|
|
|
|
|
//实例化对象时调用的函数
|
|
|
|
|
//主要用来初始化成员变量
|
|
|
|
|
//每个类都会有一个默认的无参构造函数
|
|
|
|
|
|
|
|
|
|
//语法
|
|
|
|
|
//访问修饰符 类名()
|
|
|
|
|
//{
|
|
|
|
|
// 初始化语句
|
|
|
|
|
//}
|
|
|
|
|
//不写返回值
|
|
|
|
|
//函数名和类名相同
|
|
|
|
|
//访问修饰符根据需求决定,一般为public
|
|
|
|
|
//构造函数可以重载
|
|
|
|
|
//可以用this语法重用代码
|
|
|
|
|
|
|
|
|
|
//注意
|
|
|
|
|
//有参构造会顶掉无参构造
|
|
|
|
|
//需要重新声明无参构造
|
|
|
|
|
class Test
|
|
|
|
|
{
|
|
|
|
|
public int testI;
|
|
|
|
|
public string testStr;
|
|
|
|
|
public Test()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Test(int i)
|
|
|
|
|
{
|
|
|
|
|
this.testI = i;
|
|
|
|
|
}
|
|
|
|
|
public Test(int i, string str) : this(i)//复用上方public Test(int i)
|
|
|
|
|
{
|
|
|
|
|
this.testStr = str;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 知识点一 基本概念
|
|
|
|
|
//特点
|
|
|
|
|
//当声明一个子类对象时
|
|
|
|
|
//先执行父类的构造函数
|
|
|
|
|
//再执行子类的构造函数
|
|
|
|
|
|
|
|
|
|
//注意:
|
|
|
|
|
//1.父类的无参构造 很重要
|
|
|
|
|
//2.子类可以通过base关键字 代表父类 调用父类构造
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 知识点二 继承中构造函数的执行顺序
|
|
|
|
|
//父类的父类的构造->...父类构造->...->子类构造
|
|
|
|
|
class GameObject
|
|
|
|
|
{
|
2025-09-11 15:07:37 +08:00
|
|
|
public GameObject()
|
|
|
|
|
{
|
2025-09-10 14:43:03 +08:00
|
|
|
Console.WriteLine("gameobject的构造函数");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
class Player : GameObject
|
|
|
|
|
{
|
|
|
|
|
public Player()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("player的构造函数");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
class MainPlayer : Player
|
|
|
|
|
{
|
|
|
|
|
public MainPlayer()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("mainplayer的构造函数");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 知识点三 父类的无参构造函数很重要
|
|
|
|
|
//子类实例化时默认调用的父类无参构造,如果父类无参被顶掉会报错
|
|
|
|
|
//class Father
|
|
|
|
|
//{
|
|
|
|
|
// public Father(int i)//用有参把无参构造顶了
|
|
|
|
|
// {
|
|
|
|
|
// Console.WriteLine("father的构造");
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
//class Son: Father//无参构造函数被顶导致无法正确调用父类构造函数
|
|
|
|
|
//{
|
|
|
|
|
// public Son()
|
|
|
|
|
// {
|
|
|
|
|
// Console.WriteLine("son");
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 知识点四 通过base调用指定父类构造
|
|
|
|
|
class Father
|
|
|
|
|
{
|
|
|
|
|
public Father(int i)//用有参把无参构造顶了
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("father的构造,参数i的值{0}",i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
class Son : Father
|
|
|
|
|
{
|
|
|
|
|
|
2025-09-11 15:07:37 +08:00
|
|
|
public Son(int i) : base(i)//用base来传给父类构造函数一个值
|
2025-09-10 14:43:03 +08:00
|
|
|
{
|
|
|
|
|
Console.WriteLine("son 1,参数i的值{0}", i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Son(int i, int j) : this(i)//用this来调用上一个public Son(int i):base(i)继而用base来传给父类构造函数一个值
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("son 2,参数i的值{0}", i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
internal class Program
|
|
|
|
|
{
|
|
|
|
|
static void Main(string[] args)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Hello, World!");
|
|
|
|
|
MainPlayer mp = new MainPlayer();
|
|
|
|
|
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
|
|
|
|
|
Son s = new Son(1,2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|