C# 抽象类

文章通过示例介绍了C#中抽象类和抽象方法的使用,包括抽象类不能被实例化,抽象方法必须在子类中重写等概念。同时,展示了如何利用抽象类实现多态,如计算不同形状(长方形和圆形)的面积和周长,以及模拟不同移动存储设备(U盘、MP3)的数据读写操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.抽象类

当父类中的方法不知道如何去实现的时候,可以考虑将父类写成抽象类,将方法写成抽象方法

2.代码

需求:狗狗会叫,猫咪会叫,按照以往我们会写一个父类,但是父类动物怎么叫?方法中不好实现。

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

namespace 抽象类
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Animal a = new Dog();
            a.Bark();
            Console.ReadKey();

        }
    }
    public abstract class Animal
    {
        private string _name;
        public string Name { get { return _name; } set { _name = value; } }
        public abstract void Bark();
        public abstract string TestString(string name);
    }

    public class Dog : Animal
    {

        public override void Bark()
        {

            Console.WriteLine("狗狗叫");
        }
        public override string TestString(string name)
        {

            string a = "ac";
            return a;
        }
    }
    public class Cat : Animal
    {

        public override void Bark()
        {

            Console.WriteLine("猫猫叫");
        }

        public override string TestString(string name)
        {

            string a = "ac";
            return a;
        }
    }


    public abstract class Test : Animal
    { 
    
    }

}


3.总结

1.抽象成员必须标记为abstract,并且不能有任何实现
2.抽象成员必须在抽象类中
3.抽象类不能被实例化
4.子类继承抽象类后,必须把父类中的所有抽象成员都重写(除非子类也是一个抽象类,则可以不重写)
5.抽象成员的访问修饰符不能是private
6.在抽象类中可以包含实例成员,并且抽象类的实例成员可以不被子类实现
7.抽象类是有构造函数的,虽然不能被实例化
8.如果父类的抽象方法有参数,那么继承这个抽象父类的子类在重写父类的方法的时候必须传入对应的参数,如果抽象父类的抽象方法有返回值,那么子类在重写这个抽象方法的时候也必须要传入返回值
9.如果父类中的方法有默认的实现,并且父类需要实例化,这时可以考虑将父类定义成一个普通类,用虚方法实现多态;
如果父类中的方法没有默认实现,父类也不需要被实例化,则可以将该类定义为抽象类

4.抽象类练习

计算长方形和圆的周长和面积

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

namespace 抽象类练习1
{
    internal class Program
    {
        //使用多态就矩形的和圆形的面积及周长
        static void Main(string[] args)
        {
            Shape shape = new Rectangle(5, 6);
            double a1 = shape.GetArea();
            double a2 = shape.GetPerimeter();
            Shape shape1 = new Circle(10);
            double b1 = shape1.GetArea();
            double b2 = shape1.GetPerimeter();
            Console.WriteLine("这个长方形的面积是{0},周长是{1}", a1, a2);
            Console.WriteLine("这个圆的面积是{0},周长是{1}", b1, b2);
            Console.ReadKey();
        }
    }

    public abstract class Shape
    {
        public abstract double GetArea();

        public abstract double GetPerimeter();
    }

    public class Rectangle : Shape
    {
        private double _height;
        private double _width;
        public double Height
        {
            get { return _height; }
            set { _height = value; }
        }
        public double Width { get { return _width; } set { _width = value; } }
        public Rectangle(double height, double width)
        {
            this.Height = height;
            this.Width = width;
        }

        public override double GetArea()
        {
            return this.Height * this.Width;
        }

        public override double GetPerimeter()
        {
            return 2 * (this.Width + this.Height);
        }
    }

    public class Circle : Shape
    {
        private double _radius;
        public double Radius { get { return _radius; } set { _radius = value; } }

        public Circle(double radius)
        {
            this.Radius = radius;
        }


        public override double GetArea()
        {
            return Math.PI * this.Radius * this.Radius;
        }

        public override double GetPerimeter()
        {
            return 2 * Math.PI * this.Radius;
        }
    }
}

模拟移动u盘、u盘、MP3

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

namespace 模拟硬盘读写数据
{
    internal class Program
    {
        static void Main(string[] args)
        {

            //第一张方法
            //UDisk uDisk = new UDisk();
            //MobileStorage mobileStorage = uDisk;
            //Computer computer=new Computer();
            //computer.CpuWrite(mobileStorage);
            //computer.CpuRead(mobileStorage);
            //Console.ReadKey();
            


            //第二种方法
            //MP3 mp3 = new MP3();
            //Computer computer = new Computer();
            //computer.CpuWrite(mp3);
            //computer.CpuRead(mp3);
            //Console.ReadKey();
            


            //第三种方法
            Computer computer = new Computer();
            MobileStorage mobileStorage = new MP3();
            computer.Storage=mobileStorage;
            computer.CpuWrite();
            computer.CpuWrite();    
            Console.ReadKey();  
        }
    }

    public abstract class MobileStorage
    {
        public abstract void Read();
        public abstract void Write();
    }
    public class MobileDisk : MobileStorage
    {
        public override void Read()
        {
            Console.WriteLine("移动硬盘在读数据");
        }

        public override void Write()
        {
            Console.WriteLine("移动硬盘在写数据");
        }
    }
    public class UDisk : MobileStorage
    {
        public override void Read()
        {
            Console.WriteLine("U盘在读数据");
        }

        public override void Write()
        {
            Console.WriteLine("U盘在写数据");
        }
    }
    public class MP3 : MobileStorage
    {
        public override void Read()
        {
            Console.WriteLine("MP3在读数据");
        }

        public override void Write()
        {
            Console.WriteLine("MP3在写数据");
        }
        public void play()
        {
            Console.WriteLine("我能放歌");
        }
    }

    public class Computer
    {
        private MobileStorage _storage;
        public MobileStorage Storage { get { return _storage; }set { _storage = value; } }

        public void CpuRead()
        {
          Storage.Read(); 
        }
        public void CpuWrite()
        {
            Storage.Write();
        }

        #region 第一种方法和第二种方法
        //public void CpuRead(MobileStorage ms)
        //{
        //    ms.Read();
        //}
        //public void CpuWrite(MobileStorage ms)
        //{
        //    ms.Write();
        //}
        #endregion

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值