多态之抽象类

妖精的绣舞 提交于 2020-01-23 04:53:46

抽象类注意点

  1. 当父类中的方法不知道如何实现的时候,可以考虑将父类写成抽象类,将方法写成抽象方法。
  2. 抽象类标记abstract,抽象成员必须标记为abstract,并且不能有任何实现;
  3. 抽象成员必须在抽象类中,抽象类中可以有非抽象成员,用在继承;
  4. 抽象类不能实例化;
  5. 子类继承抽象类,必须将抽象类中的抽象成员重写;

实现代码

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

namespace _03抽象类
{
    class Program
    {
        static void Main(string[] args)
        {
            //狗狗会叫  猫咪也会叫\
            //Animal] animal = new Animal();抽象类不可以实例化
            Animal animal = new Dog();
            animal.Bark();//里面装的是Dog的对象;
            Dog dog = new Dog();
            Cat cat = new Cat();
            //抽象类和虚方法的区别:抽象类父类没实现,虚方法有实现方法;
        }
        public abstract class Animal
        {
            public abstract void Bark();//抽象方法不可以有方法体
                                        //不知道如何实现方法
        }
        public class Dog:Animal
        {
            public override void Bark()
            {
                Console.WriteLine("旺旺");
            }
        }
        public class Cat : Animal
        {
            public override void Bark()
            {
                Console.WriteLine("喵喵");
            }
        }
    }
}

利用抽象类实现圆、矩形的面积和周长

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

namespace _04抽象类求面积和周长
{
    class Program
    {
        static void Main(string[] args)
        {
            //使用多态求矩形、圆形的面积和周长
            Shape shape = new Circle(10);
            shape.GetArea();
            shape.GetPerimeter();
            Shape shape1 = new Square(10,20);
            shape1.GetPerimeter();
            Console.WriteLine(shape1.GetArea().ToString());
            Console.ReadKey();
        }
    }
    public abstract class Shape
    {
        public abstract double GetArea();
        public abstract double GetPerimeter();
    }    
    public class Circle:Shape
    {
        private double _r;

        public double R
        {
            get { return _r; }
            set { _r = value; }
        }
        public Circle(double r)
        {
            this.R = r;
        }
        public override double GetArea()
        {
            return Math.PI / 2 * R * R;
        }

        public override double GetPerimeter()
        {
            return Math.PI * 2 * R ; ;
        }
    }
    public class Square : Shape
    {
        private double _long;
        private double _width;
        public double Long
        {
            get { return _long; }
            set { _long = value; }
        }

        public double Width
        {
            get { return _width; }
            set { _width = value; }
        }
        public Square(double _long,double _width)
        {
            this.Long = _long;
            this.Width = _width;
        }

        public override double GetArea()
        {
            return Long * Width;
        }

        public override double GetPerimeter()
        {
            return 2 * Long * Width;
        }
    }
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!