06抽象类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _06抽象类
{
    class Program
    {
        static void Main(string[] args)
        {
            //抽象类不允许创建对象。
            //实现多态  声明父类,指向子类
            Animal a = new Dog();
            a.Bark();
            Animal b = new Cat();
            b.Bark();

            //抽象对象,可以写抽象成员外,也可以写普通成员
            //抽象类除了不能创建对象外,属性,构造函数等和其他父类一样可以创建
            //抽象类中创建的属性,只能在子类中进行调用

            Console.ReadKey();
        }
    }

    abstract class Animal
    {
        public abstract void Bark();

        public string Name { get; set; }
        int _age;

        public int Age
        {
            get
            {
                return _age;
            }

            set
            {
                _age = value;
            }
        }
        public Animal(string name,int age)
        {
            this.Name = name;
            this.Age = age;
        }
    }

    class Dog : Animal
    {
        public Dog(string name, int age) : base(name, age)
        {
        }

        public override void Bark()
        {
            Console.WriteLine("小狗汪汪叫");
        }
    }

    class Cat : Animal
    {
        public Cat(string name, int age) : base(name, age)
        {
        }

        public override void Bark()
        {
            Console.WriteLine("小猫瞄瞄叫");
        }
    }
}

 

转载于:https://www.cnblogs.com/andu/p/6133136.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值