C#第七天继承

 

protected继承的可以访问,外部不可以访问

先调用基类的构造函数,之后在构造子类的构造函数

base可以决定调用基类的哪个构造函数

using System;

namespace test7
{
    class SecretData
    { 
        public int Age { get; set; }
    }
    class Animal
    { 
        protected SecretData SecretData=new SecretData();
        public string Name { get; set; }
    }
    class Dog : Animal//继承
    {
        public void Shout()
        { 
            Console.WriteLine("{0}岁的"+base.Name + ":旺旺",SecretData.Age);
        }
    }
    class A 
    {
        public A() { Console.WriteLine("A()"); }
        public A(int a) : this() { Console.WriteLine("A(int)"); }
    }
    class B : A
    {
        public B():base(123){ Console.WriteLine("B()"); }
    }
    class Program
    {

        static void Main(string[] args)
        {
            B b = new B();
        }
    }
}

 

使父类方法对应不同的子类展示相应的方法

using System;

namespace test7
{
    class SecretData
    { 
        public int Age { get; set; }
    }
    class Animal
    { 
        protected SecretData SecretData=new SecretData();
        public string Name { get; set; }
        public virtual void Shout() { Console.WriteLine("nishidahsaiz"); }
    }
    class Dog : Animal//继承
    {
        public override void Shout()
        { 
            Console.WriteLine("{0}岁的"+base.Name + ":旺旺",SecretData.Age);
        }
    }
    class Program
    {
        static void Test(Animal animal) 
        {
            Console.WriteLine(animal.Name);
            animal.Shout();
        }
        static void Main(string[] args)
        {
            Animal animal = new Dog() { Name = "小河" };
            //Dog animal = new Dog() { Name = "小河" };
            Test(animal);
        }
    }
}

 

using System;
using System.Collections.Generic;

namespace test7_1
{
    abstract class DashBoard //表盘
    {
        public abstract void Render();
    }
    class 雇佣兵的HP: DashBoard
    {
        public int Hp = 200;
        public override void Render() { Console.WriteLine("你是雇佣兵{0}", this.Hp); }

    }
    class 主角的HP: DashBoard
    {
        public int Hp = 100;
        public override void Render() { Console.WriteLine("你是主角{0}",this.Hp); }

    }   
    class 主角
    {
        public 主角的HP HP = new 主角的HP();
        public 雇佣兵 Helper = new 雇佣兵();

    }
    class 雇佣兵
    {
        public 雇佣兵的HP HP = new 雇佣兵的HP();

    }
    class Program
    {
        static void 重绘(List<DashBoard> dashboards)
        {
            foreach(var dashboard in dashboards)
                dashboard.Render();
        }
        static void Main(string[] args)
        {
            主角 me = new 主角();
            List<DashBoard> a = new List<DashBoard>();
            a.Add(me.HP);
            a.Add(me.Helper.HP );
            重绘(a);
            me.HP.Hp = 2222;
            重绘(a);
            Console.WriteLine("Hello World!");
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值