C#中NEW的具体用法

来源:百度知道 编辑:UC知道 时间:2024/06/05 20:40:53
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication9
{
public enum genders//定义性别枚举
{
female = 0,
male
}
public class person//人类
{
public person()//重写构造函数
{
this._name = "默认姓名";
this._age = 20;
this._gender = genders.male;
}
protected string _name;
protected int _age;
protected genders _gender;
public virtual void introducemyself()//虚函数,介绍自己的方法
{
System.Console.WriteLine("person.introducemyself()");
}
}
public class chineseperson : person//中国人继承了人类
{
public chineseperson()//中国人类
: base()//访问父类构造函数
{
this._name = "默认中文名";

public new void introducemyself()//。。。NEW?
这里的new的意思是此方法不是父类中的introducemyself(),而是新的方法,但是这个方法和父类中的introducemyself()同名,为了区别,加了个new,意思是新的方法

new 表示新建一个对象(类的实例)。
new 人(),表示新建了一个人的实例(如张三这个人)。

new完了之后,这个对象需要有个指针记录它,否则就是生了小孩子就扔野地里了,再也找不回来。
因此,要用一个指针或是父类的指针指向它,便于以后使用。
如 person aperson = new chineseperson();