Dragon in the sky

whatever happened , go ahead! Happy life of big pig and its dog.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C# Base Class Study

Posted on 2009-09-09 17:14  龙泰  阅读(478)  评论(0编辑  收藏  举报
Base classes

A class declaration may specify a base class by following the class name and type parameters with a colon and the name of the base class. Omitting a base class specification is the same as deriving from type object. In the following example, the base class of Point3D is Point, and the base class of Point is object:

public class Point
{
public int x, y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}
}

public class Point3D: Point
{
public int z;

public Point3D(int x, int y, int z): base(x, y) {
this.z = z;
}
}

A class inherits the members of its base class. Inheritance means that a class implicitly contains all members of its base class, except for the constructors of the base class. A derived class can add new members to those it inherits, but it cannot remove the definition of an inherited member. In the previous example, Point3D inherits the x and y fields from Point, and every Point3D instance contains three fields, x, y, and z.

An implicit conversion exists from a class type to any of its base class types. Therefore, a variable of a class type can reference an instance of that class or an instance of any derived class. For example, given the previous class declarations, a variable of type Point can reference either a Point or a Point3D:

Point a = new Point(10, 20);
Point b = new Point3D(10, 20, 30);