add example

This commit is contained in:
wu 2024-11-27 12:18:06 +08:00
parent 8e4db3d25b
commit b931fc5a5b
8 changed files with 239 additions and 0 deletions

View File

@ -0,0 +1,5 @@
# 动物世界
演示java中基本的类、接口、继承、多态等知识。

View 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;
}
}

View File

@ -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("=====比赛结束,小动物们度过了愉快的一天=====");
}
}

View 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()+":喵喵!");
}
}

View 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;
}
}

View 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;
}
}

View File

@ -0,0 +1,13 @@
package com.lk.world;
// 游泳接口
public interface Swimable {
/**
* 游泳函数根据游泳的距离返回需要的时间
*
* @param distance 游泳的距离以米为单位
* @return 返回需要的时间单位为秒
*/
int swim(int distance);
}

View File

@ -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;
}
}