动手动脑四

1.继承条件下的构造方法的调用

 1 package 动手动脑1;
 2 
 3 public class Child extends Parent{
 4     public Child() {
 5     
 6         System.out.println("Child Created");
 7 }
 8 
 9     }
10 package 动手动脑1;
11 
12 public class Parent extends Grandparent{
13     public Parent() {
14     
15     
16          
17 
18                 //super("Hello.Grandparent.");
19 
20                 System.out.println("Parent Created");
21         
22            // super("Hello.Grandparent.");
23 
24     }
25 
26     
27 }
28 package 动手动脑1;
29 public class Grandparent {
30     public Grandparent()
31      {
32 
33             System.out.println("GrandParent Created.");
34     
35 }
36 
37 
38     public Grandparent(String string) 
39     {
40 
41             System.out.println("GrandParent Created.String:" + string);
42     
43  }
44 
45 }
46 package 动手动脑1;
47 
48 public class TestInherits {
49 
50 
51     public static void main(String[] args) {
52         // TODO 自动生成的方法存根
53         Child c = new Child();
54         
55     }
56 
57 }

当父类构造函数在子类构造函数之前调用之前,在构造函数前加super先调用父类的构造函数,当父类在子类之后无法调用构造函数

2神奇的加号

 1 package 动手动脑1;
 2 
 3 public class Fruit {
 4 public  String  toString()
 5     {
 6         return "Fruit toString.";
 7     }
 8     public static void main(String[] args) {
 9         // TODO 自动生成的方法存根
10         Fruit f=new Fruit();
11         System.out.println("f="+f);
12         System.out.println("f="+f.toString());
13     }
14 
15 }

一个字符串和一个对象“相加”相当于调用这个方法

3探索技术的奥秘

 1 package 动手动脑1;
 2 
 3 public class ExplorationJDKSource {
 4     /**
 5      * @param args
 6      */
 7     
 8     public static void main(String[] args) {
 9         System.out.println(new A());
10     }
11 
12 
13 }
14 class A{}

原文地址:https://www.cnblogs.com/dinghaisheng/p/9901087.html