C#核心5——面向对象封装

1.拓展方法

1.拓展方法基本概念

概念:为现有非静态 变量类型 添加 新方法

作用

1.提升程序拓展性

2.不需要再对象中重新写方法

3.不需要继承来添加方法

4.为别人封装的类型写额外的方法

特点

1.一定是写在静态类中

2.一定是个静态函数

3.第一个参数为拓展目标

4.第一个参数用this修饰

2.基本语法

访问修饰符 static 返回值 函数名(this 拓展类名 参数名,参数类型 参数名,参数类型 参数名……)

3.实例

    static class Tools
    {
        //为int拓展了一个成员方法
        //成员方法 是需要 实例化对象后 才能使用的
        //value 代表 使用该方法的 实例化对象
        public static void SpeakValue(this int value)
        {
            //拓展方法的逻辑
            Console.WriteLine("为int拓展的方法" + value);
        }
        public static void SpeakStringInfo(this string str, string str2, string str3)
        {
            Console.WriteLine("为string拓展的方法");
            Console.WriteLine("调用方法的对象" + str);
            Console.WriteLine("传的参数" + str2+str3);
        }
 
        public static void Fun3(this Test t)
        {
            Console.WriteLine("为Test拓展的方法");
        }
    }

4.使用

    class Program
    {
        static void Main(string[] args)
        {
            int i = 10;
            i.SpeakValue();
 
            string str = "000";
            str.SpeakStringInfo("张三", "111");
 
            Test t = new Test();
            t.Fun3();
            #endregion
        }
    }

5.为自定义的类型拓展方法

    class Test
    {
        public int i = 10;
 
        public void Fun()
        {
            Console.WriteLine("123");
        }
        public void Fun2()
        {
            Console.WriteLine("456");
        }
    }

6.总结

概念:为现有的非静态 变量类型 添加 方法

作用:

  • 提升程序拓展性
  • 不需要再在对象中重新写方法
  • 不需要继承来添加方法
  • 为别人封装的类型写额外的方法

特点:

  • 静态类中的静态方法
  • 第一个参数 代表拓展的目标
  • 第一个参数前面一定要加 this

注意:

        可以有返回值 和 n个参数

        根据需求而定

2.运算符重载

1.运算符重载基本概念

概念

让自定义类和结构体

能够使用运算符

使用关键字    operator

特点

一定是一个公共的静态方法

返回值写在operator前

逻辑处理自定义

作用

让自定义类和结构体对象可以进行运算

注意

条件运算符需要成对实现

一个符号可以多个重载

不能使用ref和out

2.基本语法

public static 返回类型 operator 运算符(参数列表)

3.实例

    class Point
    {
        public int x;
        public int y;

        public static Point operator +(Point p1, Point p2)
        {
            Point p = new Point();
            p.x = p1.x + p2.x;
            p.y = p1.y + p2.y;
            return p;
        }

        public static Point operator +(Point p1, int value)
        {
            Point p = new Point();
            p.x = p1.x + value;
            p.y = p1.y + value;
            return p;
        }

        public static Point operator +(int value, Point p1)
        {
            Point p = new Point();
            p.x = p1.x + value;
            p.y = p1.y + value;
            return p;
        }

    }

4.使用

    class Program
    {
        static void Main(string[] args)
        {
            Point p = new Point();
            p.x = 1;
            p.y = 1;
            Point p2 = new Point();
            p2.x = 1;
            p2.y = 1;
 
            Point p3 = p + p2;
 
            Point p4 = p3 + 4;
            Point p5 = 4 + p4;
        }
    }

5.可重载和不可重载的运算符

1.可重载的运算符

(1)算术运算符

算术运算符
注意 符号需要两个参数还是一个参数
        public static Point operator -(Point p1, Point p2)
        {
            return null;
        }
        public static Point operator *(Point p1, Point p2)
        {
            return null;
        }
        public static Point operator /(Point p1, Point p2)
        {
            return null;
        }
        public static Point operator %(Point p1, Point p2)
        {
            return null;
        }
        public static Point operator ++(Point p1)
        {
            return null;
        }
        public static Point operator --(Point p1)
        {
            return null;
        }

(2)逻辑运算符 

逻辑运算符
注意 符号需要两个参数还是一个参数
        public static bool operator !(Point p1)
        {
            return false;
        }

(3)位运算符

位运算符
注意 符号需要两个参数还是一个参数
        public static Point operator |(Point p1, Point p2)
        {
            return null;
        }
        public static Point operator &(Point p1, Point p2)
        {
            return null;
        }
        public static Point operator ^(Point p1, Point p2)
        {
            return null;
        }
        public static Point operator ~(Point p1)
        {
            return null;
        }
        public static Point operator <<(Point p1, int num)
        {
            return null;
        }
        public static Point operator >>(Point p1, int num)
        {
            return null;
        }

(4) 条件运算符

条件运算符
1.返回值一般是bool值 也可以是其他的
2.相关符号必须配对实现
        public static bool operator >(Point p1, Point p2)
        {
            return false;
        }
        public static bool operator <(Point p1, Point p2)
        {
            return false;
        }
 
        public static bool operator >=(Point p1, Point p2)
        {
            return false;
        }
        public static bool operator <=(Point p1, Point p2)
        {
            return false;
        }
 
        public static bool operator ==(Point p1, Point p2)
        {
            return false;
        }
        public static bool operator !=(Point p1, Point p2)
        {
            return false;
        }

2.不可重载的运算符

逻辑与(&&) 逻辑或(||)

索引符 []

强转运算符 ()

特殊运算符

点.                 三目运算符? :               赋值符号=

6.总结 

关键字     operator

固定语法:public static 返回值 operator 运算符(参数列表)

作用:让自定义类和结构体对象 进行运算

注意:

  1. 参数的数量
  2. 条件运算符的配对实现
  3. 一个符号可以多个重载
  4. 不能用ref和out

3.内部类和分部类

1.内部类

概念

在一个类中再申明一个类

特点

使用时要用包裹者点出自己

作用

亲密关系的变现

注意

访问修饰符作用很大

    class Person
    {
        public int age;
        public string name;
        public Body body;
        public class Body
        {
            Arm leftArm;
            Arm rightArm;
            class Arm
            {
                
            }
        }
    }

2.分部类

概念

把一个类分成几部分申明

关键字     

partial

作用

分部描述一个类

增加程序的拓展性

注意

分部类可以协助多个脚本文件中

分部类的访问修饰符要一致

分部类中不能有重复成员

    partial class Student
    {
        public bool sex;
        public string name;
 
        partial void Speak();
    }
    partial class Student
    {
        public int number;
 
        partial void Speak()
        {
            //实现逻辑
        }
        public void Speak(string str)
        {
            
        }
    }

3.分部方法

概念

将方法的申明和实现分离

特点

  1. 不能加访问修饰符 默认私有
  2. 只能在分部类中申明
  3. 返回值只能是void
  4. 可以有参数但不用out关键字

局限性很大,了解即可

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值