using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lei
{
class Dog
{
public string Name { get; set; }
public void Shout()
{
Console.WriteLine(this.Name + "汪汪");
}
}
class Cat
{
public string Name { get; set; }
public void Shout()
{
Console.WriteLine(this.Name + "喵喵");
}
}
internal class Program //类
{
static void Main(string[] args)
{
var dog = new Dog() { Name = "小黑" };
dog.Shout();
}
}
}
有个问题:重复代码 使用 继承/派生 解决
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lei
{
class SecretDate
{
public int Age { get; set; }
}
class Animal//基类/父类/超类
{
protected SecretDate SecretDate = new SecretDate() { Age = 1};
//protected 对外部私用,对子类公有
public string Name { get; set; }
}
class Dog: Animal
{
public void Shout()
{
Console.WriteLine(base.SecretDate.Age + "岁的" + base.Name + ":汪汪");
//base:访问基类成员、基类构造函数。这里用this也行
}
}
class Cat: Animal
{
public void Shout()
{
Console.WriteLine(this.Name + "喵喵");
}
}
internal class Program //类
{
static void Main(string[] args)
{
var dog = new Dog() { Name = "小黑" };
dog.Shout();
}
bass:访问基类成员、基类构造函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lei
{
class A
{
public A()
{
Console.WriteLine("A()");
}
public A(string a):this()
{
Console.WriteLine("A(string)");
}
public A(int a) : this("123")
{
Console.WriteLine("A(int)");
}
}
class B:A
{
public B():base() { Console.WriteLine("B()"); }
//这里只能用bass不能用this
}
internal class Program //类
{
static void Main(string[] args)
{
B a= new B();
}
}
}
何谓多态:一个类型,多钟状态
virtual override(可用于属性、方法)
目标:将变化封闭(注:override可以继续继承)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lei
{
class SecretDate
{
public int Age { get; set; }
}
class Animal//基类/父类/超类
{
protected SecretDate SecretDate = new SecretDate() { Age = 1 };
//protected 对外部私用,对子类公有
public string Name { get; set; }
public virtual void Shout() { Console.WriteLine("未知的叫声"); }
}
class Dog : Animal
{
public override void Shout()
{
Console.WriteLine(base.SecretDate.Age + "岁的" + base.Name + ":汪汪");
//base:访问基类成员、基类构造函数。这里用this也行
}
}
class Cat : Animal
{
public override void Shout()
{
Console.WriteLine(base.SecretDate.Age + "岁的" + base.Name + ":喵喵");
}
}
internal class Program //类
{
static void Text(Animal animal)
{
Console.WriteLine(animal.Name);
animal.Shout();
}
static void Main(string[] args)
{
//Dog dog = new Dog() { Name = "小黑" };
//dog.Shout();
Animal animal = new Cat() { Name = "小黑" };
Text(animal);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace lei
{
abstract class Dashborad //一级类:表盘
{
public abstract void Render();
}
class LansquenetHP:Dashborad //类30:雇佣兵的生命值
{
public int Hp = 200;// { get; set; }
public override void Render() { Console.WriteLine("雇佣兵的HP为 {0}",this.Hp); }
}
class MasterHP:Dashborad //类20:主角的生命值
{
public int Hp = 100;// { get; set; }
public override void Render() { Console.WriteLine("主角的HP为 {0}", this.Hp); }
}
class Master//类①:主角 包含类30(主角的生命值) 包含类②(雇佣兵)
{
public MasterHP HP = new MasterHP();
public Lansquenet lansquenet = new Lansquenet();
}
class Lansquenet//类②:雇佣兵 包含类20(雇佣兵的生命值)
{
public LansquenetHP HP = new LansquenetHP();
}
internal class Program
{
static void TextMaster(List<Dashborad> dashborads)
{
foreach (var dashborad in dashborads)
dashborad.Render();
//dashborad.HP.Hp;
//dashborad.lansquenet.HP.Hp;
}
static void Cls()
{
for(int i = 1;i<=22;i++)
{
Console.WriteLine();
}
}
static void Main(string[] args)
{
Master me = new Master();
List<Dashborad> dbs = new List<Dashborad>();
dbs.Add(me.HP);
dbs.Add(me.lansquenet.HP);
TextMaster(dbs);
//TextMaster(me.HP);
//TextMaster(me.lansquenet.HP );
Thread.Sleep(1000);
Cls();
//...
me.HP.Hp -= 50;
me.lansquenet.HP.Hp -= 99;
TextMaster(dbs);
//TextMaster(me.HP);
//TextMaster(me.lansquenet.HP);
}
}
}