对象类是对一种事物的抽象,对象是类的实例。构成类的方法和变量成为类的成员。类的定义是以关键字 class 开始,后跟类的名称。类的主体,包含在一对花括号内。类的默认访问标识符是 internal,成员的默认访问标识符是 private。
我们可以使用 static 关键字把类成员定义为静态的。当我们声明一个类成员为静态时,意味着无论有多少个类的对象被创建,只会有一个该静态成员的副本。
关键字 static 意味着类中只有一个该成员的实例。静态变量用于定义常量,因为它们的值可以通过直接调用类而不需要创建类的实例来获取。静态变量可在成员函数或类的定义外部进行初始化。你也可以在类的定义内部初始化静态变量。
下面的实例演示了静态变量的用法:
- using System;
- namespace StaticVarApplication
- {
- class StaticVar
- {
- public static int num;
- public void count()
- {
- num++;
- }
- public int getNum()
- {
- return num;
- }
- }
- class StaticTester
- {
- static void Main(string[] args)
- {
- StaticVar s1 = new StaticVar();
- StaticVar s2 = new StaticVar();
- s1.count();
- s1.count();
- s1.count();
- s2.count();
- s2.count();
- s2.count();
- Console.WriteLine("s1 的变量 num: {0}", s1.getNum());
- Console.WriteLine("s2 的变量 num: {0}", s2.getNum());
- Console.ReadKey();
- }
- }
- }
当上面的代码被编译和执行时,它会产生下列结果:
- s1 的变量 num: 6
- s2 的变量 num: 6
你也可以把一个成员函数声明为 static。这样的函数只能访问静态变量。静态函数在对象被创建之前就已经存在。下面的实例演示了静态函数的用法:
- using System;
- namespace StaticVarApplication
- {
- class StaticVar
- {
- public static int num;
- public void count()
- {
- num++;
- }
- public static int getNum()
- {
- return num;
- }
- }
- class StaticTester
- {
- static void Main(string[] args)
- {
- StaticVar s = new StaticVar();
- s.count();
- s.count();
- s.count();
- Console.WriteLine("变量 num: {0}", StaticVar.getNum());
- Console.ReadKey();
- }
- }
- }
当上面的代码被编译和执行时,它会产生下列结果:
当创建一个类时,程序员不需要完全重新编写新的数据成员和成员函数,只需要设计一个新的类,继承了已有的类的成员即可。这个已有的类被称为的基类,这个新的类被称为派生类。
C# 中创建派生类的语法如下:
- <acess-specifier> class <base_class>
- {
- ...
- }
- class <derived_class> : <base_class>
- {
- ...
- }
例如:
- class Shape
- {
- public void setWidth(int w)
- {
- width = w;
- }
- public void setHeight(int h)
- {
- height = h;
- }
- protected int width;
- protected int height;
- }
-
-
- class Rectangle: Shape
- {
- public int getArea()
- {
- return (width * height);
- }
- }
-
派生类继承了基类的成员变量和成员方法。因此父类对象应在子类对象创建之前被创建。您可以在成员初始化列表中进行父类的初始化。
下面的程序演示了这点:
- using System;
- namespace RectangleApplication
- {
- class Rectangle
- {
-
- protected double length;
- protected double width;
- public Rectangle(double l, double w)
- {
- length = l;
- width = w;
- }
- public double GetArea()
- {
- return length * width;
- }
- public void Display()
- {
- Console.WriteLine("长度: {0}", length);
- Console.WriteLine("宽度: {0}", width);
- Console.WriteLine("面积: {0}", GetArea());
- }
- }
- class Tabletop : Rectangle
- {
- private double cost;
- public Tabletop(double l, double w) : base(l, w)
- { }
- public double GetCost()
- {
- double cost;
- cost = GetArea() * 70;
- return cost;
- }
- public void Display()
- {
- base.Display();
- Console.WriteLine("成本: {0}", GetCost());
- }
- }
- class ExecuteRectangle
- {
- static void Main(string[] args)
- {
- Tabletop t = new Tabletop(4.5, 7.5);
- t.Display();
- Console.ReadLine();
- }
- }
- }
当上面的代码被编译和执行时,它会产生下列结果:
- 长度: 4.5
- 宽度: 7.5
- 面积: 33.75
- 成本: 2362.5
多重继承指的是一个类别可以同时从多于一个父类继承行为与特征的功能。与单一继承相对,单一继承指一个类别只可以继承自一个父类。
C# 不支持多重继承。但是,您可以使用接口来实现多重继承。下面的程序演示了这点:
- using System;
- namespace InheritanceApplication
- {
- class Shape
- {
- public void setWidth(int w)
- {
- width = w;
- }
- public void setHeight(int h)
- {
- height = h;
- }
- protected int width;
- protected int height;
- }
-
-
- public interface PaintCost
- {
- int getCost(int area);
-
- }
-
- class Rectangle : Shape, PaintCost
- {
- public int getArea()
- {
- return (width * height);
- }
- public int getCost(int area)
- {
- return area * 70;
- }
- }
- class RectangleTester
- {
- static void Main(string[] args)
- {
- Rectangle Rect = new Rectangle();
- int area;
- Rect.setWidth(5);
- Rect.setHeight(7);
- area = Rect.getArea();
-
- Console.WriteLine("总面积: {0}", Rect.getArea());
- Console.WriteLine("油漆总成本: ${0}" , Rect.getCost(area));
- Console.ReadKey();
- }
- }
- }
当上面的代码被编译和执行时,它会产生下列结果:
C#接口
接口定义了所有类继承接口时应遵循的语法合同。接口定义了语法合同 "是什么" 部分,派生类定义了语法合同 "怎么做" 部分。
接口着重与实现什么功能,而继承着重于是否在逻辑上keyi
接口定义了属性、方法和事件,这些都是接口的成员。接口只包含了成员的声明。成员的定义是派生类的责任。接口提供了派生类应遵循的标准结构。
接口使得实现接口的类或结构在形式上保持一致。
抽象类在某种程度上与接口类似,但是,它们大多只是用在当只有少数方法由基类声明由派生类实现时。
接口的定义
接口使用 interface 关键字声明,它与类的声明类似。接口声明默认是 public 的。下面是一个接口声明的实例:
interface IMyInterface
{
void MethodToImplement();
}
以上代码定义了接口 IMyInterface。通常接口命令以 I 字母开头,这个接口只有一个方法 MethodToImplement(),没有参数和返回值,当然我们可以安装需求设置参数和返回值。
值得注意的是,该方法并没有具体的实现。
接口的实现
接口使用 interface 关键字声明,它与类的声明类似。接口声明默认是 public 的。下面是一个接口声明的实例:
- using System;
-
- interface IMyInterface
- {
-
- void MethodToImplement();
- }
-
- class InterfaceImplementer : IMyInterface
- {
- static void Main()
- {
- InterfaceImplementer iImp = new InterfaceImplementer();
- iImp.MethodToImplement();
- }
-
- public void MethodToImplement()
- {
- Console.WriteLine("MethodToImplement() called.");
- }
- }
InterfaceImplementer 类实现了 IMyInterface 接口,接口的实现与类的继承语法格式类似:
- class InterfaceImplementer : IMyInterface
继承接口后,我们需要实现接口的方法 MethodToImplement() , 方法名必须与接口定义的方法名一致。
接口的继承
以下实例定义了两个接口 IMyInterface 和 IParentInterface。
如果一个接口继承其他接口,那么实现类或结构就需要实现所有接口的成员。
以下实例 IMyInterface 继承了 IParentInterface 接口,因此接口实现类必须实现 MethodToImplement() 和 ParentInterfaceMethod() 方法:
- using System;
-
- interface IParentInterface
- {
- void ParentInterfaceMethod();
- }
-
- interface IMyInterface : IParentInterface
- {
- void MethodToImplement();
- }
-
- class InterfaceImplementer : IMyInterface
- {
- static void Main()
- {
- InterfaceImplementer iImp = new InterfaceImplementer();
- iImp.MethodToImplement();
- iImp.ParentInterfaceMethod();
- }
-
- public void MethodToImplement()
- {
- Console.WriteLine("MethodToImplement() called.");
- }
-
- public void ParentInterfaceMethod()
- {
- Console.WriteLine("ParentInterfaceMethod() called.");
- }
- }
实例输出结果为:
- MethodToImplement() called.
- ParentInterfaceMethod() called.