C# 学习笔记 继承/派生

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);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值