some fragment of thinking in java part4

1. Every non-primitive methods has a toString() method

"source = " +source; //an object of WaterSource

the compiler sees you tring to add a String object("source =") to a WaterSource . Because you can only add a "String" to another String, it says, "I'll turn source into a String by calling toString()!" After doing this it can combine the two Strings and pass the resulting String to System.out.println(). Anytime you want to allow this behavior with a class you create, you need only write a toString method.

2. Four ways to initialize the references

①. at the point the objects are defined. This means that they will always be initialized before the constructor is called.

②. in the constructor for that class.

③. right before you actually need to use the object. This is often called lazy initialization.

④. using instance initialization.

class Soap {

  private String s;

  Soap() {

    print("Soap()");

    s = "Constructed";

  }

  pulbic String toString() { return s; }

}

public class Bath {

  private String //Initializing at the point of definition:

    s1 = "Happy",

    s2 = "Happy",

    s3, s4;

  private Soap castille;

  private int i;

  private float toy;

  public Bath() {

    print("Inside Bath()");

    s3 = "Joy";

    toy = 3.14f;

    castille = new Soap();

  }

  // Instance initialization:

  { i = 47; }

  public String toString() {

    if(s4 = null) // Lazy initialization:

      s4 = "Joy";

    return 

      "s1 = " + s1 + "\n" +

      "s2 = " + s2 + "\n" +

      "s3 = " + s3 + "\n" +

      "s4 = " + s4 + "\n" +

      "i = " + i + "\n" +

      "toy = " + toy + "\n" +

      "castille = " + castille;     

  }

  public static void main(String[] args) {

    Bath b = new Bath();

    print(b);

  }

}/* output:

Inside Bath();

Soap()

s1 = Happy

s2 = Happy

s3 = Joy

s4 = Joy

i = 47

toy = 3.14

castille = Constructed 

*/

3. Inheritance syntax

unless you explicitly inherit from some other class, you implictly inherit from Java's standard root class Object

key word: extends

as a general rule make all fields private and all methods public

it's just from the outside, the subobject of the base class is wrapped within the derived-class object. it's essential that base-class subobject be initializied correctly and there is only one way to guarantee this: perform the initialization in the constructor by calling the base-class constructor. Java automatically inserts calls to the base-class to the base-class constructor in the derived-class constructor.

class Art {

  Art() { print("Art constructor"); }

}

classs Drawing extends Art {

  Drawing() { print("Drawing constructor"); }

}

public class Cartoon extends Drawing {

  public Cartoon() { print("Cartoon constructor"); }

  public static void main(String[] args) {

    Cartoon x = new Cartoon();

  }

}/*

Art constructor

Drawing constructor

Cartoon constructor

*/

that the construction happens from the base "outward", so the base class is initialized before the drived-class construction can access it.

if there's no default base-class constructor, or if you want to call a base-class constructor that has arguments, you must explicitly write a call to the base-class constructor using the super key word and the appropriate argument list, In addition, the call to the base-class constructor must be the first thing you do in the derived-class constructor 

posted on 2015-10-26 08:56  terminator-LLH  阅读(167)  评论(0编辑  收藏  举报

导航