下面的示例声明了一个抽象的 Employee 类。我们还创建了一个名为 Manager 的派生类,它提供了定义在 Employee 类中的抽象方法 show() 的实现:
using System;
public abstract class Employee
{
// abstract show method
public abstract void show();
}
// Manager class extends Employee
public class Manager: Employee
{
string name;
public Manager(string name)
{
this.name = name;
}
//override the show method
public override void show()
{
Console.WriteLine("Name : " name);
}
}
public class CreateManager
{
public static void Main(string[] args)
{
// Create instance of Manager and assign it to an Employee reference
Employee temp = new Manager("John Chapman");
// Call show method. This will call the show method of the Manager class
temp.show();
}
}
这段代码调用了由 Manager 类提供的 show() 实现,并且在屏幕上打印出雇员的名字。
接口
接口是一种“主干类”,包含方法签名但是没有方法的实现。在这个方面,接口与抽象类一样,只包含抽象方法。C# 接口非常类似于 Java 接口,工作原理基本一样。
接口的所有成员都定义为公共成员,并且接口不能包含常量、字段(私有数据成员)、构造函数、析构函数或任何类型的静态成员。如果为接口的成员指定任何修饰符,编译器将会产生错误。
为了实现接口,我们可以从接口派生类。这样的派生类必须为所有接口的方法提供实现,除非派生类声明为抽象的。
接口的声明与 Java 完全一样。在接口定义中,通过单独使用 get 和 set 关键字,属性仅指示它的类型,以及它是只读的、只写的还是可读写的。下面的接口声明了一个只读属性:
public interface IMethodInterface
{
// method signatures
void MethodA();
int MethodB(float parameter1, bool parameter2);
// properties
int ReadOnlyProperty
{
get;
}
}
用一个冒号来代替 Java 的实现关键字,类就可以继承此接口。实现类必须提供所有方法的定义以及任何必需的属性访问器:
public class InterfaceImplementation : IMethodInterface
{
// fields
private int count = 0;
private int ID;
// implement methods defined in interface
public void MethodA()
{
...
}
public int MethodB(float parameter1, bool parameter2)
{
...
return integerVariable;
}
public int ReadOnlyProperty
{
get
{
return count;
}
}
// add extra methods if required
}
实现多个接口
通过使用下面的语法,一个类可以实现多个接口:
public class MyClass : interfacename1, interfacename2, interfacename3
如果一个类实现多个接口,则成员的名称会存在二义性,通过使用属性或方法名的完全限定符可以解决这个问题。换句话说,通过使用方法的完全限定名来指示它属于哪个接口(例如属于 IMethodInterface.MethodA),派生类可以解决这种冲突。
运算符重载
与 C 一样,C# 允许我们重载运算符,以供在我们自己的类中使用。这可能使得用户定义的数据类型看起来很自然,并且可以在逻辑上作为基本数据类型使用。例如,我们可以创建一个新的名为 Complex 的数据类型来表示一个复杂的数字,并且提供一些方法,以使用标准的算术运算符对这样的数字进行算术运算,例如使用 运算符来使两个复杂的数字相加。
为了重载一个运算符,我们编写了一个函数,它将需要重载的运算符的符号放在名称 operator 的后面。例如,我们可以这样来重载 运算符:
public static complex operator (complex lhs, complex rhs)
所有的运算符重载都是类的静态方法。同时也需要注意,如果您重载等于运算符 (==),您还必须重载不等于运算符 (!=)。
可以重载的运算符的完整列表如下:
•一元运算符: , -, !, ~, , --, true, false
•二元运算符: , -, *, /, %, &, |, ^, <<, >>, ==, !=, >, <, >=, <=
下一个示例创建了一个 Complex 类,该类重载 和 - 运算符:
using System;
public class complex
{
private float real;
private float img;
public complex(float p, float q)
{
real = p;
img = q;
}
public complex()
{
real = 0;
img = 0;
}
public void Print()
{
Console.WriteLine("{0} {1}i", real, img);
}
// Overloading ' ' operator
public static complex operator (complex lhs, complex rhs)
{
complex sum = new complex();
sum.real = lhs.real rhs.real;
sum.img = lhs.img rhs.img;
return (sum);
}
// Overloading '-' operator
public static complex operator-(complex lhs, complex rhs)
{
complex result = new complex();
result.real = lhs.real - rhs.real;
result.img = lhs.img - rhs.img;
return (result);
}
}
此类允许我们使用代码来创建和操作两个复杂的数字,如下所示:
using System;
public class ComplexClass
{
public static void Main(string[] args)
{
// Set up complex numbers
complex A = new complex(10.5f,12.5f);
complex B = new complex(8.0f,4.5f);
complex C;
// Print object A and B
Console.Write("Complex Number A: ");
A.Print();
Console.Write("Complex Number B: ");
B.Print();
// Add A and B, print result
C = A B;
Console.Write("\nA B = ");
C.Print();
// Subtract A and B, print result
C = A - B;
Console.Write("A - B = ");
C.Print();
}
}
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!



