Java模板设计模式_抽象类

 1 import java.util.Scanner;
 2 class Test {
 3 
 4 
 5     public static void main(String[] args) {
 6         coffee per = new coffee();
 7         per.shaoshui();
 8         per.receipt();
 9         per.custom();
10         per.jiaoban();
11         per.dicha();
12     }
13 }
14 
15     abstract  class beverage{
16        private  void shaoshui(){
17             System.out.println("烧开水");
18     }
19         abstract  void receipt();
20       private  void jiaoban(){
21             System.out.println("搅拌饮料");
22         }
23 
24         void dicha(){
25             System.out.println("给大佬递饮料");
26         }
27     }
28     class coffee extends beverage{
29           void shaoshui(){
30             System.out.println("烧开水");
31 
32         }
33          void receipt() {
34              System.out.println("加一包秘制咖啡粉");
35          }
36 
37          public static boolean custom(){
38 
39             System.out.println("需要加牛奶吗?");
40             Scanner scanner=new Scanner(System.in);
41             String str=scanner.nextLine();
42             if(str.equalsIgnoreCase("y")){
43                 System.out.println("加一包牛奶");
44                 return true;
45             }
46             else if (str.equalsIgnoreCase("n")){
47                 System.out.println("客户不需要加牛奶");
48                 return false;
49             }
50             else{
51                 System.out.println("非法输入");
52                 return false;
53             }
54 
55         }
56         void jiaoban(){
57             System.out.println("搅拌饮料");
58         }
59         void dicha(){
60             System.out.println("给大佬递咖啡");
61         }
62     }

运行结果:

原文地址:https://www.cnblogs.com/hetaoyuan/p/10566443.html