zoukankan      html  css  js  c++  java
  • C# Base Class Study

    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);

  • 相关阅读:
    final关键字
    this and super
    java 内存分析之static
    java 内存分析之this
    java 内存分析之方法返回值二
    java 内存分析之方法返回值及匿名对象一
    java 内存分析之构造方法执行过程
    java 内存分析之堆栈空间
    java jvm概述及工作过程中的内存管理
    java 编译器
  • 原文地址:https://www.cnblogs.com/flyinthesky/p/1563408.html
Copyright © 2011-2022 走看看