添加Lesson14和Lesson15
This commit is contained in:
@@ -19,6 +19,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson12_继承-继承的
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson13_继承-里氏替换原则练习", "Lesson13_继承-里氏替换原则练习\Lesson13_继承-里氏替换原则练习.csproj", "{B17DEE02-D1AD-460B-8295-DDDE32A0EDA7}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson13_继承-里氏替换原则练习", "Lesson13_继承-里氏替换原则练习\Lesson13_继承-里氏替换原则练习.csproj", "{B17DEE02-D1AD-460B-8295-DDDE32A0EDA7}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson14_继承-继承中的构造函数", "Lesson14_继承-继承中的构造函数\Lesson14_继承-继承中的构造函数.csproj", "{DD06D61F-A96A-423A-ADB7-C74D555575AD}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson14_继承-继承中的构造函数练习", "Lesson14_继承-继承中的构造函数练习\Lesson14_继承-继承中的构造函数练习.csproj", "{01D03E15-704A-45B9-8854-2AFBED1BC69F}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson15_继承-万物之父和装箱拆箱", "Lesson15_继承-万物之父和装箱拆箱\Lesson15_继承-万物之父和装箱拆箱.csproj", "{E9C8E0F1-E80C-48B8-A049-A3D9719915E1}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson15_继承-万物之父和装箱拆箱练习", "Lesson15_继承-万物之父和装箱拆箱练习\Lesson15_继承-万物之父和装箱拆箱练习.csproj", "{FD313E4D-09E3-4310-B001-8BD9BF0B7C45}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@@ -57,6 +65,22 @@ Global
|
|||||||
{B17DEE02-D1AD-460B-8295-DDDE32A0EDA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{B17DEE02-D1AD-460B-8295-DDDE32A0EDA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{B17DEE02-D1AD-460B-8295-DDDE32A0EDA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{B17DEE02-D1AD-460B-8295-DDDE32A0EDA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{B17DEE02-D1AD-460B-8295-DDDE32A0EDA7}.Release|Any CPU.Build.0 = Release|Any CPU
|
{B17DEE02-D1AD-460B-8295-DDDE32A0EDA7}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{DD06D61F-A96A-423A-ADB7-C74D555575AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{DD06D61F-A96A-423A-ADB7-C74D555575AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{DD06D61F-A96A-423A-ADB7-C74D555575AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{DD06D61F-A96A-423A-ADB7-C74D555575AD}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{01D03E15-704A-45B9-8854-2AFBED1BC69F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{01D03E15-704A-45B9-8854-2AFBED1BC69F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{01D03E15-704A-45B9-8854-2AFBED1BC69F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{01D03E15-704A-45B9-8854-2AFBED1BC69F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{E9C8E0F1-E80C-48B8-A049-A3D9719915E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{E9C8E0F1-E80C-48B8-A049-A3D9719915E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{E9C8E0F1-E80C-48B8-A049-A3D9719915E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{E9C8E0F1-E80C-48B8-A049-A3D9719915E1}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{FD313E4D-09E3-4310-B001-8BD9BF0B7C45}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{FD313E4D-09E3-4310-B001-8BD9BF0B7C45}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{FD313E4D-09E3-4310-B001-8BD9BF0B7C45}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{FD313E4D-09E3-4310-B001-8BD9BF0B7C45}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<RootNamespace>Lesson14_继承_继承中的构造函数</RootNamespace>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,131 @@
|
|||||||
|
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
|
||||||
|
{
|
||||||
|
public GameObject() {
|
||||||
|
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
|
||||||
|
{
|
||||||
|
|
||||||
|
public Son(int i):base(i)//用base来传给父类构造函数一个值
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<RootNamespace>Lesson14_继承_继承中的构造函数练习</RootNamespace>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
namespace Lesson14_继承_继承中的构造函数练习
|
||||||
|
{
|
||||||
|
class Worker
|
||||||
|
{
|
||||||
|
public string job;
|
||||||
|
public string content;
|
||||||
|
|
||||||
|
public Worker(string job, string content)
|
||||||
|
{
|
||||||
|
this.job = job;
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Work()
|
||||||
|
{
|
||||||
|
Console.WriteLine("{0}做了{1}",job,content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class Programer : Worker
|
||||||
|
{
|
||||||
|
public Programer() : base("程序员", "编程")
|
||||||
|
{
|
||||||
|
Console.WriteLine("调用了Programer无参构造");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class Plan : Worker
|
||||||
|
{
|
||||||
|
public Plan() : base("策划", "设计游戏")
|
||||||
|
{
|
||||||
|
Console.WriteLine("调用了Plan无参");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class Art : Worker
|
||||||
|
{
|
||||||
|
public Art() : base("美术", "美术设计")
|
||||||
|
{
|
||||||
|
Console.WriteLine("调用了Art的无参构造");
|
||||||
|
}
|
||||||
|
public void testArt()
|
||||||
|
{
|
||||||
|
Console.WriteLine("调用了testArt测试函数");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
internal class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Hello, World!");
|
||||||
|
Programer programer = new Programer();
|
||||||
|
programer.Work();
|
||||||
|
|
||||||
|
Worker art = new Art();
|
||||||
|
art.Work();
|
||||||
|
|
||||||
|
Console.WriteLine(art is Programer);
|
||||||
|
Console.WriteLine(art is Art);
|
||||||
|
Console.WriteLine(art is Worker);
|
||||||
|
|
||||||
|
Art test = art as Art;
|
||||||
|
test.testArt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<RootNamespace>Lesson15_继承_万物之父和装箱拆箱</RootNamespace>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,97 @@
|
|||||||
|
namespace Lesson15_继承_万物之父和装箱拆箱
|
||||||
|
{
|
||||||
|
#region 里氏替换原则知识点回顾
|
||||||
|
//概念:父类容器装子类对象
|
||||||
|
//作用:方便进行对象存储和管理
|
||||||
|
//使用
|
||||||
|
//is和as
|
||||||
|
//is用于判断
|
||||||
|
//as用于转换
|
||||||
|
class Father
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
class Son : Father
|
||||||
|
{
|
||||||
|
public void Speak()
|
||||||
|
{
|
||||||
|
Console.WriteLine("Son.Speak()");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 知识点一 万物之父
|
||||||
|
//万物之父
|
||||||
|
//关键字Object
|
||||||
|
//概念
|
||||||
|
//object时所有类型的积累,他是一个类(引用类型)
|
||||||
|
//作用:
|
||||||
|
//1.可以利用里氏替换原则,用object容器装所有对象
|
||||||
|
//2.可以用来表示不确定类型,作为函数参数类型
|
||||||
|
#endregion
|
||||||
|
internal class Program
|
||||||
|
{
|
||||||
|
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Hello, World!");
|
||||||
|
|
||||||
|
#region 知识点二 万物之父的使用
|
||||||
|
Father f = new Son();
|
||||||
|
if (f is Son)
|
||||||
|
{
|
||||||
|
(f as Son).Speak();
|
||||||
|
}
|
||||||
|
|
||||||
|
//引用类型
|
||||||
|
object o = new Son();
|
||||||
|
o = f;
|
||||||
|
//用is和as来判断和转换即可
|
||||||
|
if(o is Son)
|
||||||
|
{
|
||||||
|
(o as Son).Speak();
|
||||||
|
}
|
||||||
|
|
||||||
|
//值类型
|
||||||
|
object o2 = 1f;
|
||||||
|
//用强转
|
||||||
|
float fl = (float)o2;
|
||||||
|
Console.WriteLine(o2);
|
||||||
|
|
||||||
|
//特殊的string类型
|
||||||
|
object str = "123";
|
||||||
|
string str2 = str.ToString();
|
||||||
|
string str3 = str as string;
|
||||||
|
Console.WriteLine(str2+str3);
|
||||||
|
|
||||||
|
//数组
|
||||||
|
object arr = new int[10];
|
||||||
|
int[] ar = arr as int[];
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 知识点三 装箱拆箱
|
||||||
|
//发生条件
|
||||||
|
//用objcet存值类型(装值)
|
||||||
|
//再把object转为值类型(拆箱)
|
||||||
|
|
||||||
|
//装箱
|
||||||
|
//把值类型用引用类型存储
|
||||||
|
//栈内存会迁移到堆内存中
|
||||||
|
|
||||||
|
//拆箱
|
||||||
|
//把引用类型存储的值类型取出来
|
||||||
|
//堆内存会迁移到栈内存中
|
||||||
|
|
||||||
|
//好处:不确定类型时可以方便参数的存储和传递
|
||||||
|
//坏处:存在内存迁移,增加性能消耗
|
||||||
|
#endregion
|
||||||
|
Test2(1, 2, 3, 4);
|
||||||
|
}
|
||||||
|
#region 知识点四 无敌知识点
|
||||||
|
//在使用变长传值时,如果不知道会传什么进去的时候就可以用object来存储
|
||||||
|
static void Test(params int[] array) { }//这个方法只能获取int类型
|
||||||
|
static void Test2(params object[] array) { }//这样就可以存任意长度任意类型的参数进来了
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<RootNamespace>Lesson15_继承_万物之父和装箱拆箱练习</RootNamespace>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
namespace Lesson15_继承_万物之父和装箱拆箱练习
|
||||||
|
{
|
||||||
|
internal class Program
|
||||||
|
{
|
||||||
|
static void Main(string[] args)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Hello, World!");
|
||||||
|
//用代码演示拆箱装箱
|
||||||
|
|
||||||
|
//装箱
|
||||||
|
int i = 10;
|
||||||
|
object o1 = i;
|
||||||
|
|
||||||
|
//拆箱
|
||||||
|
object o2 = o1;
|
||||||
|
int i2 = (int)o2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user