运算符重载和函数重写(概念、简单案例)--C#

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

namespace _03_运算符重载
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 运算符重载属于多态中的一种
            //Point p1 = new Point(100, 100);
            //Point p2 = new Point(50, 50);
            Point p3 = p1 + p2;
            //Point p4 = new Point(150, 150);
            Point p3 = new Point(p1.X + p2.X, p1.Y + p2.Y);
            //Point p3 = p1 + p2 + p4;
            //Console.WriteLine(p3);
            //Console.WriteLine("------------------");
            //Point p5 = p1 + 100;
            //Console.WriteLine(p5);
            //Point p6 = 100 + p1;
            //Console.WriteLine(p6);
            //Point p11 = p1++; 
            //Point p12 = ++p1;
            //Point p22 = p2--;
            //Point p21 = --p2;
            //p1+=p2;// 等价于p1 = p1 + p2;

            Point p1 = new Point(100, 100);
            Point p2 = new Point(100, 100);

            // 对象中的数据是一样的 但是并不是同一个对象 也就是存储的数据地址不一样 因此在执行相等判断 结果是false
            Console.WriteLine(p1 == p2);
            Console.ReadKey();
        }
        // 运算符重载支持+-*/% ++ -- 等运算符
        // += -= *= /= %= ++ -- 等运算符不支持重载 但是可以实现隐式重载
        // 列如当我们实现 + 重载是 那么 += 重载也就实现了
    }
    class Point
    {
        public int X { get; set; }
        public int Y { get; set; }
        public Point() { }

        public Point(int x, int y)
        {
            X = x;
            Y = y;

        }
        // 重载的运算符要放在类的身上
        // 重载的运算符是静态成员
        // 初始化两个点的位置
        #region 算数运算符的重载
        /*


        // operator 算子
        //用来对运算符进行重载
        public static Point operator +(Point p1, Point p2)
        {
            return new Point(p1.X + p2.X, p1.Y + p2.Y);
        }
        public static Point operator +(Point p1,int num )
        {
            return new Point(p1.X + num, p1.Y + num);
        }
        public static Point operator +(int num, Point p1)
        {
            return new Point(p1.X + num, p1.Y + num);
        }

        public static Point operator -(Point p1, Point p2)
        {
            return new Point(p1.X - p2.X, p1.Y - p2.Y);
        }
        public static Point operator *(Point p1, int num)
        {
            return new Point(p1.X * num, p1.Y * num);
        }
        public static Point operator /(Point p1, int num)
        {
            return new Point(p1.X / num, p1.Y / num);
        }
        public static Point operator %(Point p1, int num)
        {
            return new Point(p1.X % num, p1.Y % num);
        }

        public static Point operator ++(Point p1)
        {
            return new Point(p1.X + 1, p1.Y + 1);
        }
        public static Point operator --(Point p1)
        {
            return new Point(p1.X - 1, p1.Y - 1);
        }
        */
        #endregion


        // 逻辑运算符的重载
        // >= <= == != > < 等运算符也是可以重载的
        //== 运算符需要和 不等运算符一起重载
        public static bool  operator ==(Point p1, Point p2)
        {
            return   p1.X == p2.X && p1.Y == p2.Y;
        }
        public static bool operator !=(Point p1, Point p2)
        {
            return p1.X != p2.X || p1.Y != p2.Y;
        }
        public static bool operator >=(Point p1, Point p2)
        {
            return p1.X *p1.Y >= p2.X * p2.Y;
        }
        public static bool operator <=(Point p1, Point p2)
        {
            return p1.X * p1.Y <= p2.X * p2.Y;
        }
        public static bool operator >(Point p1, Point p2)
        {
            return p1.X * p1.Y > p2.X * p2.Y;
        }
        public static bool operator <(Point p1, Point p2)
        {
            return p1.X * p1.Y < p2.X * p2.Y;   
        }
 
 

    }
}
// 逻辑运算符 > 和<  >= 和<=  == 和!=  都需要成对出现
// 算术运算符 + - * / %  支持重载 需要传递两个参数
// ++ -- 运算符 也需要重载 只需要一个参数
// += -= *= /= %= 等运算符不支持重载 但是可以使用隐式重载实现 实现 + 运算符的重载 那么+= 运算符的重载也就是实现了 其他也一样
// 运算符重载需要通过 operator 关键字修饰
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _04_函数重写
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Father f = new Father();
            //Son s = new Son("小张",8);
            // 父类容器装在子类对象
            // 子类容器不能装在父类对象
            Father s = new Son("小张", 8);

            // new 关键字和override 都可以重写父类的方法
            // new 关键字在重写时 使用父类容器搭载子类对象 子类对象调用的是父类类的方法 表义不明确
            // override 关键字在重写时 使用子类容器搭载父类对象 父类对象调用的是子类类的方法 表义明确
            // 在重写时 我们一般用 override 进行重写 因为 override 关键字更加明确 new关键字重写的方法表义不明确

            // override 重写的方法需要的是父类的虚方法 (virtual) 或者 抽象方法 (abstract)
            s.Work();
            //f.Work();

            Console.ReadLine    ();
        }
    }
    class Father
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }

       virtual  public void Work()  
       {
            Console.WriteLine("父亲的工作是上班");

       }
        // 静态成员 静态成员放在类,只会初始化一次

       static  Random random = new Random();
        public Father()
        {
            // 实例方法可以获取静态数据 静态方法不能获取实例数据
            Id = random.Next();
        }
        // this() 调用无参构造函数
        public Father(string name, int age) : this()
        {
            Name = name;
            Age = age;
        }
    }

    //override 推翻
    //virtual 虚拟
    class Son : Father
    {
        // 重写父类的方法 或 属性
        // 隐式重写
        // 重写之后会有一个 波浪线 用来提示你 父类已经有该属性和方法 是否写错
        //  public string  Age { get; set; }
        // public void Work()
        //  {
        //      Console.WriteLine("儿子的工作是学习");

        //  }
        // 加new 关键字 显式重写 
        // 加new 关键字和不加new关键字实现的重写效果是一样的


        new public string Age { get; set; }
        //new public void Work()
        //{
        //    Console.WriteLine("儿子的工作是学习");

        //}
        //在子类中重写父类的虚方法
        override public void Work()
        {
            Console.WriteLine("儿子的工作是学习");

        }
        public Son() : base()
        {

        }
        public Son(string name, int age) : base(name, age)
        {

        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值