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