c#基础5

泛型 :
    允许我们延迟编写类或方法中的数据类型, 直到真正使用时确定类型的一种规范
    1.可以创建自己的泛型接口、泛型类、泛型方法、泛型集合 ,泛型事件和泛型委托
    2.泛型的格式:    结构<泛型的名称T> 在定义泛型时T通常用作变量类型名称。但实际上 T 可以用任何有效名称代替   
    3.泛型方法在定义是提供泛型的类型  在调用方法时 提供具体的延迟编写的类型  
    4.泛型无法直接使用运算符 比如 + -  <  > 等 但是能使用Object中的属性和方法

 public static void Test<T>(T a, T b) 
        { 
                string temp  = a.ToString();
                string temp1 = b.ToString();
             Console.WriteLine(temp1+temp);
        }
 
 
  public static void Test1<T,鸡>(T a, T b,鸡 c)
        {
            string temp = a.ToString();
            string temp1 = b.ToString();
            Console.WriteLine(c.GetType().ToString()); 
            Console.WriteLine(temp1 + temp);
        }
泛型类

C# 语言中泛型类的定义与泛型方法类似,是在泛型类的名称后面加上<T>,当然,也可以定

义多个类型,即“<T1,T2,・・・>”。

class People<T, C> {
 
        private T _age;
        public T Age { get; set; }
        public T Eat(C a ,C b) {
            
            
            return _age;
        } 
    
    }
泛型接口
  interface Inter<T>
    {
        void show(T t);
    }
 
    //定义接口Inter的子类InterImpA,明确泛型类型为String (2)
    public class InterImpA : Inter<int>
    {
        //子类InterImpA重写方法show,指明参数类型为string
        public void show(int t)
        {
            Console.WriteLine(t);
        }
    }
    //定义接口Inter的子类InterImpB,直接声明           (1)
    public class InterImpB<T> : Inter<T>
    {
        public void show(T t)
        {
            Console.WriteLine(t);
        }
    }

c#内置了    IComparable 接口用于在要比较的对象的类中实现,可以比较任意两个对象。

 public class Student : IComparable<Student> {
 
        int Age { get; set; }
          //定义比较方法,按照学生的年龄比较 
        public int CompareTo(Student other) {
 
            if (this.Age == other.Age)
            {
                return 1;
            }
            else if (this.Age < other.Age)
            {
                return 2;
            }
 
            else {
 
                return 3;
            }
        
        }
 
    }

2.泛型可以提供代码性能,避免了装箱和拆箱

泛型和Object类型的区别
 
值类型转换成引用类型。这个转换称为装箱。相反的过程称为拆箱
 
int number = 10;  
// 装箱  
object obj = number;  
// 拆箱  
number = (int) obj;
 
 
C# 中 Object 是一切类型的基类,可以用来表示所有类型
 
Object 类型
 
> 优点:
> 1. object类型可以用来引用任何类型的实例;
> 2. object类型可以存储任何类型的值;
> 3. 可以定义object类型的参数;
> 4. 可以把object作为返回类型。
 
> 缺点:
> 1. 会因为程序员没有记住使用的类型而出错,造成类型不兼容;
> 2. 值类型和引用类型的互化即装箱拆箱使系统性能下降
 
 
 
装箱(从值类型转换到引用类型)需要经历如下几个步骤:
int a =10
object ob = a;
 
    首先在堆上分配内存。这些内存主要用于存储值类型的数据。
    接着发生一次内存拷贝动作,将当前存储位置的值类型数据拷贝到堆上分配好的位置。
    最后返回对堆上的新存储位置的引用。
 
拆箱(从引用类型转换为值类型)的步骤则相反:
// 拆箱  
number = (int) obj;
    首先检查已装箱的值的类型兼容目标类型。
    接着发生一次内存拷贝动作,将堆中存储的值拷贝到栈上的值类型实例中。
    最后返回这个新的值。
 
 
频繁拆装箱导致性能问题
由于拆箱和装箱都会涉及到一次内存拷贝动作,因此频繁地进行拆装箱会大幅影响性能
 
 
  泛型和装箱拆箱 对比
  public void abTest(object a, object b) {
 
            Console.WriteLine(a.ToString());
            Console.WriteLine(b.ToString());
        }
        public void abTest(T a, T b)
        {
 
            Console.WriteLine(a.ToString());
            Console.WriteLine(b.ToString());
        }
 
 
 
 

