hooyantsing's Blog

第27次课程

字数统计: 1.6k阅读时长: 9 min
2019/06/23

源辰74班

第27次课程

06.23.2019

**内容    **

** Java_interface与多态2[蒋斌]**

1.复习及练习一

**Interface **Measurable

1
2
3
4
package com.hooy.mbi;
public interface Measurable {
    public double meausurable(Object obj);
}

**Class **Contair

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.hooy.mbi;
public class Contair {
    private double max;
    private Object maxObj;
    private double min;
    private Object minObj;
    private double avg;
    private double sum;
    private int count;
    private Object[] objs = new Object[2];
    private Measurable m;
    
    public Contair() {}
    public Contair(Measurable m) {
       this.m = m;
    }
    public void add(Object obj) {
       //初始化,防止报错
       if(count==0) {
           Person init = new Person("无",0.0,0.0);
           maxObj = init;
           minObj = init;
       }
       //添加过滤器
       
       if(obj==null) {
           //System.err.println("对象不能为空");
           return;
       }
       if(!(obj instanceof Person)) {
           //System.err.println("非法数据类型");
           return;
       }
       if(((Person)obj).getWeight()<=0 ||  ((Person)obj).getWeight()>=500 ||  ((Person)obj).getHeight()<=0 ||  ((Person)obj).getHeight()>=3.0){
           //System.err.println("体重或身高参数存在异常");
           return;
       }
       
       
       //自定义栈结构
       double result = m.meausurable(obj);
       if(count==0) {
           maxObj = obj;
           minObj = obj;
           max = result;
           min = result;
       }else {
           double maxResult =  m.meausurable(maxObj);
           double minResult =  m.meausurable(minObj);
           if(result>maxResult) {
               maxObj = obj;
               max = result;
           }
           if(result<minResult) {
               minObj = obj;
               min = result;
           }
       }
       //扩容
       if(count>=objs.length) {
           Object[] newObj = new  Object[objs.length*2];
           for(int i=0;i<objs.length;i++) {
               newObj[i] = objs[i];
           }
           objs = newObj;
       }
       objs[count] = obj;
       
       count++;
       
       sum += result;
       avg = sum/count;
    }
    
    
    public double getMax() {
       return max;
    }
    public void setMax(double max) {
       this.max = max;
    }
    public Object getMaxObj() {
       return maxObj;
    }
    public void setMaxObj(Object maxObj) {
       this.maxObj = maxObj;
    }
    public double getMin() {
       return min;
    }
    public void setMin(double min) {
       this.min = min;
    }
    public Object getMinObj() {
       return minObj;
    }
    public void setMinObj(Object minObj) {
       this.minObj = minObj;
    }
    public double getAvg() {
       return avg;
    }
    public void setAvg(double avg) {
       this.avg = avg;
    }
    public double getSum() {
       return sum;
    }
    public void setSum(double sum) {
       this.sum = sum;
    }
    public Measurable getM() {
       return m;
    }
    public void setM(Measurable m) {
       this.m = m;
    }
    public int getCount() {
       return count;
    }
}

**Class **Person

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.hooy.mbi;
public class Person {
    private String name;
    private double weight;
    private double height;
    public Person() {}
    public Person(String name,double  weight,double height) {
       super();
       this.name = name;
       this.weight = weight;
       this.height = height;
    }
    public String getName() {
       return name;
    }
    public void setName(String name) {
       this.name = name;
    }
    public double getWeight() {
       return weight;
    }
    public void setWeight(double weight) {
       this.weight = weight;
    }
    public double getHeight() {
       return height;
    }
    public void setHeight(double height) {
       this.height = height;
    }
    
}

**Class **PersonMeasurable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.hooy.mbi;
public class PersonMeasurable implements  Measurable{
    @Override
    public double meausurable(Object obj) {
       //obj不能为空
       if(obj==null) {
           throw new RuntimeException("对象不为空");
       }
       //obj数据类型 应是Person类型
       if(!(obj instanceof Person)) {
           throw new RuntimeException("非法数据类型");
       }
       //将Object类型强制转换为Person类型
       Person p = (Person)obj;
       double bmi =  p.getWeight()/(p.getHeight()*p.getHeight());
       return bmi;
    }
}

Main Test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.hooy.mbi;
import java.util.Random;
public class Test {
    public static void main(String[] args) {
       Contair cont = new Contair(new  PersonMeasurable());
       Person p1 = new Person("张三",100,-10);
       Person p2 = new Person("李四",80,1.5);
       Person p3 = new Person("王五",-20,1.6);
       Person p4 = new Person("麻六",160,1.6);
       Random p = new Random();
       cont.add(p1);
       cont.add(p);
       cont.add(p3);
       cont.add(p4);
       System.out.println("bmi最大:"+cont.getMax());
       System.out.println("bmi最小:"+cont.getMin());
       System.out.println("bmi平均:"+cont.getAvg());
       System.out.println("bmi总计:"+cont.getSum());
       System.out.println("最肥的是:"+((Person)cont.getMaxObj()).getName());
       System.out.println("最瘦的是:"+((Person)cont.getMinObj()).getName());
    }
}

