Chapter 7 - multiplexing class - inheritance syntax -1

Exercise 2: Detergent inherited from generating a new class. Covering scrub () and add a called sterilize () of the new approach.

A: I answer:

 1 package com.learnJava.test;
 2 
 3 /**
 4  * @Author zhuchangli
 5  * @Date 2019/9/14
 6  **/
 7 class Cleanser{
 8     private String s = "Cleanser";
 9     public void append(String a) { s += a; }
10     public void dilute(){ append("dilute()"); }
11     public void applay(){append("applay()");}
12     public void scrub(){append("scrub()");}
13     public String toString(){return s;}
14 
15     public static void main(String [] args){
16         Cleanser x = new Cleanser();
17         x.dilute();
18         x.applay();
19         x.scrub();
20         System.out.println(x);
21     }
22 }
23 //洗涤剂
24 public class Detergent extends Cleanser{
25     public void scrub(){
26         append("Detergent.scrub()");
27         super.scrub();
28     }
29 
30     // foam 泡沫
31     public void foam(){ append("foam()");}
32 
33     public static void main(String [] args){
34         Detergent x = new Detergent();
35         x.dilute();
36         x.applay();
37         x.scrub();
38         x.foam();
39         System.out.println(x);
40         System.out.println("Testing base class");
41         Cleanser.main(args);
42     }
43 }
 1 package com.learnJava.test;
 2 
 3 /**
 4  * @Author zhuchangli
 5  * @Date 2019/9/14
 6  **/
 7 public class Sterilize extends Detergent{
 8     public void scrub(){
 9         append(" Sterilize.scrub()");
10     }
11 
12     public void sterilize(){
13         append("sterilize()");
14     }
15 
16     public static void main(String [] args){
17         Sterilize s = new Sterilize();
18         s.scrub();
19         System.out.println(s);
20     }
21 }

Two: Answer:

 1 package com.learnJava.test;
 2 
 3 /**
 4  * @Author zhuchangli
 5  * @Date 2019/9/14
 6  **/
 7 
 8 // 消毒
 9 public class Sterilizer extends Detergent{
10     public void scrub(){
11         append("Sterilizer scrub()");
12     }
13 
14     public void sterilizer(){
15         append(" Detergent sterilizer()");
16     }
17 
18     public static void main(String [] args){
19         Sterilize s = new Sterilize();
20         s.dilute();
21         s.applay();
22         s.scrub();
23         s.foam();
24         s.sterilize();
25         System.out.println(s);
26         System.out.println("Testing base class: ");
27         Detergent.main(args);
28     }
29 }

All of the same class inheritance.

Three: summary

Technology 1, in each class are provided a main () method can test each class unit becomes easy, and after the completion of the test do not delete unit main () method, can be reserved for the next test.

2, in order to inherit, the general rule is that all the data members are designated as private, all the way to specify as public. As used in this section Cleanser does not add any access qualifier, the default access permissions for the package access within package only allows members to visit if inherit Cleanser from other packages, you can only access public members.

3, using super call the base class method in the scrub, and can not call scrub () method directly, because the generated recursively.

Guess you like

Origin www.cnblogs.com/changlili/p/11519512.html