3. 泛型中的数据约束可以指定泛型类型的范围

泛型约束总共有五种。
约束 	说明
T:结构 	类型参数必须是值类型
T:类 	类型参数必须是引用类型;这一点也适用于任何类、接口、委托或数组类型。
T:new() 	类型参数必须具有无参数的公共构造函数。 当与其他约束一起使用时,new() 约束必须最后指定。
T:<基类名> 	类型参数必须是指定的基类或派生自指定的基类。
T:<接口名称> 	类型参数必须是指定的接口或实现指定的接口。 可以指定多个接口约束。 约束接口也可以是泛型的。
 
        where T : struct        //约束 T 必须是值类型  
        where K : class         //约束 K 必须是引用类型   
        where V : IFace<T>      //约束 V 必须实现 IFace 接口  
        where W : K             //约束 W 必须是 K 类型,或者是 K 类型的子类  
        where X : class, new()  //约束 X 必须是引用类型,并且有一个无参数的构造函数,当有多个约束时,new()必须写在最后  
where Y : MyClass2      //约束 Y 必须是 MyClass2 类型,或者继承于 MyClass2 类
 
 
 
 
    //例子:
internal class Program
    {
        static void Main(string[] args)
        {
 
            Person<int,string,IFaceClass,string> zhangsan = new Person<int,string,IFaceClass,string>();
 
            zhangsan.Age = 10;
            Person<float,Program,IFaceClass,Program> zhangsan1 = new Person<float,Program,IFaceClass,Program>();
            zhangsan1.Age = 10.0f;
 
            Person<double, People, IFaceClass, Man> zhangsan2 = new Person<double, People, IFaceClass, Man>();
            zhangsan1.Age = 10.0f;
        }
    }
 
    public class People:IComparable<People>{
         
        public int CompareTo(People other) {
 
            return 1;
           
        }
    }
    public class Man : People { 
    
    
    
    }
 
    public interface IFace<T> {
 
        void IFaceMothod();
 
 
    }
    public class IFaceClass:IFace<int> ,IFace<float>,IFace<double>{
 
        public void IFaceMothod() { 
            
        
        }
    }
    public class Person <T,K,V,W> 
        where T : struct 
        where K : class
        where V : IFace<T>
        where W : K
    {
 
        private T _age;
 
        public T Age {  get { return _age; }  set { _age = value; } }  
 
 
        public K TesT { get; set; }
   
    }
 
 
 
 
委托约束
枚举约束  后续再说

4.泛型成员因类型不确定,可能是类、结构体、字符、枚举……所以不能使用算术运算符、比较运算符等进行运算!
注意,可以使用赋值运算符。

5.泛型默认值问题 需要用   default()方法

 
之所以会用到default关键字,是因为需要在不知道类型参数为值类型还是引用类型的情况下,为对象实例赋初值。
 
引用类型会返回 null,对于数值类型会返回零。对于结构,此关键字将返回初始化为零或 null 的每个结构成员,具体取决于这些结构是值类型还是引用类型。对于可以为 null 的值类型,默认返回 System.Nullable<T>
 
 
 
///如果我们用int型来绑定泛型参数,那么T就是int型,
///那么注释的那一行就变成了 int t = null;显然这是无意义的。
///为了解决这一问题,引入了default关键字:
 
  TestDefault<int?> testDefault = new TestDefault<int?>();
  Console.WriteLine(testDefault.foo());
 
 class TestDefault<T>
    {
        public T foo()
        {
           // T t = null; //错误赋值方式 
            T t = default(T);
            return t;
        }
    }
在C#中ArrayList与List是常用的集合
ArrayList

ArrayList的优点:

    ArrayList大小是按照其中存储的数据来动态扩充与收缩的  长度不固定

    ArrayList可以很方便地进行数据的添加插入删除

   ArrayList 可以存储任意类型

 ArrayList aList = new ArrayList();
            //插入
            aList.Add(123);
            aList.Add("ABC");
            aList.Insert(1, 123 + "ABC"); //123 + "ABC" ->(隐式转换)"123ABC"
            //移除
            aList.RemoveAt(0); //索引处
            aList.Remove("ABC");