2.复习及练习二

Interface Info

1
2
3
4
package com.hooy.pointer;
public interface Info {
    public String getInfo();
}

Abstract Printer

1
2
3
4
package com.hooy.pointer;
public abstract class Printer {
    public abstract void pointer(Info inf);
}

**Class **BlackPrinter

1
2
3
4
5
6
7
package com.hooy.pointer;
public  class BlackPrinter extends Printer {
    @Override
    public void pointer(Info inf) {
       System.out.println("黑白打印"+inf.getInfo());
    }
}

**Class **ColorPrinter

1
2
3
4
5
6
7
8
package com.hooy.pointer;
public class ColorPrinter extends Printer{
    @Override
    public void pointer(Info inf) {
       System.out.println("彩色打印"+inf.getInfo());
       
    }
}

**Class **Company

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.hooy.pointer;
public class Company implements Info{
    private String name;
    private String date;
    private Printer p;
    private int count;
    public Employee[] emps = new  Employee[2];
    
    public Company(String name, String date)  {
       super();
       this.name = name;
       this.date = date;
    }
    //不同价格打印机
    public void buy(int money) {
       if(money<1000) {
           p = new BlackPrinter();
       }else {
           p = new ColorPrinter();
       }
    }
    //将员工添加入编制
    public void add(Employee emp) {
       if(count>=emps.length) {
           Employee[] newEmps = new  Employee[emps.length*2];
           for(int i=0;i<emps.length;i++) {
               newEmps[i] = emps[i];
           }
           emps = newEmps;
       }
       emps[count] = emp;
       count++;
    }
    //公司使用打印机
    public void printComp() {
       p.pointer(this);
    }
    //员工使用打印机
    public void printEmps() {
       System.out.println("本公司员工:");
       for(int i=0;i<getCount();i++) {
           p.pointer(emps[i]);
       }
    }
    
    public String getName() {
       return name;
    }
    public void setName(String name) {
       this.name = name;
    }
    public String getDate() {
       return date;
    }
    public void setDate(String date) {
       this.date = date;
    }
    @Override
    public String getInfo() {
       return this.getName()+"创建于:"+this.getDate();       
    }
    public int getCount() {
       return count;
    }
}

**Class **Employee

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.hooy.pointer;
public class Employee implements Info{
    private String name;
    private int age;
    public Employee() {
       super();
    }
    public Employee(String name,int age) {
       super();
       this.name = name;
       this.age = age;
    }
    public String getName() {
       return name;
    }
    public void setName(String name) {
       this.name = name;
    }
    public int getAge() {
       return age;
    }
    public void setAge(int age) {
       this.age = age;
    }
    @Override
    public String getInfo() {
       return this.getName()+"年龄:"+this.getAge();
    }
}

Main Test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.hooy.pointer;
public class Test {
    public static void main(String[] args) {
       //创建公司
       Company com = new Company("源辰","2010-10-10");
       //公司购买打印机
       com.buy(1000);
       //公司使用打印机
       com.printComp();
       //创建员工
       Employee emps1 = new Employee("张三",20);
       Employee emps2 = new Employee("张三",20);
       Employee emps3 = new Employee("张三",20);
       Employee emps4 = new Employee("张三",20);
       //将员工添加入公司编制
       com.add(emps1);
       com.add(emps2);
       com.add(emps3);
       com.add(emps4);
       //员工使用打印机
       com.printEmps();
    }
}

3.复习及练习三

**Interface **Judage

1
2
3
4
package com.hooy.teacher;
public interface Judage {
    public void judage();
}

Class MathTeacher

1
2
3
4
5
6
7
package com.hooy.teacher;
public class MathJudage implements Judage{
    @Override
    public void judage() {
       System.out.println("请问1+1=?");
    }
}

Class Teacher

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.hooy.teacher;
public class Teacher {
    private String name;
    private String course;
    public Teacher() {
       super();
    }
    public Teacher(String name,String  course) {
       super();
       this.name = name;
       this.course = course;
    }
    
    public void selfIntroduce() {
       System.out.println("我是"+name+",所教科目:"+course);
    }
    public String getName() {
       return name;
    }
    public void setName(String name) {
       if(name==null || "".equals(name)) {
           this.name = "匿名";
       }
       this.name = name;
    }
    public String getCourse() {
       return course;
    }
    public void setCourse(String course) {
       if(course==null || "".equals("")) {
           this.course = "百科";
       }
       this.course = course;
    }
}

**Class **Examiner

1
2
3
4
5
6
7
8
package com.hooy.teacher;
public class Examiner {
    //形参内可以为接口类型
    public void judage(Teacher t,Judage  judage) {
       t.selfIntroduce();
       judage.judage();
    }
}

Main Test

1
2
3
4
5
6
7
8
9
package com.hooy.teacher;
public class Test {
    public static void main(String[] args) {
       Teacher t = new Teacher("张三","数学");
       MathJudage mj = new MathJudage();
       Examiner ex = new Examiner();
       ex.judage(t, mj);
    }
}
CATALOG