站内搜索: 请输入搜索关键词
当前页面: 图书首页 > NET For Java Developers Migrating To C#

NET For Java Developers Migrating To C#

[ directory ] Previous Section Next Section

4.3 Constructors

Constructors are a special breed of methods that are used for creating object instances. In C# and Java, objects can have multiple constructors. In the absence of a constructor, the JVM and CLR runtimes provide a default public no-argument constructor. Constructors can call each other.

In Listing 4.6, the class has three constructors. The least specific constructor in turn calls the next more specific constructor and so on, thereby leaving the actual implementation in one constructor (the most specific one).

Listing 4.6 Multiple Constructors (C#)
using System;
public class Cube {
  int width, height, length;

  public Cube() : this(60,60,60) {}
  public Cube(int width, int length) : this(width,length,60) {}
  public Cube(int width, int length, int height) {
    this.width = width;
    this.length = length;
    this.height = height;
  }
  public override string ToString() {
    return "Volume is "+(this.width*this.height*this.length);
  }
  public static void Main(string[] args) {
    Console.WriteLine(new Cube());
    Console.WriteLine(new Cube(10,20));
    Console.WriteLine(new Cube(10,20,40));
  }
}

A subclass of Cube can refer to its superclass constructors using the base keyword. This is equivalent to Java's super keyword. Therefore, the following class will compile:

using System;
public class DerivedCube : Cube{
  public DerivedCube (int width, int length) : base (width, length){}
}

Now let's remove the default constructor Cube() from Listing 4.6 and remove the DerivedCube(int width, int length) constructor from the preceding code and try compiling. The compiler complains. In the absence of a constructor specified for DerivedCube, the runtime tries to allocate the default public no-args constructor and notices that the superclass does not have that default constructor specified. However, when the Derived Cube(int width, int length) constructor is put back in place with the superclass not having the Cube() constructor, the compiler does not complain. This behavior is similar to that of Java.

C# provides a default no-args public constructor to a class if there is no constructor specified. However, if a constructor is specified for a class, then the runtime does not provide a default constructor. The default constructor provided by the runtime automatically calls the base(), in which case it becomes mandatory for the superclass to have a default no-args public constructor. Constructors can be private. This modifier is typically used in objects implementing the singleton pattern. Making the constructor private allows only one copy of the object (a singleton) to exist:

using System;
public class CubeFactory {
  private static singleton = new CubeFactory();
  private CubeFactory(){}
  public CubeFactory GetSingleton() { return singleton; }
}

Also, subclasses cannot refer to this private base constructor using the base() call.

Constructors can be protected, in which case subclasses can access them using the base keyword as long as the subclasses are in the same assembly as the superclass. Constructors can also be static. Static C# constructors are equivalent to the Java static initializer blocks. Static constructors cannot have any parameters. They are specified as follows:

using System;
public class RubickCube : Cube {
  static RubickCube() {
    Console.WriteLine("Static block");
  }
}

Note that static blocks do not follow the typical inheritance behavior. The static constructor of the subclass does not automatically call the static constructor of the superclass. This behavior is similar to that of Java's static initializers.

    [ directory ] Previous Section Next Section