ArrayList的缺点:

    ArrayList在存储数据时使用object类型进行存储的

    ArrayList不是类型安全的,使用时很可能出现类型不匹配的错误
    就算都插入了同一类型的数据,使用时我们也需要将它们转化为对应的原类型来处理

    ArrayList 存储在装箱和拆箱操作,导致其性能低下

           //装箱
            int i = 123;
            object o = i;
 
            //拆箱
            object o1 = 123;
            int i1 = (int)o1; //强制类型装换

List  泛型集合

   List 与其他数组的比较:
             List与静态数组(Array类)比较类似,都用于存放一组相同类型的值。
             List与动态数组(ArrayList)比较类似   都是元素长度不固定 。

//创建字符串类型List的对象listStr
            List<string> listStr = new List<string>();
            //创建元素位置且添加元素
            listStr.Add("a");
            Console.WriteLine(listStr[0]);
            
            //修改对应位置的元素
            listStr[0] = "b";
            for (int i = 1; i <=10; i++)
            {
                listStr.Add(i.ToString());
            }
            //添加元素
             listStr.AddRange(listStr);
            foreach (var item in listStr)
            {
                Console.WriteLine(item);
            }
            listStr.Insert(0, "a");
            listStr.IndexOf("a");
            listStr.Remove("a");
            listStr.RemoveAt(0);
            listStr.RemoveAll(x => x == "a");
            listStr.FindAll(x => x == "a");
C#中的Dictionary字典类
特点:

    必须包含名空间System.Collection.Generic
    Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值)
    键必须是唯一的,而值不需要唯一的

    字典 长度是不固定的 随着元素增减 而改变
    键和值都可以是任何类型(比如:string, int, 自定义类型,等等)

         using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApp33
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //字典的对象
            Dictionary<string,int> dic = new Dictionary<string,int>();
            
 
 
 
            Dictionary<int,string>  dic1 = new Dictionary<int,string>();
            //添加元素
            dic1.Add(0, "a");
            dic1.Add(1, "b");
            dic1.Add(2, "c");
 
 
            dic.Add("1", 123);
            dic.Add("2", 333);
            dic.Add("abc", 333);
            dic.Add("3", 456);
            //查找
            Console.WriteLine(dic1[0]); 
            Console.WriteLine(dic["3"]);
            //修改
            dic1[2] ="d";
            dic["2"] = 444;
 
            for (int i = 0; i < dic1.Count; i++)
            {
 
                Console.WriteLine(dic1[i]);
 
            }
 
 
 
            //dic.Keys 集合
            foreach (string item in dic.Keys)
            {
                Console.WriteLine(dic[item]);
            }
 
 
            // dic.Values 集合
            foreach (int item in dic.Values)
            {
                Console.WriteLine(item);
            }
 
            //
            foreach (KeyValuePair<string,int> item in dic)
            {
                Console.WriteLine(item.Key);
                Console.WriteLine(item.Value);
            }
           移除元素
            bool isRmove =  dic.Remove("aaadsasdasd");
            if (isRmove)
            {
 
                Console.WriteLine("删除成功");
            }
            else {
                Console.WriteLine("删除失败");
 
            }
 
 
           // //判断字典中是否包含某个key
            bool isTrue =  dic.ContainsKey("2");
            if (isTrue)
            {
 
                dic.Remove("2");
            }
            else {
                dic.Add("2",11);
            
            }
         
           // //清空
            dic.Clear();
           // //字典中key/value的个数
            Console.WriteLine(dic.Count); 
            
         
            Console.ReadKey();
        }
    }
}

常用属性

    名称    说明
    Comparer     获取用于确定字典中的键是否相等的 IEqualityComparer<T>。
    Count        获取包含在 Dictionary<TKey, TValue> 中的键/值对的数目。
    Item         获取或设置与指定的键相关联的值。
    Keys         获取包含 Dictionary<TKey, TValue> 中的键的集合。
    Values       获取包含 Dictionary<TKey, TValue> 中的值的集合。

常用方法
    名称    说明
    Add                     将指定的键和值添加到字典中。
    Clear                    从 Dictionary<TKey, TValue> 中移除所有的键和值。
    ContainsKey         确定 Dictionary<TKey, TValue> 是否包含指定的键。
    ContainsValue       确定 Dictionary<TKey, TValue> 是否包含特定值。

    Remove              从 Dictionary<TKey, TValue> 中移除所指定的键的值。
    ToString            返回表示当前对象的字符串。 (继承自 Object。)

