hooyantsing's Blog

第26次课程

字数统计: 2.4k阅读时长: 13 min
2019/06/21

源辰74班

第26次课程

06.21.2019

**内容    **

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

1.蜘蛛网扑克牌[排序功能]

知识点:

字符串内字符截取

将字符串内的数字转化为 数字

1
2
3
4
5
String name = "1-2";
//字符串内字符截取
//将字符串内的数字转化为 数字
int value = Integer.parseInt(name.substring(0,1);
获取值:value = 1;

自制排序功能(最直观):

190620Z.zip

Class Main

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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import java.awt.*;
import java.util.Random;

import javax.sql.CommonDataSource;
import javax.swing.*;

public class Main extends JFrame{
    private Container contentPane;
    private Card[] cards = new Card[56];
    //创建三个玩家
    private CardsContair[] playContair = new CardsContair[3];
    //预留地主牌
    private CardsContair landLord = new CardsContair();
    
    public void start() {
        init();
        cardInit();
        for(int i=0;i<playContair.length;i++) {
            Common.sort(playContair[i]);
            }
        for(int j=0;j<playContair[0].c.length;j++) {
            Common.move(playContair[0].c[j], playContair[0].c[j].getLocation(), new Point(50,60+j*20));
            contentPane.setComponentZOrder(playContair[0].c[j], 0);    
        }
        for(int j=0;j<playContair[1].c.length;j++) {
            Common.move(playContair[1].c[j], playContair[1].c[j].getLocation(), new Point(180+j*25,450));
            contentPane.setComponentZOrder(playContair[1].c[j], 0);    
        }
        for(int j=0;j<playContair[2].c.length;j++) {
            Common.move(playContair[2].c[j], playContair[2].c[j].getLocation(), new Point(700,60+j*20));
            contentPane.setComponentZOrder(playContair[2].c[j], 0);    
        }
            
    }
    
    public void init(){
        //设置窗口大小
        this.setSize(830, 630);
        //关闭窗口即终止程序
        this.setDefaultCloseOperation(3);
        //窗口居中
        this.setLocationRelativeTo(null);
        //空布局
        this.setLayout(null);
        //初始化容器
        contentPane = this.getContentPane();
        //设置容器背景颜色(R,G,B)
        contentPane.setBackground(new Color(0,112,26));    
        //窗口显示(非隐藏)
        this.setVisible(true);
    }
    
    public void cardInit() {
        int count = 1;
        for(int i=1;i<=5;i++) {
            for(int j=1;j<=13;j++) {
                if(i==5&&j>=3) {
                    break;
                }else {
                    //设置权值(power)
                    if(i==5 && j==2) {
                        cards[count] = new Card(i+"-"+j,false,17);
                        //控件的位置
                        cards[count].setLocation(300+(i*20), 50);
                        //将cards[count]控件添加到contentPane容器上
                        contentPane.add(cards[count]);
                        count++;
                        continue;
                    }
                    if(i==5 && j==1) {
                        cards[count] = new Card(i+"-"+j,false,16);
                        //控件的位置
                        cards[count].setLocation(300+(i*20), 50);
                        //将cards[count]控件添加到contentPane容器上
                        contentPane.add(cards[count]);
                        count++;
                        continue;
                    }
                    if(j==2) {
                        cards[count] = new Card(i+"-"+j,false,15);
                        //控件的位置
                        cards[count].setLocation(300+(i*20), 50);
                        //将cards[count]控件添加到contentPane容器上
                        contentPane.add(cards[count]);
                        count++;
                        continue;
                    }
                    if(j==1) {
                        cards[count] = new Card(i+"-"+j,false,14);
                        //控件的位置
                        cards[count].setLocation(300+(i*20), 50);
                        //将cards[count]控件添加到contentPane容器上
                        contentPane.add(cards[count]);
                        count++;
                        continue;
                    }
                    cards[count] = new Card(i+"-"+j,false,j);
                    //控件的位置
                    cards[count].setLocation(300+(i*20), 50);
                    //将cards[count]控件添加到contentPane容器上
                    contentPane.add(cards[count]);
                    count++;
                }
            }
        }
        //洗牌
        Random r = new Random();
        for(int i=0;i<1000;i++) {
            int a = r.nextInt(54)+1;
            int b = r.nextInt(54)+1;
            Card temp = cards[a];
            cards[a] = cards[b];
            cards[b] = temp;
        }
        for(int i=0;i<3;i++) {
            playContair[i] = new CardsContair();
        }
        //发牌
        
        for(int i=1;i<=54;i++) {
            if(i>=52) {
                landLord.add(cards[i]);
                Common.move(cards[i], cards[i].getLocation(), new Point(300+(i-52)*80,15));
                continue;
            }
            switch((i-1)%3) {
                case 0:
                    Common.move(cards[i], cards[i].getLocation(), new Point(50,60+i*7));
                    playContair[0].add(cards[i]);
                    cards[i].turnFront();
                    break;
                case 1:
                    Common.move(cards[i], cards[i].getLocation(), new Point(180+i*7,450));
                    playContair[1].add(cards[i]);
                    cards[i].turnFront();
                    break;
                case 2:
                    Common.move(cards[i], cards[i].getLocation(), new Point(700,60+i*7));
                    playContair[2].add(cards[i]);
                    cards[i].turnFront();
                    break;
            }
            contentPane.setComponentZOrder(cards[i], 0);    
        }    
    }
}

Class Common

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
import java.awt.Point;
public class Common {
    //移动效果方法,用于发牌
    public static void move(Card card,Point  from,Point to) {
       if(to.x != from.x) {
           double k = (1.0) * (to.y -  from.y) / (to.x - from.x);
           double b = to.y - to.x * k;
           int flag = 0;
           if(from.x < to.x) {
               flag = 10;
           }else {
               flag = -10;
           }
           for(int  i=from.x;Math.abs(i-to.x)>20;i+=flag) {
               double y = k * i + b;
               card.setLocation(i,(int)y);
               try {
                  //延迟3s 完成
                  Thread.sleep(3);
               }catch(InterruptedException  e) {
                  e.printStackTrace();
               }
           }
       }
       //位置校准
       card.setLocation(to);
    }
    
    //排序
    public static void sort(CardsContair  playContair) {
       for(int  i=0;i<playContair.getSize()-1;i++) {
           for(int  j=0;j<playContair.getSize()-i-1;j++) {
               if(playContair.c[j].getPower()>playContair.c[j+1].getPower()) {
                  Card temp =  playContair.c[j];
                  playContair.c[j] =  playContair.c[j+1];
                  playContair.c[j+1] =  temp;
               }
           }
       }
    }
}

Class Card

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
import javax.swing.*;
public class Card extends JLabel{
    private String name;
    private boolean up;
    //private int flower;
    //private int number;
    private int power;
    
    public Card(){
       
    }
    public Card(String name,boolean up,int  power) {
       this.name = name;
       this.up = up;
       this.power = power;
       //设置牌的高度和宽度
       this.setSize(71,96);
       if(up) {
           //正面
           turnFront();
       }else {
           //反面
           turnRear();
       }
    }
    //正面贴图
    public void turnFront() {
       this.setIcon(new  ImageIcon("images/"+name+".gif"));
    }
    //反面贴图
    public void turnRear() {
       this.setIcon(new  ImageIcon("images/rear.gif"));
    }
     
    
    public int getPower() {
       return power;
    }
}

Class CardsContair

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
//单个玩家
public class CardsContair {
    public Card[] c;
    private int index;
    
    public CardsContair(){
       c = new Card[17];
    }
    
    public void add(Card card) {
       if(index>=c.length) {
           //数组扩容
           Card[] newC = new  Card[c.length*2];
           for(int i=0;i<c.length;i++) {
               newC[i] = c[i];
           }
           c = newC;
       }
       c[index] = card;
       index++;
    }
    
    public int getSize() {
       return index;
    }
    public Card[] getC() {
       return c;
    }
}

Main Test

1
2
3
4
5
public class Test {
    public static void main(String[] args) {
       new Main().start();;
    }
}

2.多态

一个类的多种形态

父类的引用指向子类:父类 变量名 = new 子类;

f59f6b53b16ffe34f7bce92b9d351222.png

实践例题1:

Class Shape

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.hooy.www;
public class Shape {
    //形状
    public void draw() {
       System.out.println("基本的形状");
    }
}
class Circls extends Shape{
    //圆形
    public void draw() {
       System.out.println("圆形");
    }
}
class Square extends Shape{
    //方形
       public void draw() {
           System.out.println("方形");
       }
}

Main Shape_Test

1
2
3
4
5
6
7
package com.hooy.www;
public class Shape_Test {
    public static void main(String[] args) {
       Shape s = new Circls();
       s.draw();
    }
}

实践例题2:

**Abstract **Class Plane

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
package com.hooy.www;
public abstract class Plane {
    public abstract void fly();      //起飞
    public abstract void land();
    public void planeFlyandLand(Plane p) {
       p.fly();
       p.land();
    }
}
class Copter extends Plane{
    //重写
    public void fly() {
       System.out.println("垂直起飞");
    }
    public void land() {
       System.out.println("垂直降落");
    }
}
class Jet extends Plane{
    //重写
    public void fly() {
       System.out.println("喷气起飞");
    }
    public void land() {
       System.out.println("喷气降落");
    }
}

Main Plane_Test

1
2
3
4
5
6
7
package com.hooy.www;
public class Plane_Test {
    public static void main(String[] args) {
       Plane p = new Jet();
       p.planeFlyandLand(p);
    }
}

3.接口(interface)

比抽象类更抽象

不能实例化(new)

接口里:

属性不加final也是常量,不加static也是静态。

方法不加abstract也是抽象方法。方法是抽象的,没有方法体。

实践例题1:

interface Jump

1
2
3
4
5
6
7
8
package com.hooy.interface_Test;
//接口:制定的一种标准
public interface Jump {
    //接口内所有属性均为:final(常量)  static(静态)
    final static int a = 10;
    //接口内所有方法均为:abstract(抽象)
    public void superJump();
}

Main Person

1
2
3
4
5
6
7
8
9
package com.hooy.interface_Test;
public class Person implements Jump{
    //重写:具体实现 接口Jump中的superJump方法
    @Override
    public void superJump() {
       System.out.println("跳的很高");
       
    }
}

接口可以多实现

1
2
3
4
//例如:
public class Gaode implements TalkLikeGirl,Path{
                类体
}

接口可以多继承

实践例题2:

Talk

**Interface **baseTalk

1
2
3
4
package com.hooy.Talk;
public interface baseTalk {
    public void talk(String content);
}

**Interface ** TalkLikeGirl

1
2
3
4
package com.hooy.Talk;
public interface TalkLikeGirl extends  baseTalk{
    public void changeTalk(String content);
}

Interface Path

1
2
3
4
package com.hooy.Talk;
public interface Path {
    public void genPah(String content);
}

Class Gaode

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.Talk;
public class Gaode implements  TalkLikeGirl,Path{
    @Override
    public void talk(String content) {
       System.out.println("高德地图欢迎您");
       changeTalk(content);
       genPah(content);
    }
    @Override
    public void genPah(String content) {
       System.out.println("林志玲音色:林志玲为您导航!");
       System.out.println(content);
    }
    @Override
    public void changeTalk(String content) {
       //空格为分隔符,String[0] = "衡阳",String[1] = "北京"
       String start = content.split("  ")[0];
       String end = content.split(" ")[1];
       System.out.println(start+"--长沙--"+end);
    }
    
}

Main Test

1
2
3
4
5
6
7
8
package com.hooy.Talk;
public class Test {
    public static void main(String[] args) {
       Gaode gd = new Gaode();
       //content = "衡阳 北京"
       gd.talk("衡阳 北京");
    }
}

实践例题3:

项目中使用Class(类)来实现,下节课中将用Interface(接口)来实现。

Teacher

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
36
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 EnglishTeacher

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
package com.hooy.teacher;
public class EnglishTeacher extends Teacher  {
    //超类里已经有的成员,但权限为私有,所以需要重新创建成员
    private String name;
    private String course;
    public EnglishTeacher(String name) {
       super();
       this.name = name;
       this.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;
    }
    
}

Class MathTeacher

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
package com.hooy.teacher;
public class MathTeacher extends Teacher {
    //超类里已经有的成员,但权限为私有,所以需要重新创建成员
    private String name;
    private String course;
    public MathTeacher(String name) {
       super();
       this.name = name;
       this.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;
    }
       
}
    

**Class **Examiner

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.hooy.teacher;
public class Examiner {
    public void judage(Teacher t) {
       t.selfIntroduce();
       if("数学".equals(t.getCourse())){
           System.out.println("1+1=?");
       }else if("英语".equals(t.getCourse())) {
           System.out.println("衬衫的价格?");
       }else {
           System.out.println("西游记作者:章承恩?");
       }
    }
}

Main Test

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