Think in Java (iv): multiplexing classes

Source: Internet
Author: User

1. Each non-basic type of object has a ToString method, and the method is called when the compiler needs a string and you have only one object.
public class Sprinklersystem {private string Valve;public string toString () {value = "Test"; return "valve =" + valve;} public static void Main (string[] args) {Sprinklersystem sprinklers = new Sprinklersystem (); System.out.println (sprinklers);}} /* Output:value = "Test"; *///:~

2. Java automatically inserts a call to the parent class constructor in the constructor of the subclass, that is, the constructor of the parent class is called first, and if the parent class does not have a parameterless constructor, explicitly call the parent class with the parameter constructor by super (Param)

Class Art {art () {print ("Art constructor");}} Class 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 ();}} /* Output:art constructordrawing Constructorcartoon constructor*///:~

3. It is not possible to assume that a variable is final and that it cannot be changed, and final means that the reference cannot be considered to be another new object.
public class Finalarguments {void with (final Gizmo g) {//! g = new Gizmo ();//illegal--G is final}}

4. Inheritance and initialization:
When accessing the Beetle.main () method, the loader starts and finds the compiled code for the Beetle class, and during loading it the compiler notices that it has a parent class.
The parent class starts to load, and then static initialization in the parent class is executed ...
Class Insect {private int i = 9;protected int j;insect () {print ("i =" + i + ", j =" + j); j = 39;} private static int x1 = Printinit ("Static insect.x1 initialized"); static int printinit (String s) {print (s); return 47;}} public class Beetle extends insect {private int k = Printinit ("beetle.k initialized");p ublic Beetle () {print ("k =" + k);p Rint ("j =" + j);} private static int x2 = Printinit ("Static beetle.x2 initialized");p ublic static void Main (string[] args) {print ("Beetle Co Nstructor "); Beetle B = new Beetle ();}} /* output:static insect.x1 initializedstatic beetle.x2 initializedbeetle Constructori = 9, j = 0Beetle.k INITIALIZEDK = 47 j = 39*///:~


Think in Java (iv): Reuse class

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.