C# 结构体(Struct)

1.结构体是值类型数据结构。

2.引用类型派生自 System.Object ,而值类型均隐式派生自 System.ValueType

  // 数值类型
                Console.WriteLine(1 is ValueType); // true
                                                   // 布尔类型
                Console.WriteLine(true is ValueType); // true
                                                      // 结构类型
                Console.WriteLine(new Person() is ValueType); // true
                                                              // 枚举类型
                Console.WriteLine(EnumTest.Item1 is ValueType); // true

3.struct 关键字用于创建结构体

struct MyStruct2 {
          
        }

3.结构可带有方法、字段、属性、运算符,委托和事件。

struct MyStruct2 {
            private int A;
            public int Age { get; set; }
            public void Test() { };
            delegate void Delegate1();
            event  Delegate1 Delegate2;
 
            public static MyStruct2 operator -(MyStruct2 stu1, MyStruct2 stu2)
            {
 
                return new MyStruct2();        
              
            }
 
        }

4.结构可定义构造函数,但不能定义析构函数。不能定义无参构造函数。无参构造函数(默认)自带

如果定义有参构造函数 必须在 构造函数内部 初始化所有 字段和属性

 struct MyStruct2 {
            private int A;
            public int Age { get; set; }
            public void Test() { };
            public   delegate void Delegate1();
            public  event  Delegate1 Delegate2;
 
            public MyStruct2(int a,int age,Delegate1 delegate1) { 
            
                A = a;
                Age = age;
                Delegate2 = delegate1;
            }
            //~MyStruct() {   //错误
            
            
            //}
 
        }

5.结构可实现一个或多个接口  但是不能继承其他类

6.结构不支持被其他类继承。

6.结构成员不能指定为virtual 或 protected、

7.结构可以使用 New 创建对象 也可以不使用 ,如果结构中有属性 必须使用new 创建对象

10.结构可以嵌套在类中使用 也可以和类并列定义在命名空间中

类和结构总结:

          1. 类和结构实际上都是创建对象的模板, 每个对象都包含数据,并提供了处理和访问数据的方法
           2. 类是引用类型 对象存于堆中 可以通过GC管理内存   结构是值类型 对象存于栈中  、
           3  结构不能被继承  也不能继承其他类  但是能继承接口
           4. 结构和类 都能使用new 创建对象   但是结构也可以不使用
           5. 结构作为方法参数 默认是值传递   类类型默认是引用传递

枚举
作用:

枚举是描述一组整数值的结构  使数字更具有具体意义

1.枚举是值类型

2.枚举类型使用 enum 关键字声明的。

3.枚举是一组整型常量 默认是从 0开始  也可以自己定义范围

4.枚举使用enum关键字来声明,与类同级。枚举本身可以有修饰符,但枚举的成员始终是公共的,不能有访问修饰符。枚举本身的修饰符仅能使用public和internal。

5.枚举都是隐式密封的,不允许作为基类派生子类

  enum TestEnum {
        
        A =10, B, C=100, D, E, F
   
    }

委托和事件

委托

1.委托就是把方法(函数)变成一种引用类型的方式

2.委托 关键字 delegate(代理)

3.委托分为定义委托和使用委托

定义委托

1.定义委托和定义类一样可在命名空间中定义,也可以像变量一样在类的内部定义,

3.委托可以使用修饰符:public,private,protected等。

3.委托可以根据不同类型的方法(有参无参 有返回值 无返回值等)定义 多个委托类型

delegate void IntMethod(int x);
//定义了一个 无返回值有一个int类型参数的 委托
使用委托

//1. 创建委托类型的对象 用来管理某个符合类型的方法  并且使用委托对象 来调用方法

  delegate void VoidDelegate();
    delegate void VoidDelegate_1(int a);
    delegate void VoidDelegate_2(int a ,int b);
    internal class Program
    {
 
        static void Main(string[] args)
        {
 
            VoidDelegate  voidDelegate = new VoidDelegate(ProgramMothod);\
            voidDelegate();
            VoidDelegate_1  voidDelegate_1 = new VoidDelegate_1(ProgramMothod_1);
            voidDelegate_1(10);
            VoidDelegate_2 voidDelegate_2 = new VoidDelegate_2(ProgramMothod_2);
            voidDelegate_2(10, 20);
            Console.ReadKey();
        }
 
        public static void ProgramMothod() {
 
            Console.WriteLine("1");
 
        }
        public static void ProgramMothod_1(int a) {  
            
            Console.WriteLine(a); 
        
        }
        public static void ProgramMothod_2(int a, int b) {
 
            Console.WriteLine(a+b);
        }
    }
