Some fragment of thinking in Java part4

Source: Internet
Author: User

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 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 both Strings and pass the resulting String to System.out.println (). Anytime 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 hte point the objects is defined. This means that they would always be initialized before the constructor is called.

②. The constructor for that class.

③. 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 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 are only one-to-guarantee this:perform th E 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 constructi On can access it.

It there ' s no default Base-class constructor, or if you want to call a Base-class constructor that have arguments, you must Explicitly write a call to the Base-class constructor using the Super key word and the appropriate argument list, in Addi tion, the call to the Base-class constructor must is the first thing you does in the Derived-class constructor

Some fragment of thinking in Java part4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.