C#引用类型(1)

来源:互联网 发布:528雾化器绕丝数据 编辑:程序博客网 时间:2024/04/29 09:44

 

classC# 参考)

类是使用关键字 class 声明的,如下面的示例所示:

复制代码

class TestClass {

// Methods, properties, fields, events, delegates

// and nested classes go here. }

备注

C++ 不同,C# 中仅允许单个继承。也就是说,类只能从一个基类继承实现。但是,一个类可以实现一个以上的接口下表给出了类继承和接口实现的一些示例:

继承

示例

class ClassA { }

单个

class DerivedClass: BaseClass { }

无,实现两个接口

class ImplClass: IFace1, IFace2 { }

单一,实现一个接口

class ImplDerivedClass: BaseClass, IFace1 { }

只有嵌套类允许访问级别 protected private

还可以声明有类型参数的泛型类;有关更多信息,请参见泛型类

一个类可包含下列成员的声明:

·   构造函数

·   析构函数

·   常数

·   字段

·   方法

·   属性

·   索引器

·   运算符

·   事件

·   委托

·  

·   接口

·   结构

示例

下面的示例说明如何声明类的字段、构造函数和方法。该例还说明了如何实例化对象及如何打印实例数据。在此例中声明了两个类,一个是 Kid 类,它包含两个私有字段(name age)和两个公共方法。第二个类 MainClass 用来包含 Main

复制代码

// keyword_class.cs

// class example using System;

class Kid {

private int age;

private string name;

 

// Default constructor:

public Kid() { name = "N/A"; }

// Constructor:

public Kid(string name, int age) {

this.name = name; this.age = age;

}

// Printing method:

public void PrintKid() {

Console.WriteLine("{0}, {1} years old.", name, age);

}

}

 

 

class MainClass {

static void Main() {

// Create objects

// Objects must be created using the new operator:

Kid kid1 = new Kid("Craig", 11);

Kid kid2 = new Kid("Sally", 10);

// Create an object using the default constructor:

Kid kid3 = new Kid();

 

// Display results:

Console.Write("Kid #1: "); kid1.PrintKid(); Console.Write("Kid #2: "); kid2.PrintKid(); Console.Write("Kid #3: "); kid3.PrintKid();

}

}

输出

Kid #1: Craig, 11 years old. Kid #2: Sally, 10 years old. Kid #3: N/A, 0 years old.

注释

注意:在上例中,私有字段(name age)只能通过 Kid 类的公共方法访问。例如,不能通过 Main 方法用如下语句打印小孩的姓名:

复制代码

Console.Write(kid1.name); // Error

只有当 Main Kid 的成员时,才能从 Main 访问该类的私有成员。

如果在类的内部声明的类型没有访问修饰符,则该类型默认为 private,因此,如果移除关键字,则此示例中的数据成员仍然会是 private 的。

最后要注意的是,默认情况下,对于使用默认构造函数 (kid3) 创建的对象,age 字段初始化为零。

 

 

委托(C# 参考)

委托类型声明的格式如下:

复制代码

public delegate void TestDelegate(string message);

delegate 关键字用于声明一个引用类型,该引用类型可用于封装命名方法或匿名方法委托类似于 C++ 中的函数指针;但是,委托是类型安全和可靠的。有关委托的应用,请参见委托泛型委托

备注

委托是事件的基础。

通过将委托与命名方法或匿名方法关联,可以实例化委托。有关更多信息,请参见命名方法匿名方法

为了与命名方法一起使用,委托必须用具有可接受签名的方法进行实例化。有关方法签名中允许的方差度的更多信息,请参见委托中的协变和逆变。为了与匿名方法一起使用,委托和与之关联的代码必须一起声明。本节讨论这两种实例化委托的方法。

复制代码

using System;

// Declare delegate -- defines required signature:

 

delegate void SampleDelegate(string message);

 

class MainClass {

// Regular method that matches signature:

static void SampleDelegateMethod(string message) {

Console.WriteLine(message); }

 

static void Main() {

// Instantiate delegate with named method:

 SampleDelegate d1 = SampleDelegateMethod;

 

// Instantiate delegate with anonymous method:

SampleDelegate d2 = delegate(string message) { Console.WriteLine(message); };

 

// Invoke delegate d1:

d1("Hello");

// Invoke delegate d2:

d2(" World");

}

}

C# 语言规范

有关更多信息,请参见 C# 语言规范中的以下各章节:

·   1.11 委托

·   15 委托

 

 

接口(C# 参考)

接口只包含方法委托事件的签名。方法的实现是在实现接口的类中完成的如下面的示例所示:

复制代码

interface ISampleInterface {

void SampleMethod();

}

 

class ImplementationClass : ISampleInterface {

// Explicit interface member implementation:

void ISampleInterface.SampleMethod()

{ // Method implementation. }

 

 static void Main() {

// Declare an interface instance.

ISampleInterface obj = new ImplementationClass();

// Call the member.

obj.SampleMethod();

}

}

备注

接口可以是命名空间或类的成员,并且可以包含下列成员的签名:

·   方法

·   属性

·   索引器

·   事件

一个接口可从一个或多个基接口继承。

当基类型列表包含基类和接口时,基类必须是列表中的第一项。

实现接口的类可以显式实现该接口的成员。显式实现的成员不能通过类实例访问,而只能通过接口实例访问,例如:

有关显式接口实现的更多详细信息和代码示例,请参见显式接口实现(C# 编程指南)

示例

下面的示例演示了接口实现。在此例中,接口 IPoint 包含属性声明,后者负责设置和获取字段的值。Point 类包含属性实现。

复制代码

// keyword_interface_2.cs

// Interface implementation using System;

 

interface IPoint {

 // Property signatures:

int x { get; set; }

int y { get; set; }

}

 

class Point : IPoint {

// Fields:

private int _x;

private int _y;

 

// Constructor:

public Point(int x, int y) {

 _x = x;

_y = y;

}

 

// Property implementation:

public int x {

get { return _x; }

set { _x = value; }

}

 

public int y {

get { return _y; }

 set { _y = value; }

}

}

 

class MainClass {

 

static void PrintPoint(IPoint p) {

Console.WriteLine("x={0}, y={1}", p.x, p.y);

}

 

static void Main() {

 Point p = new Point(2, 3);

Console.Write("My Point: ");

PrintPoint(p);

}

}

输出

My Point: x=2, y=3

C# 语言规范

有关更多信息,请参见 C# 语言规范中的以下各章节:

·   1.9 接口

·   3.4.5 接口成员

·   4.2.4 接口类型

·   10.1.2.2 接口实现

·   11.2 结构接口

·   13 接口

 
原创粉丝点击