使用泛型委托
    delegate void VoidDelegate_3<T>(T a, T b);
    internal class Program
    {
 
        static void Main(string[] args)
        {
            VoidDelegate_3<string>  voidDelegate_3  =  new VoidDelegate_3<string> (ProgramMothod_3);
 
            VoidDelegate_3<int> voidDelegate_4 = new VoidDelegate_3<int>(ProgramMothod_2);
            Console.ReadKey();
        }
 
        public static void ProgramMothod_2(int a, int b) {
 
            Console.WriteLine(a+b);
        }
        public static void ProgramMothod_3(string a, string b)
        {
 
            Console.WriteLine(a + b);
        }
    }
使用委托作为方法参数
 
        static void Main(string[] args)
        {
 
            ProgramMothod_3(3, 4, ProgramMothod_2);
            Console.ReadKey();
        }
     
        public static int  ProgramMothod_2(int a, int b)
        {
            return  a + b;
        }
        public static void ProgramMothod_3(int w,int y,VoidDelegate_4 voidDelegate_4)
        {
            int temp = voidDelegate_4(w,y);
            Console.WriteLine(temp);
        }
    }
使用Action和Func委托

方法的返回类型和名字千千万万,无法对每个方法都去定义对应的委托,.net为了方便使用委托,定义了两个泛型委托。

Action

Action委托表示一个void返回类型的方法

Func

Func委托表示一个带返回类型的方法

至多可以传递16种不同类型的参数和一个返回类型。返回类型放在<>最后一个。

 static void Main(string[] args)
        {
 
            Action action1 = new Action(ProgramMothod_1);
            Action<int, int> action = new Action<int, int>(ProgramMothod_2);
         
            Func<int> func = new Func<int>(ProgramMothod_4);
            Func<int,int,int> func1 =new Func<int, int, int> (ProgramMothod_5);
            
            Console.ReadKey();
        }
        public static void ProgramMothod_1() { 
        
        }
        public static void ProgramMothod_2(int a, int b) {
 
            Console.WriteLine(a+b);
        }
        public static int ProgramMothod_4()
        {
            return 1;
        }
        public static int ProgramMothod_5(int a ,int b)
        {
            return a+b;
        }
    }
委托的多播

1.委托对象可使用“+”运算符进行合并。

2.“-”运算符可用于从合并的委托中移除组件委托

