1.继承
理论知识:
//继承
//C#语言只支持单继承,不支持多继承---只能继承一个类//基类---被别人继承,但没有继承别人//子类在实例化的过程中,除了调用自己的构造函数,还会调用父类的构造函数,如果父类还有父类,也会调用//构造函数的调用顺序:先父类,再自己。//如何创建子类的构造函数:子类至少要有一个和父类匹配的上的构造函数(如果如果构造函数有参数,则还需
//要先调??类的构造函数并传参数,然后在调??类的构造)
//调用方式:public ?类名(参数列表) : base(参数列表)
课上代码:
using System;namespace Day13_2
{//以英雄联盟里野怪为背景讲继承//怪物类----父类public class Monster{//名称public string name;//攻击力public float attackPower;//有参构造函数,系统不再创建无参构造函数public Monster(string name, float attackPower){}public void Attact(){Console.WriteLine($"{name}正在攻击,攻击力为{attackPower}");}}//蓝Buff类public class BlueBuff{public float magicVauleAdd = 20;public float cdScale = 0.3f;public BlueBuff(float magicVauleAdd,float cdScale){}}public class BlueBuffMonster : Monster{public BlueBuff blueBuff;public BlueBuffMonster(string name, float attackPower, float magicValueAdd, float cdScale) : base(name,attackPower){//实例化BuffblueBuff = new BlueBuff(magicValueAdd,cdScale);}}//红Buff类public class RedBuff{public float addAttackBuff = 50;public RedBuff(float addAttackBuff){this.addAttackBuff = addAttackBuff;}}public class RedBuffMonster : Monster{public RedBuff redBuff;public RedBuffMonster(string name, float attackPower,float addAttackBuff) : base(name, attackPower){}}//小龙类public class DragonBuff{public BlueBuff blueBuff;public RedBuff redBuff;public float healthValueVuff;public DragonBuff(float healthValueVuff){this.healthValueVuff = healthValueVuff;}}public class DragonMonster : Monster{public DragonBuff DragonBuff;public DragonMonster(string name, float attackPower) : base(name, attackPower){}public DragonMonster(string name, float attackPower, DragonBuff dragonBuff) : base(name, attackPower){DragonBuff = dragonBuff;}}//工具类public class UseMonster{public void Demo(){BlueBuffMonster xiaolan = new BlueBuffMonster("小兰",20,3,0.3f);RedBuffMonster xiaohong = new RedBuffMonster("小红",30,50);xiaolan.Attact();xiaohong.Attact();}}class Program{static void Main(string[] args){new UseMonster().Demo();}}}
2.多态
理论知识:
//多态---同?类对象,做同?件事情,表现出不同的状态//将?类对象转换为?类类型?
//可以隐式转换,可以理解为栈内存储是?类对象的指针//将?类对象转换为?类类型?
//1.需要强制转换,(要转换的类型)对象
//对象 as 要转换的类型【仅限引?类型】
//2.如果转换失败,则转换后的对象为null
//强制转换之前需要先进?类型判断,判断是否可以转换成功
//对象 is 类型【仅限引?类型】//?类空参数构造函数的调??
//?类在实例化的时候,必会调??类的构造函数
//?类在声明构造函数的时候,要想办法调??类的构造,
//1.如果?类是空参数的构造函数 : base(),可以不写:base(),系统会默认调??类的空参数构造
//2.如果?类是有参数的构造函数,那么?定概要通过:base的?式调?,传参//?类?法的覆盖?***********覆盖(父类用父类,子类用子类,子类没有才用父类)
//1.前提条件:?类中有?个public函数,?类中没有该函数.因为?类中并没有该函数,所以调?必是?类的
//2.前提条件:?类?已经有了该函数,?类??也有该函数,?类对象调??类的该函数,?类对象调??类的该函数
//书写该函数的时候,规范的写法应该是:[访问修饰符] new 返回值类型 函数名(参数列表)
//覆盖:?类也有该函数了,以后调?的时候就调??类的该函数//?类?法的重写【表现出多态】
//重写:把?类和?类的该函数都重新写了?遍,有的新的内容
//此时,?类的对象,?论是不是转换成了?类的类型,都会执?重写后的该函数
//1.如果?类想要?类可以重写该函数,那么?类的该函数必须是?个虚函数
//[访问修饰符] virtual 返回值类型 函数名(参数列表)
//2.?类该怎么重写?
//[访问修饰符] override 返回值类型 函数名(参数列表)
课上代码:
using System;namespace Day13_3
{//以河南人和铁铃人都是中国人,说你在干什么的不同方式为背景public class Chinese{public string name;public Chinese(string name){this.name = name;}public virtual void SayWhatAreYouDoing(){Console.WriteLine($"{name}你在干什么?");}}public class HeNanPerson : Chinese{public new string name;public HeNanPerson(string name) : base(name){this.name = name;}public override void SayWhatAreYouDoing(){Console.WriteLine($"{name}说,弄啥嘞?");}}public class TieLingPerson : Chinese{public new string name;public TieLingPerson(string name) : base(name){this.name = name;}public override void SayWhatAreYouDoing(){Console.WriteLine("干哈呢?");}}public class PersonTest{public void Demo(){TieLingPerson mrZhao = new TieLingPerson("赵本山");//转型[隐式转换]--向上转型Chinese newMrZhao = mrZhao;newMrZhao.SayWhatAreYouDoing();Chinese longgang = new HeNanPerson("龙岗");longgang.SayWhatAreYouDoing();//使用is关键字是不是指定类型if (longgang is HeNanPerson){//向下转型(需要判断)//向下转型时,要使用as关键字进行强制转换HeNanPerson yueyue = longgang as HeNanPerson;yueyue.SayWhatAreYouDoing();ShowMe(mrZhao);ShowMe(longgang);}}public void ShowMe(Chinese chinese){chinese.SayWhatAreYouDoing();}}class Program{static void Main(string[] args){new PersonTest().Demo();}}
}
3.作业
//作业题
//梳理元旦购物项?
//实现如下需求
//定义如下类:
//1.装备类:
// 特征:名称
//2.武器类 继承 装备类:
// 特征:等级、攻击?
// ?为:攻击
//3.法杖类 继承 武器类:
// ?为:远程魔法攻击
//4.?箭类 继承 武器类:
// ?为:远程物理攻击
//5.?剑类 继承 武器类:
// 独有特征:防御?
// ?为:近战物理攻击
//6.防具类 继承 装备类:
// 特征:防御?、耐久度
// ?为:破损
//7.实现英雄类如下需求
//定义?法实现:添加装备到装备列表
//定义?法实现:删除装备从装备列表
//定义?法实现:从装备列表中找到?把?箭进?攻击
//定义?法实现:?装备列表中的所有武器各进??次攻击,查看攻击效果的不同。
代码实现:
using System;namespace Day13_4
{//装备类class Equipment{public string name;public Equipment(string name){this.name = name;}}//武器类class Weapon : Equipment{public int level;public float attackPower;public Weapon(string name, int level, float attackPower) : base(name){this.level = level;this.attackPower = attackPower;}public virtual void Attack(){Console.WriteLine($"使用武器{name}等级为{level}发起攻击,造成{attackPower}点伤害...");}}//法杖类class Staff : Weapon{public Staff(string name, int level, float attackPower) : base(name, level, attackPower){}//远程法术攻击public override void Attack(){Console.WriteLine($"武器{name}已经{level}级发出{attackPower}点的远程法术攻击...");}}//弓箭类class Arrow : Weapon{public Arrow(string name, int level, float attackPower) : base(name, level, attackPower){}//远程物理攻击public override void Attack(){Console.WriteLine($"使用武器{name}已经{level}级发出{attackPower}点的远程物理攻击...");}}//长剑类class LongSword : Weapon{public LongSword(string name, int level, float attackPower, float defensePower) : base(name, level, attackPower){DefensePower = defensePower;}//防御力public float DefensePower;//近程物理攻击public override void Attack(){Console.WriteLine($"使用武器{name}已经{level}级发出{attackPower}点的近程物理攻击...");}}//防具类class Armor : Equipment{public float DefensePower; //防御力public float Durability; //耐久度public Armor(string name, float defensePower, float durability) : base(name){DefensePower = defensePower;Durability = durability;}public void Demaged(){Durability = Durability - 1;Console.WriteLine($"装备耐久度为{Durability}...");}}//英雄类class Hero{public Equipment[] equipment;public Hero(){equipment = new Equipment[5];}//添加装备public void AddEquipment(Equipment weapon){for (int i = 0; i < 5; i++){if (equipment[i] == null){equipment[i] = weapon;Console.WriteLine($"{equipment[i].name}已添加...");return;}}Console.WriteLine("您的装备已满,无法添加...");}//删除装备public void DeleteEquipment(Equipment weapon){for (int i = 0; i < 5; i++){if (equipment[i].name == weapon.name){equipment[i] = null;Console.WriteLine($"{weapon.name}已移除...");return;}}Console.WriteLine($"装备列表中没有装备{weapon.name}...");}//从装备列表中找出一把弓箭进行攻击public void FindArrowAttack() {for (int i = 0; i < equipment.Length; i++){if (equipment[i] == null){continue;}if (equipment[i] is Arrow){(equipment[i] as Arrow)?.Attack();//Arrow test = equipment[i] as Arrow;//test.Attack();return;}}Console.WriteLine("武器列表中没有弓箭...");}//装备列表中的所有装备各进行一次攻击public void AllWeaponAttack(){for (int i = 0; i < equipment.Length; i++){if (equipment[i] == null){continue;}if (equipment[i] is Weapon){(equipment[i] as Weapon)?.Attack();//Weapon test = equipment[i] as Weapon;//test.Attack();}}}}class Program{static void Main(string[] args){Hero xiaoming = new Hero();Equipment weapon1 = new Armor("冰痕之握",250,100);Equipment weapon2 = new Weapon("冰霜长矛",3,100);Equipment weapon3 = new LongSword("长剑之光",3,100,80);Equipment weapon4 = new Staff("虚无法杖",5,200);Equipment weapon5 = new Arrow("破魔之剑",3,100);xiaoming.AddEquipment(weapon1);xiaoming.AddEquipment(weapon2);xiaoming.AddEquipment(weapon3);xiaoming.AddEquipment(weapon4);xiaoming.AddEquipment(weapon5);Console.WriteLine("---------------------------------------------------------");xiaoming.DeleteEquipment(weapon4); Console.WriteLine("---------------------------------------------------------");xiaoming.FindArrowAttack();Console.WriteLine("---------------------------------------------------------");xiaoming.AllWeaponAttack();Console.WriteLine("---------------------------------------------------------");for (int i = 0; i < 5; i++){if (xiaoming.equipment[i] == null){continue;}Console.WriteLine(xiaoming.equipment[i].name);}}}
}