每日要點(diǎn)
接口
接口是方法聲明的集合
接口的三個(gè)關(guān)鍵點(diǎn):
- 接口代表能力 (兒子實(shí)現(xiàn)和尚的接口,就有和尚的能力)
- 接口代表約定 (兒子實(shí)現(xiàn)和尚的接口,必須全部實(shí)現(xiàn)接口里的抽象方法)
- 接口代表角色(實(shí)現(xiàn)x的接口,就可以扮演x角色)
就地實(shí)例化
所謂就地實(shí)例化實(shí)際上是創(chuàng)建了匿名內(nèi)部類(anonymous inner type)的對(duì)象
Monk wenshuMonk = new Monk() {
@Override
public void knockTheBell() {
}
} ;
適配器類
適配器類 - 缺省適配模式
public class Tianxing implements Monk {
@Override
public void chant() {
}
@Override
public void eatVegetable() {
}
@Override
public void knockTheBell() {
}
@Override
public void practiceKongfu() {
}
}
接口Java 8+
- 方法可以默認(rèn)
interface Foo {
public default void bar() {
System.out.println("hello");
}
}
interface Kao {
public default void bar() {
System.out.println("fuck");
}
}
public class A implements Foo, Kao {
@Override
public void bar() {
System.out.println("goodbye");
Foo.super.bar();
Kao.super.bar();
}
public static void main(String[] args) {
A a = new A();
a.bar();
}
}
-
Lambda表達(dá)式 --->匿名函數(shù)
僅限于接口只有一個(gè)方法 且 不是默認(rèn)
okButton.addActionListener(e -> {
changeBgColor();
});
注意
接口之間可以相互繼承而且允許多重繼承(一個(gè)接口繼承多個(gè)接口)
public interface NB extends Musician, Monk
雜項(xiàng)
-
類和類之間簡(jiǎn)單的說(shuō)有三種關(guān)系
- is-a關(guān)系 - 繼承 - 學(xué)生和人
- has-a關(guān)系 - 關(guān)聯(lián)(聚合/合成) - 撲克和一張牌
- use-a關(guān)系 - 依賴 - 人和房子
class Person {
private House h; // 關(guān)聯(lián)
public void buy(House h) { // 依賴
}
}
-
類和它實(shí)現(xiàn)的接口之間的關(guān)系:
- play-a 扮演 / like-a 像 --實(shí)現(xiàn)
-
命名相關(guān)
當(dāng)不知道去什么名時(shí)
// fuck up
public void foo();
// beyond all recognization
public void bar();
例子
- 1.點(diǎn)確定變化背景顏色
public class MyFrame extends JFrame {
// private int x;
// private int y;
public MyFrame() {
this.setSize(300, 200);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(null);
/* this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
});*/
JButton okButton = new JButton("確定");
okButton.setBounds(120, 100, 60, 30);
// 給按鈕對(duì)象添加行為監(jiān)聽(tīng)器
// okButton.addActionListener(new ActionListener() {
// // 接口的回調(diào)(callback)方法
// @Override
// public void actionPerformed(ActionEvent e) {
// changeBgColor();
// }
// });
// Java 8+ ---> Lambda表達(dá)式 --->匿名函數(shù)
// 僅限于接口只有一個(gè)方法 且 不是默認(rèn)
okButton.addActionListener(e -> {
changeBgColor();
});
this.add(okButton);
}
/* @Override
public void paint(Graphics g) {
super.paint(g);
g.drawOval(x - 50, y - 50, 100, 100);
}*/
public void changeBgColor() {
int r = (int) (Math.random() * 256);
int g = (int) (Math.random() * 256);
int b = (int) (Math.random() * 256);
this.getContentPane().setBackground(new Color(r, g, b));
}
public static void main(String[] args) {
new MyFrame().setVisible(true);
}
}
-
2.小球動(dòng)畫
小球類:
public class Ball {
private int x; // 左上角橫坐標(biāo)
private int y; // 左上角縱坐標(biāo)
private int size; // 尺寸
private Color color; // 顏色
private int sx; //速度在橫坐標(biāo)分量
private int sy; // 速度在縱坐標(biāo)的分量
public Ball(int x, int y, int size, Color color, int sx, int sy) {
this.x = x;
this.y = y;
this.size = size;
this.color = color;
this.sx = sx;
this.sy = sy;
}
/**
* 移動(dòng)
*/
public void move() {
x += sx;
y += sy;
if (x <= 0 || x >= 800 - size) {
sx = -sx;
}
if (y <= 30 || y >= 600 - size) {
sy = -sy;
}
}
/**
* 繪制小球
* @param g 畫筆
*/
public void draw(Graphics g) {
g.setColor(color);
g.fillOval(x, y, size, size);
}
}
窗口類:
public class BallFrame extends JFrame {
private BufferedImage image = new BufferedImage(800, 600, 1);
private Ball[] ballsArray = new Ball[100];
private int total = 0;
public BallFrame() {
this.setTitle("小球動(dòng)畫");
this.setSize(800, 600);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (total < ballsArray.length) {
int x = e.getX();
int y = e.getY();
int size = (int) (Math.random() * 81 + 20);
Color color = getRandomColor();
int sx;
int sy;
do {
sx = (int) (Math.random() * 21 - 10);
sy = (int) (Math.random() * 21 - 10);
} while (sx == 0 && sy == 0);
Ball ball = new Ball(x - size / 2, y - size / 2, size, color, sx, sy);
ballsArray[total++] = ball;
}
}
});
Timer timer = new Timer(40, e -> {
for (int i = 0; i < total; i++) {
Ball ball = ballsArray[i];
ball.move();
}
repaint();
});
timer.start();
}
private Color getRandomColor() {
int r = (int) (Math.random() * 256);
int g = (int) (Math.random() * 256);
int b = (int) (Math.random() * 256);
return new Color(r, g, b);
}
@Override
public void paint(Graphics g) {
Graphics otherGraphics = image.getGraphics();
super.paint(otherGraphics);
for (int i = 0; i < total; i++) {
Ball ball = ballsArray[i];
ball.draw(otherGraphics);
}
g.drawImage(image, 0, 0, null);
}
public static void main(String[] args) {
new BallFrame().setVisible(true);
}
}
作業(yè)
- **1.題目:
我想做燕子 只需簡(jiǎn)單思想 只求風(fēng)中流浪
我想做樹(shù) 不長(zhǎng)六腑五臟 不會(huì)寸斷肝腸
我做不成燕子 所以我 躲不過(guò)感情的墻
我做不成樹(shù) 因此也 撐不破傷心的網(wǎng)
來(lái)生做燕子吧 隨意找棵樹(shù)休息翅膀 然后淡然飛向遠(yuǎn)方
來(lái)生做樹(shù)吧 枝頭的燕子飛走時(shí) 不去留戀地張望 **
燕子接口:
public interface Swallow {
public default void fly() {}
public default void simpleThink() {}
public default void treeRest() {}
}
樹(shù)接口:
public interface Tree {
public default void photosynthesis() {}
}
人類:
public class Person implements Swallow, Tree {
String name;
public Person(String name) {
this.name = name;
}
public void complexThink() {
System.out.println(name + "正在思考復(fù)雜的事.");
}
public void fallInLove() {
System.out.println(name + "正在談戀愛(ài).");
}
public void weep() {
System.out.println(name + "正在哭泣.");
}
@Override
public void photosynthesis() {
System.out.println(name + "想變成一棵樹(shù)自由呼吸.");
}
@Override
public void fly() {
System.out.println(name + "想要變成燕子飛翔.");
}
@Override
public void simpleThink() {
System.out.println(name + "想要變成燕子簡(jiǎn)單的去思考.");
}
@Override
public void treeRest() {
System.out.println(name + "想要變成燕子在樹(shù)上休息.");
}
}
測(cè)試類:
Person person = new Person("小白");
person.complexThink();
person.fallInLove();
person.weep();
Swallow swallow = person;
swallow.fly();
swallow.simpleThink();
swallow.treeRest();
Tree tree = person;
tree.photosynthesis();