using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace day2
{
class Program
{
static void Main(string[] args)
{
Student st = new Student(1);
Console.WriteLine(st.Id);
}
}
class Student
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
public Student(int cid) /*构造函数,如果不写此方法,会使用默认构造函数。构
造函数的函数名必须与类名一致*/
{
this.id = cid;
}
}
}
构造函数 :
是一种特殊的方法 主要用来在创建对象时初始化对象 即为对象成员变量赋初始值,总与new运算
符一起使用在创建对象的语句中 特别的一个类可以有多个构造函数 可根据其参数个数的不同或参数类型
的不同来区分它们 即构造函数的重载
new 的用法:
new 关键字:
(1)new 运算符 用于创建对象和调用构造函数。
(2)new 修饰符 用于隐藏基类成员的继承成员。
(3)new 约束 用于在泛型声明中约束可能用作类型参数的参数的类型。
new 运算符:
1.用于创建对象和调用构造函数
例:Class_Test MyClass = new Class_Test();
2.也用于为值类型调用默认的构造函数
例:int myInt = new int();
myInt 初始化为 0,它是 int 类型的默认值。该语句的效果等同于:int myInt = 0;
3.不能重载 new 运算符。
4.如果 new 运算符分配内存失败,则它将引发 OutOfMemoryException 异常。
new 修饰符
使用 new 修饰符显式隐藏从基类继承的成员。若要隐藏继承的成员,请使用相同名称在派生类中声明该成
员,并用 new 修饰符修饰它。
new 和 override的区别:
class Program
{
static void Main(string[] args)
{
contact ct1 = new class1();
contact ct2 = new class2();
ct1.prinf();
ct2.prinf();
}
new在用作修饰符时,new关键字可以在派生类中隐藏基类的方法,也就说在使用派生类的方法是
调用的方法是New关键字新定义出来的方法,而不是基类的方法。在不使用New关键字来隐藏基类方法也是
可以的,编译器会出现一个警告,提示如果有意去隐藏基类的方法,请使用New关键字修饰。
Override关键字主要是提供派生类对基类方法的新实现,重写的基类方法必须和Override的方法
具有相同的签名,此关键字不可以用于重写非虚方法和静态方法,与其配套使用的关键字是Virtual、
abstract、Override。与此同时,Override方法还不可以修改Virtual方法的可访问性,Override方法和
Virtual方法必须具有相同的访问修饰符,不能使用修饰符 new、static、virtual 或 abstract 来修改
override 方法。
}
abstract public class contact
{
public virtual void prinf()
{
Console.WriteLine ("这是虚方法");
}
}
public class class1 : contact
{
public override void prinf()
{
Console.WriteLine ("这是新的方法");
}
}
public class class2 : contact
{
public new void prinf()
{
Console.WriteLine ("这是另一个新的方法");
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace day2
{
class Program
{
static void Main(string[] args)
{
Student st = new Student(1);
Console.WriteLine(st.Id);
}
}
class Student
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
public Student(int cid) /*构造函数,如果不写此方法,会使用默认构造函数。构
造函数的函数名必须与类名一致*/
{
this.id = cid;
}
}
}
构造函数 :
是一种特殊的方法 主要用来在创建对象时初始化对象 即为对象成员变量赋初始值,总与new运算
符一起使用在创建对象的语句中 特别的一个类可以有多个构造函数 可根据其参数个数的不同或参数类型
的不同来区分它们 即构造函数的重载
new 的用法:
new 关键字:
(1)new 运算符 用于创建对象和调用构造函数。
(2)new 修饰符 用于隐藏基类成员的继承成员。
(3)new 约束 用于在泛型声明中约束可能用作类型参数的参数的类型。
new 运算符:
1.用于创建对象和调用构造函数
例:Class_Test MyClass = new Class_Test();
2.也用于为值类型调用默认的构造函数
例:int myInt = new int();
myInt 初始化为 0,它是 int 类型的默认值。该语句的效果等同于:int myInt = 0;
3.不能重载 new 运算符。
4.如果 new 运算符分配内存失败,则它将引发 OutOfMemoryException 异常。
new 修饰符
使用 new 修饰符显式隐藏从基类继承的成员。若要隐藏继承的成员,请使用相同名称在派生类中声明该成
员,并用 new 修饰符修饰它。
new 和 override的区别:
class Program
{
static void Main(string[] args)
{
contact ct1 = new class1();
contact ct2 = new class2();
ct1.prinf();
ct2.prinf();
}
new在用作修饰符时,new关键字可以在派生类中隐藏基类的方法,也就说在使用派生类的方法是
调用的方法是New关键字新定义出来的方法,而不是基类的方法。在不使用New关键字来隐藏基类方法也是
可以的,编译器会出现一个警告,提示如果有意去隐藏基类的方法,请使用New关键字修饰。
Override关键字主要是提供派生类对基类方法的新实现,重写的基类方法必须和Override的方法
具有相同的签名,此关键字不可以用于重写非虚方法和静态方法,与其配套使用的关键字是Virtual、
abstract、Override。与此同时,Override方法还不可以修改Virtual方法的可访问性,Override方法和
Virtual方法必须具有相同的访问修饰符,不能使用修饰符 new、static、virtual 或 abstract 来修改
override 方法。
}
abstract public class contact
{
public virtual void prinf()
{
Console.WriteLine ("这是虚方法");
}
}
public class class1 : contact
{
public override void prinf()
{
Console.WriteLine ("这是新的方法");
}
}
public class class2 : contact
{
public new void prinf()
{
Console.WriteLine ("这是另一个新的方法");
}
}