Day12_14

一.接口的使用

  • interface implements
package Day12_14_01;

/*
 * 接口的三个关键点:
 * 1.接口代表能力(实现了接口就表示拥有了该接口的能力(方法))
 * 2.接口代表约定(实现某接口之后必须要将接口的所有方法重写,否则必须声明该类为抽象类)
 * 3.接口代表角色(实现了接口 就扮演了该接口所表示的角色)
 * 
 * 接口之间允许相互继承的,而且支持多重继承(一个接口继承多个接口)
 * 
 * 
 */

public class FatherTest
{

    public static void main(String[] args)
    {
        Son son =new Son("小明");
        son.gamble();
        son.smoke();
        son.chant();
        son.eatVegetable();
        son.knockTheBell();
        son.playPiano();
        son.playViolin();
        
        
//      抽象类和接口本身不能实例化,但是可以使用就地实例化的方式来创建对象
//      所谓就地实例化就是创建了一个匿名内部类(anonymous inner type)的对象
        Musician musician=new Musician()
        {
            
            @Override
            public void playViolin()
            {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void playPiano()
            {
                // TODO Auto-generated method stub
                
            }
        };
        
        Father father=son;
        father.gamble();
        //father.smoke();当son赋值给father后,因为father指定了是Father类型,
        //因此son不再具有Son的行为 ,也就是son会损失Son里面的行为,不在具有somke()行为
        
        
        Monk monk =son;
        monk.chant();
        //同理,将son赋值给monk,因为monk指定为Monk类,所以只能有Monk接口的方法
        //Musician musician=son也如此
    }

}```

package Day12_14_01;

public class Father
{
public String name;

public Father(String name)
{
    this.name= name;
}

public void gamble()
{
    System.out.println(this.name+"正在赌博!");
}

public void smoke()
{
    System.out.println(this.name+"会抽烟");
}

}


package Day12_14_01;

public class Son extends Father implements Monk,Musician
{

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

public void smoke()
{
    System.out.println(this.name+"会抽烟");
}

@Override
public void chant()
{
    System.out.println(name+"念经");
}

@Override
public void eatVegetable()
{   
    System.out.println(this.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+"拉小提琴");
    
}

}


package Day12_14_01;

public interface Monk
{
public void chant();

public void eatVegetable();

public void knockTheBell();

}


package Day12_14_01;

public interface Musician
{
public void playPiano();

public void playViolin();

}


#二.球球案例
- 适配器和lanmda表示的使用(java8新特性)

package Day12_14_02;

public class MeFrameTest
{
public static void main(String[] args)
{
new MeFrame().setVisible(true);
}
}


package Day12_14_02;

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 MeFrame extends JFrame
{
private BufferedImage image = new BufferedImage(800, 600, 1);

private Ball[] ballaArray = new Ball[100];
private int total = 0;

public MeFrame()
{
    this.setTitle("小球");
    this.setSize(800, 600);
    this.setLocationRelativeTo(null);
    this.setResizable(false);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    this.addMouseListener(new MouseAdapter()
    {
        @Override
        public void mousePressed(MouseEvent e)
        {
            if (total < ballaArray.length)
            {
                int x = e.getX();
                int y = e.getY();
                int size =CommonUtil.randomInt(20, 100);
                int sx = CommonUtil.randomInt(-10, 10);
                int sy = CommonUtil.randomInt(-10, 10);
                Color color =CommonUtil.RandomColor();

                Ball ball = new Ball(x, y, size, color, sx, sy);
                ballaArray[total] = ball;
                total++;

            }
        }

    });

    Timer timer = new Timer(10, e ->
    {
        for (int i = 0; i < total; i++)
        {
            ballaArray[i].move();
        }
        repaint();

    });

    timer.start();
}


public void paint(Graphics g)
{
    Graphics otherGrephics = image.getGraphics();
    super.paint(otherGrephics);
    for (int i = 0; i < total; i++)
    {
        ballaArray[i].draw(otherGrephics);
    }

    g.drawImage(image, 0, 0, null);
}

}


package Day12_14_02;

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

public class Ball
{

private int y;
private int size;
private Color color;
private int sx;
private int sy;

private int x;
public Ball(int x, int y, int size, Color color, int sx, int sy)
{
    super();
    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;
    }
}

public void draw(Graphics g)
{
    g.setColor(color);
    g.fillOval(x, y, size, size);
}

}


#三.创建一个属于自己的工具类

package Day12_14_02;

import java.awt.Color;

/*

  • 写一个工具类的要点:

  • 1.所有方法都应该是静态方法

  • 2.将构造器私有,不允许调用构造器创建对象

  • 3.工具类一般不会被继承,所以是final的
    */
    public final class CommonUtil
    {
    private CommonUtil()
    {
    //防止内部调用构造器创建对象
    throw new AssertionError();
    }

    public static int randomInt(int min,int max)
    {
    return (int) (Math.random()*(max-min+1)+min);
    }

    public static Color RandomColor()
    {
    return new Color(randomInt(0, 255), randomInt(0, 255), randomInt(0, 55));
    }
    }

你可能感兴趣的:(Day12_14)