【C#】基础——静态多态性(函数重载、运算符重载)

静态多态性:

  • 函数重载
  • 运算符重载

函数重载:

在同一个范围内对相同的函数名有多个定义。函数的定义必须彼此不同,可以是参数列表中的参数类型不同,也可以是参数个数不同。不能重载只有返回类型不同的函数声明。例如:下列实列演示了简单的相加功能:

using System;

namespace PolymorphismApplication
{
    public class TestData
    {
        public int Add(int a,int b,int c)
        {
            return a + b + c;
        }
        public int Add(int a,int b)
        {
            return a + b;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            TestData dataClass = new TestData();
            int add1 = dataClass.Add(1, 2);
            int add2 = dataClass.Add(1, 2, 3);
            Console.WriteLine("add1 : " + add1 + "\nadd2 : " + add2);
            Console.ReadKey();
           // Console.WriteLine("Hello World!");
        }
    }
}

下面是相同的几个Print函数:

using System;

namespace 多态性
{
    public class TestPrint
    {
       public void Print(int a)
        {
            Console.WriteLine("输出的是整形 : " + a);
        }
        public void Print(double a)
        {
            Console.WriteLine("输出的是浮点型 : " + a);
        }
        public void Print(string s)
        {
            Console.WriteLine("输出的是字符串 : " + s);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            TestPrint T = new TestPrint();
            T.Print(1);
            T.Print(1.23);
            T.Print("hello C#");

            Console.ReadKey();
           // Console.WriteLine("Hello World!");
        }
    }
}

运算符重载:

通过关键字operator后跟运算符实现两个对象的运算。下面的小程序展示了两个长方体的对象的相加:

using System;

namespace OperatorOvlApplication
{
   
    class Box
    {
        private double length, breadth, height;
        public double GetVolume()
        {
            return length * breadth * height;
        }
        public void SetLength(double L)
        {
            length = L;
        }
        public void SetBreadth(double B)
        {
            breadth = B;
        }
        public void SetHeight(double H)
        {
            height = H;
        }

        //重载 + 运算符来把两个BOX对象相加
        public static Box operator+ (Box a,Box b)
        {
            Box box = new Box();
            box.height = a.height + b.height;
            box.length = a.length + b.length;
            box.breadth = a.breadth + b.breadth;
            return box;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

            Box Box1 = new Box();
            Box Box2 = new Box();
            Box Box3 = new Box();
            double volume = 0.0;

            Box1.SetBreadth(6);
            Box1.SetHeight(5);
            Box1.SetLength(7);

            Box2.SetBreadth(3);
            Box2.SetHeight(8);
            Box2.SetLength(9);

            volume = Box1.GetVolume();
            Console.WriteLine("Box1的体积是 : " + volume);

            volume = Box2.GetVolume();
            Console.WriteLine("Box2的体积是 : " + volume);

            Box3 = Box1 + Box2;
            volume = Box3.GetVolume();
            Console.WriteLine("Box3的体积是 : " + volume);

            Console.ReadKey();
           // Console.WriteLine("Hello World!");
        }
    }
}

关键语句:

        //重载 + 运算符来把两个BOX对象相加
        public static Box operator+ (Box a,Box b)
        {
            Box box = new Box();
            box.height = a.height + b.height;
            box.length = a.length + b.length;
            box.breadth = a.breadth + b.breadth;
            return box;
        }

下表展示了C#中可重载与不可重载的运算符:

运算符描述
+, -, !, ~, ++, --这些一元运算符只有一个操作数,且可以被重载。
+, -, *, /, %这些二元运算符带有两个操作数,且可以被重载。
==, !=, <, >, <=, >=这些比较运算符可以被重载。
&&, ||这些条件逻辑运算符不能被直接重载。
+=, -=, *=, /=, %=这些赋值运算符不能被重载。
=, ., ?:, ->, new, is, sizeof, typeof这些运算符不能被重载。

例如:

using System;

namespace OperatorOvlApplication
{
   
    class Box
    {
        private double length, breadth, height;
        public double GetVolume()
        {
            return length * breadth * height;
        }
        public void SetLength(double L)
        {
            length = L;
        }
        public void SetBreadth(double B)
        {
            breadth = B;
        }
        public void SetHeight(double H)
        {
            height = H;
        }

        //重载 + 运算符来把两个BOX对象相加
        public static Box operator+ (Box a,Box b)
        {
            Box box = new Box();
            box.height = a.height + b.height;
            box.length = a.length + b.length;
            box.breadth = a.breadth + b.breadth;
            return box;
        }

        // ==相等
        public static bool operator == (Box a,Box b)
        {
            if (a.length == b.length && a.breadth == b.breadth && a.height == b.height)
                return true;
            else
                return false;
        }

        // !=不等于
        public static bool operator != (Box a,Box b)
        {
            if (a.length != b.length || a.breadth != b.breadth || a.height != b.height)
                return true;
            else
                return false;
        }

        // <小于
        public static bool operator < (Box a,Box b)
        {
            if (a.length < b.length && a.breadth < b.breadth && a.height < b.height)
            {
                return true;
            }
            else
                return false;
        }

        //>大于
        public static bool operator > (Box a ,Box b)
        {
            if (a.length > b.length && a.height > b.height && a.breadth > b.breadth)
                return true;
            else
                return false;
        }

        public override string ToString()
        {
            return String.Format("({0},{1},{2})",length,breadth,height);
        }

    }

    class Program
    {
        static void Main(string[] args)
        {

            Box Box1 = new Box();
            Box Box2 = new Box();
            Box Box3 = new Box();
            double volume = 0.0;

            Box1.SetBreadth(6);
            Box1.SetHeight(5);
            Box1.SetLength(7);

            Box2.SetBreadth(7);
            Box2.SetHeight(8);
            Box2.SetLength(9);

            volume = Box1.GetVolume();
            Console.WriteLine("Box1的体积是 : " + volume);

            volume = Box2.GetVolume();
            Console.WriteLine("Box2的体积是 : " + volume);

            Box3 = Box1 + Box2;
            volume = Box3.GetVolume();
            Console.WriteLine("Box3的体积是 : " + volume);

            //使用重载显示两个盒子
            Console.WriteLine("Box1 : {0}", Box1.ToString());
            Console.WriteLine("Box2 : {0}", Box2.ToString());

            if (Box1 == Box2)
                Console.WriteLine("Box1 == Box2");
            else
                Console.WriteLine("Box1 != Box2");

            if (Box1 > Box2)
                Console.WriteLine("Box1 > Box2");
            else
            {
                Console.WriteLine("Box1 < Box2");
            }

            Console.ReadKey();
           // Console.WriteLine("Hello World!");
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值