From b931fc5a5b3dcc0a689bd090ea3ee05ac5a39eef Mon Sep 17 00:00:00 2001 From: wu <303054730@qq.com> Date: Wed, 27 Nov 2024 12:18:06 +0800 Subject: [PATCH] add example --- examples/chapter07/AnimalWorld/readme.md | 5 + .../AnimalWorld/src/com/lk/world/Animal.java | 26 +++++ .../src/com/lk/world/AnimalWorld.java | 39 ++++++++ .../AnimalWorld/src/com/lk/world/Cat.java | 15 +++ .../AnimalWorld/src/com/lk/world/Dog.java | 23 +++++ .../AnimalWorld/src/com/lk/world/Duck.java | 23 +++++ .../src/com/lk/world/Swimable.java | 13 +++ .../src/com/lk/world/SwimingRace.java | 95 +++++++++++++++++++ 8 files changed, 239 insertions(+) create mode 100644 examples/chapter07/AnimalWorld/readme.md create mode 100644 examples/chapter07/AnimalWorld/src/com/lk/world/Animal.java create mode 100644 examples/chapter07/AnimalWorld/src/com/lk/world/AnimalWorld.java create mode 100644 examples/chapter07/AnimalWorld/src/com/lk/world/Cat.java create mode 100644 examples/chapter07/AnimalWorld/src/com/lk/world/Dog.java create mode 100644 examples/chapter07/AnimalWorld/src/com/lk/world/Duck.java create mode 100644 examples/chapter07/AnimalWorld/src/com/lk/world/Swimable.java create mode 100644 examples/chapter07/AnimalWorld/src/com/lk/world/SwimingRace.java diff --git a/examples/chapter07/AnimalWorld/readme.md b/examples/chapter07/AnimalWorld/readme.md new file mode 100644 index 0000000..d646b30 --- /dev/null +++ b/examples/chapter07/AnimalWorld/readme.md @@ -0,0 +1,5 @@ + +# 动物世界 + +演示java中基本的类、接口、继承、多态等知识。 + diff --git a/examples/chapter07/AnimalWorld/src/com/lk/world/Animal.java b/examples/chapter07/AnimalWorld/src/com/lk/world/Animal.java new file mode 100644 index 0000000..766345f --- /dev/null +++ b/examples/chapter07/AnimalWorld/src/com/lk/world/Animal.java @@ -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; + } +} diff --git a/examples/chapter07/AnimalWorld/src/com/lk/world/AnimalWorld.java b/examples/chapter07/AnimalWorld/src/com/lk/world/AnimalWorld.java new file mode 100644 index 0000000..30a534f --- /dev/null +++ b/examples/chapter07/AnimalWorld/src/com/lk/world/AnimalWorld.java @@ -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("=====比赛结束,小动物们度过了愉快的一天====="); + } +} \ No newline at end of file diff --git a/examples/chapter07/AnimalWorld/src/com/lk/world/Cat.java b/examples/chapter07/AnimalWorld/src/com/lk/world/Cat.java new file mode 100644 index 0000000..558c577 --- /dev/null +++ b/examples/chapter07/AnimalWorld/src/com/lk/world/Cat.java @@ -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()+":喵喵!"); + } +} diff --git a/examples/chapter07/AnimalWorld/src/com/lk/world/Dog.java b/examples/chapter07/AnimalWorld/src/com/lk/world/Dog.java new file mode 100644 index 0000000..5970d50 --- /dev/null +++ b/examples/chapter07/AnimalWorld/src/com/lk/world/Dog.java @@ -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; + } + +} diff --git a/examples/chapter07/AnimalWorld/src/com/lk/world/Duck.java b/examples/chapter07/AnimalWorld/src/com/lk/world/Duck.java new file mode 100644 index 0000000..da0b59d --- /dev/null +++ b/examples/chapter07/AnimalWorld/src/com/lk/world/Duck.java @@ -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; + } + +} diff --git a/examples/chapter07/AnimalWorld/src/com/lk/world/Swimable.java b/examples/chapter07/AnimalWorld/src/com/lk/world/Swimable.java new file mode 100644 index 0000000..2a3dc3b --- /dev/null +++ b/examples/chapter07/AnimalWorld/src/com/lk/world/Swimable.java @@ -0,0 +1,13 @@ +package com.lk.world; + +// 游泳接口 +public interface Swimable { + + /** + * 游泳函数,根据游泳的距离返回需要的时间 + * + * @param distance 游泳的距离,以米为单位 + * @return 返回需要的时间,单位为秒 + */ + int swim(int distance); +} diff --git a/examples/chapter07/AnimalWorld/src/com/lk/world/SwimingRace.java b/examples/chapter07/AnimalWorld/src/com/lk/world/SwimingRace.java new file mode 100644 index 0000000..ae0b624 --- /dev/null +++ b/examples/chapter07/AnimalWorld/src/com/lk/world/SwimingRace.java @@ -0,0 +1,95 @@ +package com.lk.world; + +import java.util.*; + +// 游泳比赛 +public class SwimingRace { + // 比赛名称 + String name; + + // 开始时间 + Date start; + + // 场地 + String location; + + // 参赛动物 + List members = new ArrayList<>(); + // 成绩 + private Map 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 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 getMembers() { + return members; + } + + public Map getResults() { + return results; + } +}