mirror of
https://gitee.com/many2many/java-web.git
synced 2025-01-11 14:40:55 +08:00
add example
This commit is contained in:
parent
8e4db3d25b
commit
b931fc5a5b
5
examples/chapter07/AnimalWorld/readme.md
Normal file
5
examples/chapter07/AnimalWorld/readme.md
Normal file
@ -0,0 +1,5 @@
|
||||
|
||||
# 动物世界
|
||||
|
||||
演示java中基本的类、接口、继承、多态等知识。
|
||||
|
26
examples/chapter07/AnimalWorld/src/com/lk/world/Animal.java
Normal file
26
examples/chapter07/AnimalWorld/src/com/lk/world/Animal.java
Normal file
@ -0,0 +1,26 @@
|
||||
package com.lk.world;
|
||||
|
||||
/**
|
||||
* 动物抽象类
|
||||
*/
|
||||
public abstract class Animal {
|
||||
// 动物的名字
|
||||
private String name;
|
||||
|
||||
// 构造方法
|
||||
public Animal(String name) {
|
||||
this.name = name;
|
||||
System.out.println(name + "在动物世界诞生了");
|
||||
}
|
||||
|
||||
// 抽象方法:每个动物都有自己的叫声
|
||||
abstract void sound();
|
||||
|
||||
// 具体方法:介绍自己
|
||||
void introduce() {
|
||||
System.out.println("HI,我的名字是:" + name);
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.lk.world;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class AnimalWorld {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("============欢迎来到动物世界============");
|
||||
Dog wangcai = new Dog("旺财");
|
||||
wangcai.introduce();
|
||||
|
||||
Dog dahuang = new Dog("大黄");
|
||||
dahuang.introduce();
|
||||
|
||||
Cat xiaohua = new Cat("小花");
|
||||
xiaohua.introduce();
|
||||
|
||||
Cat xiaohei = new Cat("小黑");
|
||||
xiaohei.introduce();
|
||||
|
||||
Duck xhy = new Duck("小黄鸭");
|
||||
xhy.introduce();
|
||||
|
||||
Duck cxy = new Duck("丑小鸭");
|
||||
cxy.introduce();
|
||||
|
||||
SwimingRace race = new SwimingRace("游泳冠军争霸赛", new Date(), "沂河");
|
||||
System.out.println("========="+race.getName()+"即将开始============");
|
||||
race.sighUp(wangcai);
|
||||
race.sighUp(xhy);
|
||||
race.sighUp(xiaohua);
|
||||
race.sighUp(dahuang);
|
||||
|
||||
System.out.println("============比赛开始===============");
|
||||
race.play(100);
|
||||
race.anounceWinner();
|
||||
|
||||
System.out.println("=====比赛结束,小动物们度过了愉快的一天=====");
|
||||
}
|
||||
}
|
15
examples/chapter07/AnimalWorld/src/com/lk/world/Cat.java
Normal file
15
examples/chapter07/AnimalWorld/src/com/lk/world/Cat.java
Normal file
@ -0,0 +1,15 @@
|
||||
package com.lk.world;
|
||||
|
||||
// 继承Animal类
|
||||
public class Cat extends Animal{
|
||||
// 构造方法,子类必须重写父类的构造方法
|
||||
public Cat(String name){
|
||||
super(name);
|
||||
}
|
||||
|
||||
// 重写父类的抽象方法
|
||||
@Override
|
||||
public void sound(){
|
||||
System.out.println(getName()+":喵喵!");
|
||||
}
|
||||
}
|
23
examples/chapter07/AnimalWorld/src/com/lk/world/Dog.java
Normal file
23
examples/chapter07/AnimalWorld/src/com/lk/world/Dog.java
Normal file
@ -0,0 +1,23 @@
|
||||
package com.lk.world;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
// 狗,继承动物类,实现游泳接口
|
||||
public class Dog extends Animal implements Swimable {
|
||||
public Dog(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sound() {
|
||||
System.out.println(getName() + ": 汪汪!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int swim(int distance){
|
||||
int seconds = new Random().nextInt(distance/3, distance);
|
||||
System.out.println(getName()+"游了"+seconds+"秒");
|
||||
return seconds;
|
||||
}
|
||||
|
||||
}
|
23
examples/chapter07/AnimalWorld/src/com/lk/world/Duck.java
Normal file
23
examples/chapter07/AnimalWorld/src/com/lk/world/Duck.java
Normal file
@ -0,0 +1,23 @@
|
||||
package com.lk.world;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
// 鸭子,继承动物类,实现游泳接口
|
||||
public class Duck extends Animal implements Swimable {
|
||||
public Duck(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sound() {
|
||||
System.out.println(getName()+": 嘎嘎!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int swim(int distance){
|
||||
int seconds = new Random().nextInt(distance/4, distance/2);
|
||||
System.out.println(getName()+"游了"+seconds+"秒");
|
||||
return seconds;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.lk.world;
|
||||
|
||||
// 游泳接口
|
||||
public interface Swimable {
|
||||
|
||||
/**
|
||||
* 游泳函数,根据游泳的距离返回需要的时间
|
||||
*
|
||||
* @param distance 游泳的距离,以米为单位
|
||||
* @return 返回需要的时间,单位为秒
|
||||
*/
|
||||
int swim(int distance);
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
package com.lk.world;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
// 游泳比赛
|
||||
public class SwimingRace {
|
||||
// 比赛名称
|
||||
String name;
|
||||
|
||||
// 开始时间
|
||||
Date start;
|
||||
|
||||
// 场地
|
||||
String location;
|
||||
|
||||
// 参赛动物
|
||||
List<Animal> members = new ArrayList<>();
|
||||
// 成绩
|
||||
private Map<Animal, Integer> results = new HashMap<>();
|
||||
|
||||
// 构造器
|
||||
public SwimingRace(String name, Date start, String location) {
|
||||
this.name = name;
|
||||
this.start = start;
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
/**
|
||||
* 动物报名游泳比赛的方法
|
||||
*
|
||||
* @param animal 动物对象,尝试报名参加游泳比赛
|
||||
* @return 如果动物可以参加游泳比赛,返回true;否则,返回false
|
||||
*/
|
||||
public boolean sighUp(Animal animal) {
|
||||
// 检查动物是否能游泳
|
||||
if(animal instanceof Swimable){
|
||||
// 如果能游泳,将动物添加到成员列表中
|
||||
members.add(animal);
|
||||
// 发出声音,表示报名成功
|
||||
animal.sound();
|
||||
return true;
|
||||
}else{
|
||||
// 如果不能游泳,输出提示信息
|
||||
System.out.println("很遗憾,"+animal.getName()+"不能参加游泳比赛");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 比赛
|
||||
public void play(int distance) {
|
||||
|
||||
for (Animal a : members) {
|
||||
int seconds = ((Swimable) a).swim(distance);
|
||||
results.put(a, seconds);
|
||||
}
|
||||
}
|
||||
|
||||
// 宣布冠军
|
||||
public void anounceWinner() {
|
||||
Animal champion = null;
|
||||
int minTime = Integer.MAX_VALUE;
|
||||
|
||||
for (Map.Entry<Animal, Integer> entry : results.entrySet()) {
|
||||
Animal animal = entry.getKey();
|
||||
int time = entry.getValue();
|
||||
if (time < minTime) {
|
||||
minTime = time;
|
||||
champion = animal;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("冠军是:" + champion.getName()+" 成绩是:"+minTime);
|
||||
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Date getStart() {
|
||||
return start;
|
||||
}
|
||||
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public List<Animal> getMembers() {
|
||||
return members;
|
||||
}
|
||||
|
||||
public Map<Animal, Integer> getResults() {
|
||||
return results;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user