如下关键字:
key_static
key_new
key_this
key_delegate
环境VS2010 C#控制台程序 张永辉 2012年11月9日
----------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace cs1
{
//-----------------------static 与 非static-----------------------------------------------------
class class1
{
public static String staticStr = "staticStr"; // static 在所属类被装载时创建
public String notstaticStr = "notstaticStr"; //非static 在类被实例化时创建
}
class key_static
{
public key_static()
{
Console.WriteLine("key_static==================");
Console.WriteLine(class1.staticStr); // static通过类进行访问
class1 class1eg1 = new class1(); // static所属类的所有实例的同一静态变量都是同一个值
class1 class1eg2 = new class1();
class1eg1.notstaticStr = "notstaticStr1"; //非static通过对象进行访问
class1eg2.notstaticStr = "notstaticStr1"; //非static不是同一个值
}
}
//----------------------- new ----------------------------------------------------------------
//new 修饰符只能用于继承类,一般用于弥补基类设计的不足
//new 修饰符和 override 修饰符不可同时用在一个成员上,因为这两个修饰符在含义上互相排斥
//new 操作符用于实例化一个类型
//e.g
class BaseClass
{
//1基类设计者声明了一个PI的公共变量,方便进行运算
public static double PI = 3.1415;
}
class DervieClass : BaseClass
{
//继承类发现PI的值精度不够,于是通过new修饰符 显示隐藏基类中的声明
public new static double PI = 3.14159265;
}
class key_new
{
public key_new()
{
Console.WriteLine("key_new=====================");
Console.WriteLine(BaseClass.PI);
Console.WriteLine(DervieClass.PI);
}
}
//----------------------- this ----------------------------------------------------------------
class class2
{
//在此的this定义为:
//class2* const thhis; 所以,this是一个常量,不能this++
//this仅限于构造函数和方法成员中使用
private double value; //会为每个new的class分配内存在堆上
private string str;
//构造函数:
public class2(double value)
{
//限定同名的隐藏成员
this.value = value;
}
//成员函数.
public void class2do(string newstr)
{
str = newstr; //这里等同于 this.str = newstr;
}
//this不能用于静态成员的实现里,因为这时对象或结构并未实例化,
public class2(class2 mythis) //
{
//用对象本身实例化自己没有意义
if (this != mythis)
{
Console.WriteLine("this==this");//为什么会相等?
value += 100;
}
}
}
class key_this
{
public key_this()
{
Console.WriteLine("key_this====================");
class2 c2 = new class2(55.6);
class2 c3 = new class2(c2);
c2.class2do("zhang");
c3.class2do("yong");
}
}
//----------------------- sealed --------------------------------------------------------------
//sealed 修饰符表示密封, 表示该类不能再被继承,不能和 abstract 同时使用
//用于方法和属性时,表示该方法或属性不能再被继承,
//必须和 override 关键字一起使用,因为使用 sealed 修饰符的方法或属性肯定是基类中相应的虚成员
class class3
{
class A
{
public virtual void F()
{
Console.WriteLine("A.F");
}
public virtual void G()
{
Console.WriteLine("A.G");
}
}
}
class key_sealed
{
public key_sealed()
{
Console.WriteLine("key_sealed==================");
class3 c3 = new class3();
}
}
//--------------------Delegate 委托-------------------------------------------------------------
//Delegate它实际上是一个能够持有对某个方法的引用的类,
//delegate类能够拥有一个签名(signature),并且它"只能持有与它的签名相匹配的方法的引用".类似指针,能够指向非静态的指针。
class class4
{
public delegate void MyDelegate(string name); //声明 MyDelegate() 一个委托
public static void MyFun0(string name) //被委托的方法0,它与MyDelegate具有相同的参数和返回值类型
{
Console.WriteLine("MyFun 0: " + name);
}
public static void MyFun1(string name) //被委托的方法1
{
Console.WriteLine("MyFun 1: " + name);
}
}
class key_delegate
{
public key_delegate()
{
Console.WriteLine("key_delegate================");
//创建委托对象
class4.MyDelegate mdf = new class4.MyDelegate(class4.MyFun0);
mdf("123"); //调用到委托 class4.MyFun0
class4.MyFun0("456"); //直接调用函数
mdf = class4.MyFun1; //更改委托
mdf("789");
}
}
//----------------------主进程------------------------------------------------------------------
class Program
{
static void Main(string[] args)
{
key_static k1 = new key_static();
key_new k2 = new key_new();
key_this k3 = new key_this();
key_delegate k4 = new key_delegate();
Console.Read();
}
}
}