美文网首页
Java学习第14天

Java学习第14天

作者: _Raye | 来源:发表于2016-12-14 20:49 被阅读0次

接口

  • (英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明。一个类通过实现接口的方式,从而来继承接口的抽象方法。

  • 接口是方法声明的集合。接口里的方法是自动公开的而且都是抽象的,编译器会自动加上public abstract

  • 接口里的方法都是抽象的,不能再接口中实现。但是Java 8 以后,接口中的方法可以通过default来默认实现。实现接口的类在实现接口(implements)的方法中可以重写这些方法,若不重写,这些方法会使用默认的实现方法(default)

  • 一个类只能继承一个抽象类,而一个类却可以实现多个接口,通过逗号连接

  • 接口没有构造方法

  • 接口可以多重继承(可以继承其他接口)

  • 类和类之间简单的说有三种关系:

  1. is-a 关系 - 继承 - 学生和人
  2. has-a关系 - 关联(聚合/合成) - 扑克和一张牌
  3. use-a关系 - 依赖 - 人和房子
  • 类和它实现的接口之间的关系:
    play-a / like-a - 实现

#######接口案例:

  • 爸爸类
package org.mobiletrain;

public class Father {

    protected String name;
    
    public Father(String name) {
        this.name = name;
    }
    
    public void gamble(){
        System.out.println(name + "正在赌博");
    }
}
  • 和尚类
package org.mobiletrain;


public interface Monk {

    public default void chant(){
        System.out.println("hello");
    }
    
    public void eatVegetable();
    
    public void knockTheBell();
}
  • 音乐家类
package org.mobiletrain;

public interface Musician {

    void playPiano();
    
    void playViolin();
}
  • son类继承了father类,同时实现了monk和musician接口
package org.mobiletrain;

public class Son extends Father implements Monk,Musician {

    public Son(String name) {
        super(name);
        
    }

    public void somke(){
        System.out.println(name + "正在抽烟");
    }

    @Override
    public void chant() {       
         System.out.println(name + "正在念《大悲咒》");
    }

    @Override
    public void eatVegetable() {
        System.out.println(name + "正在念吃斋");
    }

    @Override
    public void knockTheBell() {
        System.out.println(name + "正在敲钟");
    }

    @Override
    public void playPiano() {       
        System.out.println(name + "正在弹钢琴");
    }

    @Override
    public void playViolin() {
        System.out.println(name + "正在拉小提琴");
    }
    
    
}

Java 8 出现的适配器(Adapter)

  • 使用适配器,可以选择性地实现一个接口中的某些方法;

####### 鼠标点击,出现随机颜色随机大小的圆,且不会移动出边框

  • 先创建一个ball类
package org.mobiletrain.ui;

import java.awt.Color;
import java.awt.Graphics;

/**
 * 小球
 * @author apple
 *
 */
public class Ball {

    protected Color color;//颜色
    protected int x;    //左上角横坐标
    protected int y;    //左上角纵坐标
    protected int size; //尺寸
    protected int sx;   //速度在横坐标上的分量
    protected int sy;   //速度在纵坐标上的分量
    
    public Ball(Color color,int x,int y,int size,int sx,int sy) {
        this.color = color;
        this.x = x;
        this.y = y;
        this.size = size;
        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;
        }
    }
    
    public void draw(Graphics g){
        g.setColor(color);
        g.fillOval(x - size / 2, y - size / 2, size, size);
    }
    
}
  • 再创建一个窗口(继承JFrame)
package org.mobiletrain.ui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.Timer;

//动画
@SuppressWarnings("serial")
public class Frame extends JFrame {
    private BufferedImage image = new BufferedImage(800, 600, 1);
    //private Ball ball = new Ball(Color.BLUE, 20, 30, 80, 4, 4);
    private Ball ballsArray[] = new Ball[100];
    private int total = 0;
    
    public Frame(){
        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);
                    int sx = (int) (Math.random() * 20 - 10);
                    int sy = (int) (Math.random() * 20 - 10);
                    Color color = getRandmColor();
                        Ball ball = new Ball(color, x, y, size, sx, sy);
                        ballsArray[total++] = ball;             
                }
            }
        });
        
        
        Timer timer = new Timer(40, e -> {
            //ball.move();
            for (int i = 0; i < total; i++) {
                ballsArray[i].move();
            }
            repaint();
        });
        timer.start();
    }
    
    public Color getRandmColor(){
        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);
        //ball.draw(otherGraphics);
        for(int i = 0;i < total;i++){
            ballsArray[i].draw(otherGraphics);
        }
        g.drawImage(image,0,0,null);
    }
    
    
    public static void main(String[] args) {
        new Frame().setVisible(true);
    }
}

lambda表达式

  • Java 8+ ---> lambda表达式(匿名函数)
    仅限于接口中只有一个方法,而且没有默认实现

#######在窗口设置一个按钮,点击按钮,窗口背景颜色更改

package org.mobiletrain.ui;

import java.awt.Color;
//import java.awt.event.ActionEvent;
//import java.awt.event.ActionListener;


import javax.swing.JButton;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class MyFrame extends JFrame {

    public MyFrame(){
        this.setSize(300,200);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLayout(null);
        
        JButton okButton = new JButton("确定");
        okButton.setBounds(120, 100, 60, 30);
//      ActionListener listener = new ActionListener() {
//          
//          //只有一个方法的接口(函数接口),里面的方法通常都是回调方法
//          //接口的回调(callback)方法
//          @Override
//          public void actionPerformed(ActionEvent e) {
//              changeBgColor();
//              
//          }
//      }
        
        //Java 8+ ---> lambda表达式(匿名函数)
        //仅限于接口中只有一个方法,而且没有默认实现
        okButton.addActionListener(e -> {
            changeBgColor();
        });
        //okButton.addActionListener(listener);//动作监听器,监听按钮的点击事件
        this.add(okButton);
    }
    
    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);
    }
}

相关文章

网友评论

      本文标题:Java学习第14天

      本文链接:https://www.haomeiwen.com/subject/wgqwmttx.html