• NEW关键字的三种用法


    声明:本文最初是本人从他出转载到51CTO上的一篇文章,但现在记不清最初从出处了,原文作者看到还请原来,现在发表在这里只为学习,本人在51CTO的该文章的地址为:http://kestrelsaga.blog.51cto.com/3015222/751536

    一、             new运算符
    ①     用于创建对象和调用构造函数。 例如:
     
    1. Class1 obj  = new Class1(); 
    ②     还可用于创建匿名类型的实例:
    1. var query = from cust in customers  
    2.             select new {Name = cust.Name, Address = cust.PrimaryAddress};  
      ③     new 运算符还用于调用值类型的默认构造函数。 例如:
     
    1. int i = new int(); 
    在上一个语句中,i 初始化为 0,它是 int 类型的默认值。 该语句的效果等同于:
     
    1. int i = 0; 
             请记住,为结构声明默认的构造函数是错误的,因为每一个值类型都隐式具有一个公共的默认构造函数。 可以在结构类型上声明参数化构造函数以设置其初始值,但是,只有在需要默认值之外的值时才必须这样做。
             值类型对象(例如结构)是在堆栈上创建的,而引用类型对象(例如类)是在堆上创建的。 两种类型的对象都是自动销毁的,但是,基于值类型的对象是在超出范围时销毁,而基于引用类型的对象则是在对该对象的最后一个引用被移除之后在某个不确定的时间销毁。 对于占用固定资源(例如大量内存、文件句柄或网络连接)的引用类型,有时需要使用确定性终止以确保对象被尽快销毁。
            不能重载 new 运算符。
            如果 new 运算符分配内存失败,将引发异常 OutOfMemoryException
    在下面的示例中,通过使用 new 运算符创建并初始化一个 struct 对象和一个类对象,然后为它们赋值。 显示了默认值和所赋的值。
     
    1. struct SampleStruct  
    2. {  
    3.    public int x;  
    4.    public int y;  
    5.  
    6.    public SampleStruct(int x, int y)  
    7.    {  
    8.       this.x = x;  
    9.       this.y = y;  
    10.    }  
    11. }  
    12.  
    13. class SampleClass  
    14. {  
    15.    public string name;  
    16.    public int id;  
    17.  
    18.    public SampleClass() {}  
    19.  
    20.    public SampleClass(int id, string name)  
    21.    {  
    22.       this.id = id;  
    23.       this.name = name;  
    24.    }  
    25. }  
    26.  
    27. class ProgramClass  
    28. {  
    29.    static void Main()  
    30.    {  
    31.       // Create objects using default constructors:  
    32.       SampleStruct Location1 = new SampleStruct();  
    33.       SampleClass Employee1 = new SampleClass();  
    34.  
    35.       // Display values:  
    36.       Console.WriteLine("Default values:");  
    37.       Console.WriteLine("   Struct members: {0}, {1}",  
    38.              Location1.x, Location1.y);  
    39.       Console.WriteLine("   Class members: {0}, {1}",  
    40.              Employee1.name, Employee1.id);  
    41.  
    42.       // Create objects using parameterized constructors:  
    43.       SampleStruct Location2 = new SampleStruct(10, 20);  
    44.       SampleClass Employee2 = new SampleClass(1234, "Cristina Potra");  
    45.  
    46.       // Display values:  
    47.       Console.WriteLine("Assigned values:");  
    48.       Console.WriteLine("   Struct members: {0}, {1}",  
    49.              Location2.x, Location2.y);  
    50.       Console.WriteLine("   Class members: {0}, {1}",  
    51.              Employee2.name, Employee2.id);  
    52.    }  
    53. }  
    54. /*  
    55. Output:  
    56. Default values:  
    57.    Struct members: 0, 0  
    58.    Class members: , 0  
    59. Assigned values:  
    60.    Struct members: 10, 20  
    61.    Class members: Cristina Potra, 1234  
    62. */ 
     注意,在示例中字符串的默认值为 null 因此未显示它。
     
    二、             new修饰符
    在用作修饰符时,new 关键字可以显式隐藏从基类继承的成员。 隐藏继承的成员时,该成员的派生版本将替换基类版本。 虽然可以在不使用 new 修饰符的情况下隐藏成员,但会生成警告。 如果使用 new 显式隐藏成员,则会取消此警告,并记录要替换为派生版本这一事实。
    若要隐藏继承的成员,请使用相同名称在派生类中声明该成员,并使用 new 修饰符修饰该成员。 例如: 
     
    1. public class BaseC  
    2. {  
    3.     public int x;  
    4.     public void Invoke() { }  
    5. }  
    6. public class DerivedC : BaseC  
    7. {  
    8.     new public void Invoke() { }  
    9. }  
    在此示例中,DerivedC.Invoke 隐藏了 BaseC.Invoke。 字段 x 不受影响,因为它没有被类似名称的字段隐藏。
    通过继承隐藏名称采用下列形式之一:
    ·             引入类或结构中的常数、指定、属性或类型隐藏具有相同名称的所有基类成员。
    ·             引入类或结构中的方法隐藏基类中具有相同名称的属性、字段和类型。 同时也隐藏具有相同签名的所有基类方法。
    ·             引入类或结构中的索引器将隐藏具有相同名称的所有基类索引器。
    对同一成员同时使用 new 和 override 是错误的做法,因为这两个修饰符的含义互斥。 new 修饰符会用同样的名称创建一个新成员并使原始成员变为隐藏的。 override 修饰符会扩展继承成员的实现。
    在不隐藏继承成员的声明中使用 new 修饰符将会生成警告。
    在该例中,基类 BaseC 和派生类 DerivedC 使用相同的字段名 x,从而隐藏了继承字段的值。 该示例演示了 new 修饰符的用法。 另外还演示了如何使用完全限定名访问基类的隐藏成员。见:
     
    1. public class BaseC  
    2. {  
    3.     public static int x = 55;  
    4.     public static int y = 22;  
    5. }  
    6.  
    7. public class DerivedC : BaseC  
    8. {  
    9.     // Hide field 'x'.  
    10.     new public static int x = 100;  
    11.  
    12.     static void Main()  
    13.     {  
    14.         // Display the new value of x:  
    15.         Console.WriteLine(x);  
    16.  
    17.         // Display the hidden value of x:  
    18.         Console.WriteLine(BaseC.x);  
    19.  
    20.         // Display the unhidden member y:  
    21.         Console.WriteLine(y);  
    22.     }  
    23. }  
    24. /*  
    25. Output:  
    26. 100  
    27. 55  
    28. 22  
    29. */ 
    在此示例中,嵌套类隐藏了基类中同名的类。 此示例演示了如何使用 new 修饰符来消除警告消息,以及如何使用完全限定名来访问隐藏的类成员。见:
     
    1. public class BaseC   
    2. {  
    3.     public class NestedC   
    4.     {  
    5.         public int x = 200;  
    6.         public int y;  
    7.     }  
    8. }  
    9.  
    10. public class DerivedC : BaseC   
    11. {  
    12.     // Nested type hiding the base type members.  
    13.     new public class NestedC     
    14.     {  
    15.         public int x = 100;  
    16.         public int y;   
    17.         public int z;  
    18.     }  
    19.  
    20.     static void Main()   
    21.     {  
    22.         // Creating an object from the overlapping class:  
    23.         NestedC c1  = new NestedC();  
    24.  
    25.         // Creating an object from the hidden class:  
    26.         BaseC.NestedC c2 = new BaseC.NestedC();  
    27.  
    28.         Console.WriteLine(c1.x);  
    29.         Console.WriteLine(c2.x);     
    30.     }  
    31. }  
    32. /*  
    33. Output:  
    34. 100  
    35. 200  
    36. */ 
    如果移除 new 修饰符,该程序仍可编译和运行,但您会收到以下警告:The keyword new is required on 'MyDerivedC.x' because it hides inherited member 'MyBaseC.x'.
    三、             new约束
    new 约束指定泛型类声明中的任何类型参数都必须有公共的无参数构造函数。 如果要使用 new 约束,则该类型不能为抽象类型。
    当泛型类创建类型的新实例,请将 new 约束应用于类型参数,如下面的示例所示:
     
    1. class ItemFactory<T> where T : new()  
    2.     {  
    3.         public T GetNewItem()  
    4.         {  
    5.             return new T();  
    6.         }  
    7. }  
    当与其他约束一起使用时,new() 约束必须最后指定:
    1. public class ItemFactory2<T>  
    2.         where T : IComparable, new()  
    3.     {  
    4.     }  
  • 相关阅读:
    OpenGL使用笔记-数学函数
    OpenGL--windows<vs2019>配置
    CTF学习记录--Wireshark抓包工具使用说明
    最小公倍数(LCM)
    CTF学习记录--抓包工具BurpSuite
    CTF学习记录--Robots协议
    ubuntu安装过程中的一些问题
    hadoop优化之拙见
    hadoop map-red的执行过程
    namenode需要升级
  • 原文地址:https://www.cnblogs.com/KeSaga/p/4354060.html
Copyright © 2020-2023  润新知