NEW关键字的三种用法
声明:本文最初是本人从他出转载到51CTO上的一篇文章,但现在记不清最初从出处了,原文作者看到还请原来,现在发表在这里只为学习,本人在51CTO的该文章的地址为:http://kestrelsaga.blog.51cto.com/3015222/751536
- Class1 obj = new Class1();
- var query = from cust in customers
- select new {Name = cust.Name, Address = cust.PrimaryAddress};
- int i = new int();
- int i = 0;
- struct SampleStruct
- {
- public int x;
- public int y;
- public SampleStruct(int x, int y)
- {
- this.x = x;
- this.y = y;
- }
- }
- class SampleClass
- {
- public string name;
- public int id;
- public SampleClass() {}
- public SampleClass(int id, string name)
- {
- this.id = id;
- this.name = name;
- }
- }
- class ProgramClass
- {
- static void Main()
- {
- // Create objects using default constructors:
- SampleStruct Location1 = new SampleStruct();
- SampleClass Employee1 = new SampleClass();
- // Display values:
- Console.WriteLine("Default values:");
- Console.WriteLine(" Struct members: {0}, {1}",
- Location1.x, Location1.y);
- Console.WriteLine(" Class members: {0}, {1}",
- Employee1.name, Employee1.id);
- // Create objects using parameterized constructors:
- SampleStruct Location2 = new SampleStruct(10, 20);
- SampleClass Employee2 = new SampleClass(1234, "Cristina Potra");
- // Display values:
- Console.WriteLine("Assigned values:");
- Console.WriteLine(" Struct members: {0}, {1}",
- Location2.x, Location2.y);
- Console.WriteLine(" Class members: {0}, {1}",
- Employee2.name, Employee2.id);
- }
- }
- /*
- Output:
- Default values:
- Struct members: 0, 0
- Class members: , 0
- Assigned values:
- Struct members: 10, 20
- Class members: Cristina Potra, 1234
- */
- public class BaseC
- {
- public int x;
- public void Invoke() { }
- }
- public class DerivedC : BaseC
- {
- new public void Invoke() { }
- }
- public class BaseC
- {
- public static int x = 55;
- public static int y = 22;
- }
- public class DerivedC : BaseC
- {
- // Hide field 'x'.
- new public static int x = 100;
- static void Main()
- {
- // Display the new value of x:
- Console.WriteLine(x);
- // Display the hidden value of x:
- Console.WriteLine(BaseC.x);
- // Display the unhidden member y:
- Console.WriteLine(y);
- }
- }
- /*
- Output:
- 100
- 55
- 22
- */
- public class BaseC
- {
- public class NestedC
- {
- public int x = 200;
- public int y;
- }
- }
- public class DerivedC : BaseC
- {
- // Nested type hiding the base type members.
- new public class NestedC
- {
- public int x = 100;
- public int y;
- public int z;
- }
- static void Main()
- {
- // Creating an object from the overlapping class:
- NestedC c1 = new NestedC();
- // Creating an object from the hidden class:
- BaseC.NestedC c2 = new BaseC.NestedC();
- Console.WriteLine(c1.x);
- Console.WriteLine(c2.x);
- }
- }
- /*
- Output:
- 100
- 200
- */
- class ItemFactory<T> where T : new()
- {
- public T GetNewItem()
- {
- return new T();
- }
- }
- public class ItemFactory2<T>
- where T : IComparable, new()
- {
- }
转载于:https://www.cnblogs.com/KeSaga/p/4354060.html
NEW关键字的三种用法相关推荐
- 【零基础学Java】—this关键字的三种用法+Java继承的三个特点(二十一)
[零基础学Java]-this关键字的三种用法+Java继承的三个特点(二十一) 一.this关键字的三种用法 在本类的成员方法中,访问本类的成员变量 在本类的成员方法中,访问本类的另一个成员方法 在 ...
- java this()函数_java中this关键字的三种用法
this是自身的一个对象,代表对象本身,可以理解为:指向对象本身的一个指针. this的用法在java中大体可以分为3种: 1.普通的直接引用,this相当于是指向当前对象本身. 2.形参与成员名字重 ...
- Java中的this关键字(三种用法)
this关键字的三种用法: 通过this关键字可以明确的访问一个类的成员变量,解决成员变量与局部变量名称冲突问题. 实例: public class Student {//定义四个成员变量String ...
- 微课系列(6):Python关键字else的三种用法
技术要点:Python关键字else在选择结构.循环结构和异常处理结构中的三种用法. ----------相关阅读---------- 教学课件 1900页Python系列PPT分享一:基础知识(10 ...
- static的三种用法,定义静态变量,静态函数,静态代码块!
static的三种用法,定义静态变量,静态函数,静态代码块! 1.定义静态变量 class PersonStatic { //静态变量的特点 //1.静态变量无需生成对象就可被调用,可以使用类名和对象 ...
- 【零基础学Java】—final关键字与四种用法(二十九)
[零基础学Java]-final关键字与四种用法(二十九) 一.final关键字 final关键字代表最终.不可改变的 常见的四种用法: 可以用来修饰一个类 可以用来修饰一个方法 可以用来修饰一个局部 ...
- this关键字的四种用法
this关键字的四种用法 一.this.属性名 表示对象自己的属性使用this调用成员变量,解决变量与局部变量之间的冲突问题 1.实例分析 代码如下(示例): public class Person{ ...
- Kotlin object的三种用法
文章目录 Kotlin object的三种用法 概述 object 匿名内部类 object 单例模式 @JvmStatic companion object 伴生对象 工厂模式 Kotlin obj ...
- java里break的使用方法_Java中break的第三种用法说明
在Java中,break语句有三种用法,第一种是用于终止switch语句中的语句序列,第二种是用于退出循环,然而第三种是用作goto语句的"文明"形式! 我们知道,goto语句会破 ...
最新文章
- 再见 FTP/SFTP,是时候拥抱下一代文件传输利器 Croc 了!
- 温泉关一役历史资料(电影:斯巴达300勇士)
- 浅谈CMMI几个过程概念流程管理 (转)
- 建站需要mysql_你还在对建站数据不重视吗-Navicat for MySQL赶快上手使用吧
- MySql---数据库笔记(功能齐全)
- SAP ABAP Netweaver里进程能够申请的最大内存
- 前端学习(1645):前端系列实战课程之留言板功能实现
- 火速拿来用!对比近 10,000 个 Python 开源项目发现最实用的 TOP34!
- 安装McAfee 8.7i 提示错误1920怎么办?
- c# 第七节 编程规范,vs中的各种设置
- 【转】Sections Headers for Android ListViews
- XShell/Xftp免费版下载及安装教程
- 服装制版师用软件代替手工_楚天都市报_多媒体报
- 吐血分享9个软件下载网站以及那些开发者大佬的传奇人生
- 为Springboot工作流版本myabaits的xml 建表
- 重磅!Google推出了AI人体图像分割工具,惊呆了小伙伴.....
- C 语言 数据类型及所占字节数
- 加大货币发行量 quantitative easing
- 身份证识别SDK——混合非原生调用
- 基于微信小程序食谱大全系统(微信小程序毕业设计)
热门文章
- sendmail服务器案例配置
- C# 视频监控系列(9):服务器端——数据捕获(抓图 + 录像)
- 反编译工具jad简单用法
- 金邦黑金刚4G内存 VS Vista系统
- slf4j 日志监控
- Azure系列2.1.15 —— SharedAccessBlobPolicy
- C# 值类型与引用类型
- “.Net 社区虚拟大会”(dotnetConf) 2016 Day 3 Keynote: Scott Hanselman
- Maven+mybatis教程
- 轻量级持久层V2版本代码与模板