using System;-------------封装继承多态--------------------
namespace aHomework
{
class Equip
{
public string name;
public Equip(string name)
{
this.name = name;
}
}
class Weapon : Equip
{
public int level;
public float attackPower;
public Weapon(string name, float attackPower) : base(name)
{
this.attackPower = attackPower;
}
public virtual void Attack()
{
Console.WriteLine($"{name}攻击,攻击力{attackPower}");
}
}
class Armor : Equip
{
//防御力
public float armorPower;
//耐久度
public float durability;
public Armor(string name, float armorPower) : base(name)
{
this.armorPower = armorPower;
}
}
class MagicWeapon : Weapon
{
public MagicWeapon(string name, float attackPower) : base(name, attackPower)
{
}
public override void Attack()
{
Console.Write("魔法武器");
//父类中的Attack执行
base.Attack();
}
}
class BowAndArrow : Weapon
{
public BowAndArrow(string name, float attackPower) : base(name, attackPower)
{
}
public override void Attack()
{
Console.Write("弓箭武器");
base.Attack();
}
}
class LongSword : Weapon
{
//防御力
public float armorPower;
public LongSword(string name, float attackPower, float armorPower) : base(name, attackPower)
{
this.armorPower = armorPower;
}
public override void Attack()
{
Console.Write("长剑武器");
base.Attack();
}
}
class Hero
{
/// <summary>
/// 装备列表
/// </summary>
public Equip[] equipList;
public Hero()
{
equipList = new Equip[6];
}
public void AddEquip(Equip equip)
{
for (int i = 0; i < equipList.Length; i++)
{
if (equipList[i] == null)
{
//将新装备存储
equipList[i] = equip;
Console.WriteLine(equip.name + "添加到列表成功!");
return;
}
}
Console.WriteLine("装备列表已满!");
}
public void RemoveEquip(Equip equip)
{
if (equip != null)
{
RemoveEquip(equip.name);
return;
}
Console.WriteLine("装备为空!");
}
public void RemoveEquip(string equipName)
{
for (int i = 0; i < equipList.Length; i++)
{
if(equipList[i] == null)
continue;
if (equipList[i].name == equipName)
{
Console.WriteLine(equipName + "从列表删除成功!");
//删除该装备
equipList[i] = null;
return;
}
}
Console.WriteLine($"列表中无{equipName}装备,删除失败!");
}
/// <summary>
/// 找到一把弓箭并进行攻击
/// </summary>
public void FindBowAndArrowAndAttack()
{
for (int i = 0; i < equipList.Length; i++)
{
if(equipList[i] == null)
continue;
if (equipList[i] is BowAndArrow)
{
//攻击
(equipList[i] as Weapon).Attack();
return;
}
}
Console.WriteLine("没有找到弓箭装备!");
}
/// <summary>
/// 找到所有武器,并进行攻击
/// </summary>
public void FindAllWeaponAndAttack()
{
for (int i = 0; i < equipList.Length; i++)
{
if(equipList[i] == null)
return;
if (equipList[i] is Weapon)
{
//武器进行攻击
(equipList[i] as Weapon).Attack();
}
}
}
}
class Program
{
static void Main(string[] args)
{
MagicWeapon wydb = new MagicWeapon("无用大棒",20);
LongSword wjzr = new LongSword("无尽之刃",100,0);
Equip bwg = new BowAndArrow("霸王弓",80);
Equip ktkj = new Armor("狂徒铠甲",80);
Hero angle = new Hero();
angle.AddEquip(ktkj);
angle.AddEquip(wydb);
angle.AddEquip(wjzr);//Equip equip = wjzr;
angle.AddEquip(bwg);
angle.RemoveEquip(wjzr);
angle.RemoveEquip("饮血剑");
angle.FindBowAndArrowAndAttack();
Console.WriteLine("---------");
angle.AddEquip(wjzr);
angle.FindAllWeaponAndAttack();
}
}
}
using System;--------抽象类*-------------------------------
namespace bAbstruct
{
/// <summary>
/// 运动员
/// </summary>
abstract class Sportsman
{
private string name;
public Sportsman(string name)
{
this.name = name;
}
/// <summary>
/// 训练
/// </summary>
public abstract void Train();
/*
* 抽象方法必须在抽象类中存在
* 但是,抽象类中不一定要有抽象方法,一般在抽象类中都会有抽象方法
* 但是,抽象类中也可以存在普通方法
*/
public void Eat()
{
Console.WriteLine("吃饭");
}
}
/// <summary>
/// 足球运动员
/// </summary>
class FootBallSportsman : Sportsman
{
//问题2:训练方法可以重写,也可以不重写
// public override void Train()
// {
// Console.WriteLine("跑步、传球、射门");
// }
public override void Train()
{
Console.WriteLine("跑步、传球、射门");
}
public FootBallSportsman(string name) : base(name)
{
}
}
abstract class SwimSportsman : Sportsman
{
/*
* 抽象方法在普通子类中,必须重写
* 然而,在抽象子类中,可以选择重写或不重写
*/
// public override void Train()
// {
// }
protected SwimSportsman(string name) : base(name)
{
}
}
/// <summary>
/// 自由泳
/// </summary>
class FreedomSwimSportsman : SwimSportsman
{
public override void Train()
{
Console.WriteLine("摆臂、蹬腿、憋气");
}
public FreedomSwimSportsman(string name) : base(name)
{
}
}
/// <summary>
/// 蛙泳
/// </summary>
class Breaststroke : SwimSportsman
{
public override void Train()
{
Console.WriteLine("扩臂、蹬腿、憋气");
}
public Breaststroke(string name) : base(name)
{
}
}
class Program
{
static void Main(string[] args)
{
// Sportsman liuxiang = new Sportsman();
// //问题1:方法体为空,不知道训练什么
// liuxiang.Train();
//抽象类允许声明对象
Sportsman yaoming;
//抽象类不能进行实例化对象
//new Sportsman();
Sportsman wulei = new FootBallSportsman("武磊");
wulei.Train();
Sportsman xiaoming = new FreedomSwimSportsman("xiaoming");
Sportsman xiaohong = new Breaststroke("xiaohong");
xiaoming.Train();
xiaohong.Train();
}
}
}
using System;-------------------------------------------------------------
/// <summary>
/// 员工
/// </summary>
abstract class Emplyee
{
private string name;
private string id;
public string Name
{
get { return name;}
set { name = value; }
}
public string Id
{
get { return id;}
set { id = value; }
}
// 抽象方法一定要是public 或 protected
public abstract float CalculateSalary();
public abstract void Work();
}
class Manager : Emplyee
{
public float workScore;
public float baseSalary = 10000;
public override float CalculateSalary()
{
float salary = baseSalary + workScore * 100;
Console.WriteLine($"{Name}正在结算薪资,薪资为{salary}");
return salary;
}
public override void Work()
{
Console.WriteLine($"{Name}正在工作,获取到10点工作积分");
workScore += 10;
}
}
namespace cAbstructEx
{
class Program
{
static void Main(string[] args)
{
Emplyee xiaohong = new Manager();
xiaohong.Name = "小红";
xiaohong.Work();
xiaohong.Work();
xiaohong.Work();
float money = xiaohong.CalculateSalary();
}
}
}
using System;--------密封类-----
using System.Reflection.PortableExecutable;
namespace dSealed
{
/// <summary>
/// 添加Sealed关键词,该类就变成了密封类
/// 功能:该类不能被继承
/// 不能有派生类
/// </summary>
sealed class Time
{
public string date;
public string time;
}
// class TestClass : Time
// {
// }
abstract class Sportsman
{
public abstract void Train();
public virtual void Match()
{
}
}
class RuningMan : Sportsman
{
///当前方法不能再被子类重写
public sealed override void Train()
{
Console.WriteLine("力量练习");
}
}
class LongTimeRunningman : RuningMan
{
//子类不能重写父类的密封方法了
// public override void Train()
// {
// Console.WriteLine("耐久度练习");
// }
}
class Program
{
static void Main(string[] args)
{
}
}
class MyClass
{
private static MyClass instance;
public static MyClass GetInstance()
{
if (instance==null)
{
instance = new MyClass();
}
return instance;
}
private MyClass()
{
}
}
class SingleTon
{
private static SingleTon instance;
public static SingleTon GetInstance()
{
if (instance==null)
{
instance = new SingleTon();
}
return instance;
}
private SingleTon()
{
}
}
}
using System;----------静态类--------------
namespace eStatic
{
class Equip
{
}
static class GameConst
{
//静态类中的常数定义的可以使用全大写or大驼峰
public const float PLAYER_HEIGHT = 1.8f;
public static float Player_MoveSpeed = 5;
public static Equip[] Player_Equips;
//静态构造函数
//1、[前面不允许添加访问修饰符]
//2、[不能有参数]
//3、[执行的时间节点:第一次访问该类的静态字段、属性、方法]
static GameConst()
{
Player_Equips = new Equip[5];
Console.WriteLine("GameConst 静态构造");
}
}
//在静态类中,只能有出现静态的东西,不能有成员的东西
static class MathTool
{
//成员的字段、属性、方法,都不能存在
// private string name;
// public string Name
// {
// get => name;
// set => name = value;
// }
// public void Demo()
// {
// }
public static float[] Sort(float[] array)
{
return null;
}
}
//普通的类中,也可以设置静态的字段、属性、方法
class Person
{
public static float PLAYER_HEIGHT = 1.8f;
//静态字段
private static long population = 7000000000;
public static int historyLength = 5000;
//静态属性
public static long Polulation
{
get { return population;}
set { population = value; }
}
//静态方法
public static long GetPopulation()
{
//静态方法里,不能出现成员的字段、属性、方法
// Console.WriteLine(name);
return Person.population;
}
static Person()
{
Console.WriteLine("Person 静态构造");
}
public void SayHello()
{
//成员方法里可以访问访问静态字段、方法
Console.WriteLine(population);
Console.WriteLine(historyLength);
GetPopulation();
}
public string name;
}
class Program
{
static void Main(string[] args)
{
//静态字段直接通过类名去访问
// Console.WriteLine(Person.population);
Console.WriteLine(Person.historyLength);
Person xiaoming = new Person();
// xiaoming.name
Person xiaohong = new Person();
// xiaohong.name
Person.GetPopulation();
Console.WriteLine(Person.historyLength);
Console.WriteLine(Person.Polulation);
//静态类不能进行实例化对象
// new MathTool();
//静态类的主要用途是,用于调用类中的静态字段、属性、方法
MathTool.Sort(new float[] {2, 3, 5, 6, 3});
//访问常数?常数一直保持不变,跟对象也毫无关系
//访问方式,也是通过类名访问,类名.常数变量
Console.WriteLine(GameConst.PLAYER_HEIGHT);
Console.WriteLine(GameConst.Player_MoveSpeed);
Console.WriteLine(GameConst.Player_MoveSpeed);
}
}
}
using System;-----------单例------------------
namespace fSingleton
{
/// <summary>
/// 单例类
/// </summary>
class Singleton
{
private static Singleton instance;
public static Singleton GetInstance()
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
private Singleton()
{
}
}
class AudioManager
{
private static AudioManager instance;
public static AudioManager GetInstance()
{
if (instance == null)
{
instance = new AudioManager();
}
return instance;
}
private AudioManager()
{
}
#region 单例成员
public float maxVolume = 10;
public void Play(string audioName)
{
Console.WriteLine($"正在播放{audioName}");
}
#endregion
}
class PlayerManager
{
private static PlayerManager instance;
public static PlayerManager GetInstance()
{
if (instance == null)
{
instance = new PlayerManager();
}
return instance;
}
private PlayerManager()
{
}
}
class GameData
{
private GameData()
{
}
#region ReadOnly方式
//让某个静态字段只能读,不能写 readonly
// public static readonly GameData instance = new GameData();
#endregion
#region 使用只读属性方式
//可以用只读属性解决
// public static GameData Instance
// {
// get { return instance;}
// }
#endregion
#region 使用静态方法【懒加载】
private static GameData instance;
public static GameData GetInstance()
{
if (instance == null)
{
instance = new GameData();
}
return instance;
}
#endregion
#region 使用单例
public string gameTime;
#endregion
}
class Program
{
static void Main(string[] args)
{
// new GameData()
// GameData.Instance = null;
// GameData.instance = null;
GameData ins = GameData.GetInstance();
ins.gameTime = "100";
ins = null;
Console.WriteLine(ins == GameData.GetInstance());
Console.WriteLine(GameData.GetInstance() == null);
AudioManager.GetInstance().maxVolume = 100;
AudioManager.GetInstance().Play("主背景音乐");
}
}
}