3.只有相同类型的委托可被合并

 internal class Program
    {
        static void Main(string[] args)
        {
            Action totalAction;
            Action action1 = new Action(ProgramMothod_1);
            Action action2 = new Action(ProgramMothod_2);
            totalAction = action1;
            totalAction += action2;
            totalAction();
            
            Console.ReadKey();
        }
        public static void ProgramMothod_1() { 
        
        }
        public static void ProgramMothod_2()
        {
 
        }

使用委托作为方法的参数

 internal class Program
    {
        static void Main(string[] args)
        {
            ProgramMothod_1(ProgramMothod_2);
            
            Console.ReadKey();
        }
        public static void ProgramMothod_1(Action action) { 
              
             action();
        }
        public static void ProgramMothod_2()
        {
            Console.WriteLine("11");
        }
事件

1.事件基于委托的,可以为任何一种委托提供一种发布\订阅机制。(类似委托多播)
2.使用event关键字将一个委托类型定义为事件  事件就是委托的一个对象

 internal class Program
    {
        static void Main(string[] args)
        {
            Heater  heater = new Heater();
            heater.BoilEvent += Heater1;  
            heater.BoilEvent += Heater2; //添加事件
 
            heater.BoilWater();//执行事件
        }
        
        public static void Heater1(int x) {
 
            Console.WriteLine("水已经{0}度了,可以用茶杯接水了",x);
 
        }
        public static void Heater2(int x)
        {
 
            Console.WriteLine("水已经{0}度了,可以用大桶接水了", x);
 
        }
    }
    //烧开水事件 类
    public class Heater
    {
        private int temperature;//水温
        public delegate void BoilHandle(int x);//声明关于事件的委托
        public event BoilHandle BoilEvent;//声明水要烧开的事件
        public void BoilWater()
        { //烧水的方法
            for (int i = 0; i <= 100; i++)
            {
                temperature = i;
                if (temperature > 96)
                {
                    if (BoilEvent != null)
                    {
                        BoilEvent(temperature);
                    }
                }
            }
        }
 
    }

普通匿名函数

1.针对委托的使用

2.可以快捷的使委托实例化。
3.不建议再使用匿名函数,C#3.0后使用lambda表达式替代匿名函数

匿名函数的写法
:
 
class Program
    {
        delegate int MyDelegate(int a, int b);
        static void Main(string[] args)
        {
            MyDelegate md = delegate(int a, int b) { return a + b; };
            int sum = md(1,2);
            Console.WriteLine(sum);
        }
    }
事件与普通匿名函数
nternal class Program
    {
        static void Main(string[] args)
        {
            Heater  heater = new Heater();
            heater.BoilEvent += delegate (int x)   //添加匿名函数
            {
 
                Console.WriteLine("水已经{0}度了,可以用茶杯接水了", x);
            };
            heater.BoilEvent += delegate (int x)  //添加匿名函数
            {
 
                Console.WriteLine("水已经{0}度了,可以用大桶接水了", x);
            };
 
            heater.BoilWater();//执行事件
        }
 
    }
    public class Heater
    {
        private int temperature;//水温
        public delegate void BoilHandle(int x);//声明关于事件的委托
        public event BoilHandle BoilEvent;//声明水要烧开的事件
        public void BoilWater()
        { //烧水的方法
            for (int i = 0; i <= 100; i++)
            {
                temperature = i;
                if (temperature > 96)
                {
                    if (BoilEvent != null)
                    {
                        BoilEvent(temperature);
                    }
                }
            }
        }
 
    }
lambda表达式
1.普通匿名函数的升级版本  (箭头函数)

2.比普通匿名函数更为简洁,数据类型可以不用写 

class Program
    {
        delegate int MyDelegate(int a, int b);
        static void Main(string[] args)
        {
            MyDelegate md = (a,b)=>{return a+b;};
            int sum = md(1, 2);
            Console.WriteLine(sum);
        }
    }
事件与lambda表达式
internal class Program
    {
        static void Main(string[] args)
        {
            Heater  heater = new Heater();
            heater.BoilEvent += x =>
            {
                Console.WriteLine("水已经{0}度了,可以用茶杯接水了", x);
 
            };
            heater.BoilEvent += x=> //添加匿名函数
            {
 
                Console.WriteLine("水已经{0}度了,可以用大桶接水了", x);
            };
            heater.BoilWater();//执行事件
            Console.ReadKey();
        }
 
    }
    public class Heater
    {
        private int temperature;//水温
        public delegate void BoilHandle(int x);//声明关于事件的委托
        public event BoilHandle BoilEvent;//声明水要烧开的事件
        public void BoilWater()
        { //烧水的方法
            for (int i = 0; i <= 100; i++)
            {
                temperature = i;
                if (temperature > 96)
                {
                    if (BoilEvent != null)
                    {
                        BoilEvent(temperature);
                    }
                }
            }
        }
 
    }
.运算符重载
1.含义

  是对已有的运算符重新定义新的运算规则,以适应不同的数据类型

特点:
              1.运算符重载的声明方式:operator 关键字
              2.必须用public修饰且必须是类的静态的方法
              3.重载运算符的方法 不用主动调用 只要使用重载的运算符就相当调用了方法
              4.运算符只能采用值参数,不能采用ref或out参数
              5.重载运算符的返回值不一定必须是自己,但一定不能是void

运算符     可重载性
!  ++、--、     可以重载这些一元运算符
 +、-、*、/、%、&、|  可以重载这些二元运算符
 ==、!=、<、>、<=、>=      可以重载比较运算符,必须成对重载
 &&、||      不能重载条件逻辑运算符,但可以使用能够重载的&和|进行计算
 +=、-=、*=、/=、%=、&=、|=、^=、<<=、>>=      不能显式重载赋值运算符,在重写单个运算符如+、-、%时,它们会被  隐式重写
 =、.、?:、->、new、is、sizeof、typeof       ()   []     不能重载这些运算符

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值