美文网首页
.net New有几种用法

.net New有几种用法

作者: 张中华 | 来源:发表于2018-11-21 21:37 被阅读37次

3种。
1)new 运算符:用于创建对象和调用构造函数。
2)new 修饰符:在用作修饰符时,new 关键字可以显式隐藏从基类继承的成员。
3)new 约束:用于在泛型声明中约束可能用作类型参数的参数的类型。
针对2,在使用一个类继承一个基类,并且写了同样的方法,此时会正常的使用,但是会提示,建议使用new关键字,显示隐藏。
对同一成员同时使用 new 和 override 是错误的做法,因为这两个修饰符的含义互斥。 new 修饰符会用同样的名称创建一个新成员并使原始成员变为隐藏的。 override 修饰符会扩展继承成员的实现。

在不隐藏继承成员的声明中使用 new 修饰符将会生成警告。

针对3,new约束指定泛型类声明中的任何类型参数都必须具有公共的无参数构造函数。

class ClassFour<T> where T : new()
    {
        public T GetNewItem()
        {
            return new T();
        }
    }

ClassFour<Employee> EmployeeFactory = new ClassFour<Employee>();
////此处编译器会检查Employee是否具有公有的无参构造函数。
//若没有则会有The Employee must have a public parameterless constructor 错误。

Code Practice:


Code:

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

namespace TheWayToUseNew
{
    public class ClassOne
    {
        public void PrintName()
        {
            Console.WriteLine("Name");
        }
    }

    public class ClassTwo : ClassOne
    {
        new public void PrintName()
        {
            Console.WriteLine("NameTwo");
        }

    }

    public class ClassThree : ClassOne
    {
        public void PrintName()
        {
            Console.WriteLine("NameThree");
        }

    }

    class ClassFour<T> where T : new()
    {
        public T GetNewItem()
        {
            return new T();
        }
    }
    public class Employee
    {
        private string name;
        private int id;

        public Employee()
        {
            name = "Temp";
            id = 0;
        }

        public Employee(string s, int i)
        {
            name = s;
            id = i;
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public int ID
        {
            get { return id; }
            set { id = value; }
        }
    }

    class Program
    {

        static void Main(string[] args)
        {
            // first way for instantiation
            ClassOne classOne = new ClassOne();
            classOne.PrintName();

            // second way to hide base class method
            ClassTwo classTwo = new ClassTwo();
            classTwo.PrintName();

            // third way to not hide base class method
            ClassThree classThree = new ClassThree();
            classThree.PrintName();

            // fourth way to constraint genericity
            ClassFour<Employee> EmployeeFactory = new ClassFour<Employee>();
            ////此处编译器会检查Employee是否具有公有的无参构造函数。
            //若没有则会有The Employee must have a public parameterless constructor 错误。
            Console.WriteLine("{0}'ID is {1}.", EmployeeFactory.GetNewItem().Name, EmployeeFactory.GetNewItem().ID);

            Console.ReadLine();

        }
    }
}

相关文章

  • .net New有几种用法

    3种。1)new 运算符:用于创建对象和调用构造函数。2)new 修饰符:在用作修饰符时,new 关键字可以显式隐...

  • 2018年9月5日.NET笔试面试题

    new 有几种用法?new Class();覆盖方法 public new XXXX(){}new 约束指定泛型类...

  • Selenium基本用法汇总

    用法声明 三种等待方式 几种元素定位方式 滚动窗口 https://blog.csdn.net/zwq912318...

  • Synchronized 有几种用法

    我们都知道 Synchronized 是线程安全同步用的,大部分程序可能只会用到同步方法上面。其实 Synchro...

  • 箭头函数

    这个是几种用法分为为是否有参数的

  • JS 里的数组

    数组用法 在说数组的用法前先研究下string的用法和加new的用法 由此可见,直接使用和new的区别是:没有ne...

  • js面向对象编程概述

    问:在js里面被 new 之后的函数和普通的函数有何区别吗? 1. New命令# 1.1 基本用法 new 命令的...

  • java创建对象的几种方式

    java中创建对象有几种方式? 使用new关键字 如 User user=new User(); 执行这条语句,j...

  • 关于android 线程

    thread 线程几种状态 创建(new) Thread thread=new Thread; 就绪(runnab...

  • new/delete用法

    new用法 1,开辟单变量地址空间 int *p = new int ;//开辟大小为sizeof(int)空间 ...

网友评论

      本文标题:.net New有几种用法

      本文链接:https://www.haomeiwen.com/subject/yxlhqqtx.html