Java学习笔记 - 第013天

每日要点

接口

接口是方法声明的集合

接口的三个关键点:
  • 接口代表能力 (儿子实现和尚的接口,就有和尚的能力)
  • 接口代表约定 (儿子实现和尚的接口,必须全部实现接口里的抽象方法)
  • 接口代表角色(实现x的接口,就可以扮演x角色)
就地实例化

所谓就地实例化实际上是创建了匿名内部类(anonymous inner type)的对象

    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+
  • 方法可以默认
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表达式 --->匿名函数
    仅限于接口只有一个方法 且 不是默认
        okButton.addActionListener(e -> {
            changeBgColor();
        });
注意

接口之间可以相互继承而且允许多重继承(一个接口继承多个接口)
public interface NB extends Musician, Monk

杂项

  • 类和类之间简单的说有三种关系
    • is-a关系 - 继承 - 学生和人
    • has-a关系 - 关联(聚合/合成) - 扑克和一张牌
    • use-a关系 - 依赖 - 人和房子
  class Person {
    private House h; // 关联
    public void buy(House h) { // 依赖
    }
  }
  • 类和它实现的接口之间的关系:
    • play-a 扮演 / like-a 像 --实现
  • 命名相关
    当不知道去什么名时
    // fuck up
    public void foo();
    
    // beyond all recognization
    public void bar();

例子

  • 1.点确定变化背景颜色
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);
        // 给按钮对象添加行为监听器
//      okButton.addActionListener(new ActionListener() {
//          // 接口的回调(callback)方法
//          @Override
//          public void actionPerformed(ActionEvent e) {
//              changeBgColor();
//          }
//      });     
        // Java 8+ ---> Lambda表达式 --->匿名函数
        // 仅限于接口只有一个方法 且 不是默认
        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.小球动画
    小球类:
public class Ball {
    private int x;         // 左上角横坐标
    private int y;         // 左上角纵坐标
    private int size;      // 尺寸
    private Color color;   // 颜色
    private int sx;        //速度在横坐标分量
    private int sy;        // 速度在纵坐标的分量
    
    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;
    }
    
    /**
     * 移动
     */
    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("小球动画");
        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);
    }
}

作业

  • **1.题目:
    我想做燕子 只需简单思想 只求风中流浪
    我想做树 不长六腑五脏 不会寸断肝肠
    我做不成燕子 所以我 躲不过感情的墙
    我做不成树 因此也 撑不破伤心的网
    来生做燕子吧 随意找棵树休息翅膀 然后淡然飞向远方
    来生做树吧 枝头的燕子飞走时 不去留恋地张望 **
    燕子接口:
public interface Swallow {
    
    public default void fly() {}
    
    public default void simpleThink() {}
    
    public default void treeRest() {}
}

树接口:

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 + "正在思考复杂的事.");
    }

    public void fallInLove() {
        System.out.println(name + "正在谈恋爱.");
    }
    
    public void weep() {
        System.out.println(name + "正在哭泣.");
    }

    @Override
    public void photosynthesis() {
        System.out.println(name + "想变成一棵树自由呼吸.");
    }

    @Override
    public void fly() {
        System.out.println(name + "想要变成燕子飞翔.");
    }

    @Override
    public void simpleThink() {
        System.out.println(name + "想要变成燕子简单的去思考.");
    }

    @Override
    public void treeRest() {
        System.out.println(name + "想要变成燕子在树上休息.");
    }   
}

测试类:

        Person person = new Person("小白");
        person.complexThink();
        person.fallInLove();
        person.weep();
        
        Swallow swallow = person;
        swallow.fly();
        swallow.simpleThink();
        swallow.treeRest();
        
        Tree tree = person;
        tree.photosynthesis();
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,319评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,801评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,567评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,156评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,019评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,090评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,500评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,192评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,474评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,566评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,338评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,212评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,572评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,890评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,169评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,478评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,661评论 2 335

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,493评论 18 399
  • 每日要点 面向对象的四大支柱 抽象 - 定义一个类的过程就是一个抽象的过程(数据抽象、行为抽象),通过抽象我们可以...
    迷茫o阅读 381评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,494评论 18 139
  • 每日要点 工厂 简单工厂模式/静态工厂模式用工厂创建对象(跟具体的类型实现解耦合) static static属于...
    迷茫o阅读 241评论 0 0
  • 每日要点 容器 容器(集合框架Container) - 承载其他对象的对象 Collection List Arr...
    迷茫o阅读 159评论 0 0