C#多态性

C#多态性

静态多态性

在编译时,函数和对象的连接机制被称为早期绑定,也被称为静态绑定。C# 提供了两种技术来实现静态多态性。分别为:

函数重载

运算符重载
运算符重载将在下一章节讨论,接下来我们将讨论函数重载。

函数重载

您可以在同一个范围内对相同的函数名有多个定义。函数的定义必须彼此不同,可以是参数列表中的参数类型不同,也可以是参数个数不同。不同重载只有返回类型不同的函数声明。

下面的实例演示了几个相同的函数 Add(),用于对不同个数参数进行相加处理:

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

namespace ConsoleApp2
{
    class Shape
    {
        public void setWidth(int w)
        {
            width = w;
        }
        public void setHeight(int h)
        {
            height = h;
        }
        protected int width;
        protected int height;
    }
    //基类 Cost
    public interface Cost
    {
        int getCost(int area);
    }
    // 派生类
    class Rectangle : Shape, Cost
    {
        public int getArea()
        {
            return (width * height);
        }
        public int getCost(int area)
        {
            return area * 8;
        }
    }
    public class Data
    {
        public int AddData(int a,int b)
        {
            return a + b;
        }
        public int AddData(int a, int b,int c)
        {
            return a + b + c * 10;
        }
    }
    class Test
    {
        public
        static void Main(string[] args)
        {
            int add1;
            int add2;
            Data data = new Data();
            add1 = data.AddData(2, 9);
            add2 = data.AddData(8, 9, 7);
            Console.WriteLine("add1的值:" + add1);
            Console.WriteLine("add2的值:" + add2);
            Console.ReadKey();

        }
    }
}

动态多态性

C# 允许您使用关键字 abstract 创建抽象类,用于提供接口的部分类的实现。当一个派生类继承自该抽象类时,实现即完成。抽象类包含抽象方法,抽象方法可被派生类实现。派生类具有更专业的功能。
您不能创建一个抽象类的实例。
您不能在一个抽象类外部声明一个抽象方法。
通过在类定义前面放置关键字 sealed,可以将类声明为密封类。当一个类被声明为 sealed 时,它不能被继承。抽象类不能被声明为 sealed。

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

namespace ConsoleApp2
{
    abstract class Shape
    {
        abstract public int area();
    }
    //基类 Cost
    public interface Cost
    {
        int getCost(int area);
    }
    // 派生类
    class Rectangle : Shape
    {
        private int len;
        private int width;
        public Rectangle(int a = 0, int b = 0)
        {
            len = a;
            width = b;
        }
        public override int area()
        {
            //Console.WriteLine("面积:");
            return (width * len);
        }
    }
    public class Data
    {
        public int AddData(int a,int b)
        {
            return a + b;
        }
        public int AddData(int a, int b,int c)
        {
            return a + b + c * 10;
        }
    }
    class Test
    {
        public
        static void Main(string[] args)
        {
            Rectangle r = new Rectangle(10, 7);
            double a = r.area();
            Console.WriteLine("面积:{0}", a);
            Console.ReadKey();
        }
    }

}

如果不实现Shape抽象类的这个方法,如果不实现就会报错
在这里插入图片描述

运行结果

在这里插入图片描述
当有一个定义在类中的函数需要在继承类中实现时,可以使用虚方法。

虚方法是使用关键字 virtual 声明的。

虚方法可以在不同的继承类中有不同的实现。

对虚方法的调用是在运行时发生的。

动态多态性是通过 抽象类 和 虚方法 实现的。
在这里插入图片描述
在这里插入图片描述

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值