例题

案例一:群主发红包

  1. 某群有一管理员,多个群员,管理员给群员发红包
  2. 从管理员余额中扣除一定金额,并分为n等份,让成员领取
public class User {
	private int money;
	private String name;
	public User() {super();}
	public User(int money, String name) {
		super();
		this.money = money;
		this.name = name;】
	}
	public void show() {System.out.println(this.getMoney());}
	public int getMoney() {return money;}
	public void setMoney(int money) {this.money = money;}
	public String getName() {return name;}
	public void setName(String name) {this.name = name;}
}
package demo12;

import java.util.ArrayList;

public class Manager extends User{
	public Manager() {
		super();
	}
	public Manager(int money, String name) {
		super(money, name);
	}
	public ArrayList<Integer> send(int totalMoney, int count){
		ArrayList<Integer> list = new ArrayList<>();
		if (totalMoney > super.getMoney()) {
			System.out.println("余额不足");
			return list;
		}
		int sum = 0;
		for (int i = 0; i < count - 1; i++) {
			list.add(totalMoney / count);
			sum += totalMoney / count;
		}
		list.add(totalMoney - sum);
		super.setMoney(super.getMoney() - totalMoney);
		return list;
	}
}
package demo12;

import java.util.ArrayList;
import java.util.Random;

public class Number extends User{
	public Number() {
		super();
	}
	public Number(int money, String name) {
		super(money, name);
	}
	
	public void recive(ArrayList<Integer> list) {
		if (list.size() != 0) {
			int index = new Random().nextInt(list.size());
			int get_money = list.remove(index);
			super.setMoney(super.getMoney() + get_money);
		}
	}
}
package demo12;
import java.util.ArrayList;

public class Test1 {
	public static void main(String[] args) {
		Manager manager = new Manager();
		manager.show();
		Number number1 = new Number();
		Number number2 = new Number();
		Number number3 = new Number();
		manager.setMoney(30);
		manager.show();
		ArrayList<Integer> list = manager.send(29, 3);
		number1.recive(list);
		number2.recive(list);
		number3.recive(list);
		number1.show();
		number2.show();
		number3.show();
		manager.show();
	}

二、接口多态练习:

  • 电脑有使用USB的功能,键盘和鼠标也是USB,现要用电脑类实现键盘鼠标的功能
public interface Usb{
	abstract void turnOn();
	abstract void turnOff();
	abstract void run();
}
// 注意Keybound是类,不是接口
public class Keybound implements Usb{

	@Override
	public void turnOn() {
		System.out.println("键盘开");
	}

	@Override
	public void turnOff() {
		System.out.println("键盘关");
	}
	
	@Override
	public void run() {
		System.out.println("键盘运行");
	}
}
// 注意Mouse是类,不是接口
public class Mouse implements Usb{
	
	@Override
	public void turnOn() {
		System.out.println("鼠标开");
	}
	
	@Override
	public void turnOff() {
		System.out.println("鼠标关");
	}
	
	@Override
	public void run() {
		System.out.println("键盘运行");
	}
}
// 电脑类不能继承Usb,Mouse等
public class Computer {
	public void turnOnComputer() {
		System.out.println("开机");
	}
	public void turnOffComputer() {
		System.out.println("关机");
	}
	public void display(Usb usb) {
		usb.turnOn();
		if (usb instanceof Keybound) {
			Keybound keybound = (Keybound) usb;
			keybound.run();
		}
		if (usb instanceof Mouse) {
			Mouse mouse = (Mouse) usb;
			mouse.run();
		}
		usb.turnOff();
	}
}
public class Test1 {
	public static void main(String[] args) {
		Computer computer = new Computer();
		computer.turnOnComputer();
		
		Mouse mouse = new Mouse();
		computer.display(mouse);	// 被向上转型
		
		Keybound keybound = new Keybound();
		computer.display(keybound);
		
		computer.turnOffComputer();
	}
}
posted @ 2021-11-24 21:48  亓亓fr  阅读(30)  评论(0编辑  收藏  举报