首页 > 编程语言 > 详细

[20-05-04][Thinking in Java 1]Java Inheritance 1 - Inheritance Syntax 1

时间:2020-05-04 12:34:42      阅读:44      评论:0      收藏:0      [点我收藏+]
 1 package test_1_1;
 2 
 3 public class Cleanser {
 4 
 5     /**
 6      * 继承:基类方法的使用,添加方法,覆盖方法
 7      */
 8     
 9     private String s = "Cleanser";
10 
11     public void append(String a) {
12 
13         s += a;
14     }
15 
16     public void dilute() {
17 
18         append(" dilute()");
19     }
20 
21     public void apply() {
22 
23         append(" apply()");
24     }
25 
26     public void scrub() {
27 
28         append(" scrub()");
29     }
30     
31     public String toString() {
32         
33         return s;
34     }
35     
36     public static void main(String[] args) {
37         
38         Cleanser x = new Cleanser();
39         x.dilute();
40         x.apply();
41         x.scrub();
42         System.out.println(x);
43     }
44 }

 

 1 package test_1_1;
 2 
 3 public class Detergent extends Cleanser{
 4 
 5     public void scrub() {
 6         
 7         append(" Detergent.scrub()");
 8         super.scrub();
 9     }
10     
11     public void foam() {
12         
13         append(" foam()");
14     }
15     
16     public static void main(String[] args) {
17 
18         Detergent x = new Detergent();
19         x.dilute();
20         x.apply();
21         x.scrub();
22         x.foam();
23         System.out.println(x);
24     }
25 
26 }

 

 1 package test_1_1;
 2 
 3 public class AnionicDetergent extends Detergent {
 4 
 5     public void scrub() {
 6         
 7         append(" AD scrub()");
 8         super.scrub();
 9     }
10     
11     public void sterilize() {
12         
13         append(" sterilize()");
14     }
15     
16     public static void main(String[] args) {
17 
18         AnionicDetergent ad = new AnionicDetergent();
19         ad.dilute();
20         ad.apply();
21         ad.scrub();
22         ad.foam();
23         ad.sterilize();
24         System.out.println(ad);
25         System.out.println("---");
26         Detergent.main(args);
27         System.out.println("---");
28         Cleanser.main(args);
29     }
30 
31 }

 

结果如下:

Cleanser dilute() apply() AD scrub() Detergent.scrub() scrub() foam() sterilize()
---
Cleanser dilute() apply() Detergent.scrub() scrub() foam()
---
Cleanser dilute() apply() scrub()

[20-05-04][Thinking in Java 1]Java Inheritance 1 - Inheritance Syntax 1

原文:https://www.cnblogs.com/mirai3usi9/p/12825776.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!