From 9db82eb8453586d83953513565ef8f7c622e9dbc Mon Sep 17 00:00:00 2001 From: ZhangJinxuan <2194936226@qq.com> Date: Tue, 31 Mar 2026 10:29:57 +0800 Subject: [PATCH 1/3] Add ShapeDemo.java in w5 folder --- 6/w5/ShapeDemo.java | 51 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 6/w5/ShapeDemo.java diff --git a/6/w5/ShapeDemo.java b/6/w5/ShapeDemo.java new file mode 100644 index 0000000..998237d --- /dev/null +++ b/6/w5/ShapeDemo.java @@ -0,0 +1,51 @@ +public class ShapeDemo { + public static void main(String[] args) { + // 创建不同形状的对象 + Shape circle = new Circle(); + Shape rectangle = new Rectangle(); + + // 测试drawShape方法 + drawShape(circle); + drawShape(rectangle); + } + + /** + * 调用形状的draw方法 + * @param s 形状对象 + */ + public static void drawShape(Shape s) { + s.draw(); + } +} + +/** + * 形状基类 + */ +class Shape { + /** + * 绘制方法 + */ + public void draw() { + System.out.println("绘制形状"); + } +} + +/** + * 圆形类 + */ +class Circle extends Shape { + @Override + public void draw() { + System.out.println("绘制圆形"); + } +} + +/** + * 矩形类 + */ +class Rectangle extends Shape { + @Override + public void draw() { + System.out.println("绘制矩形"); + } +} \ No newline at end of file From 236d7ae1260f9d6980b3d389c1771a9038f50c53 Mon Sep 17 00:00:00 2001 From: ZhangJinxuan <2194936226@qq.com> Date: Sun, 19 Apr 2026 19:22:15 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=B8=8A=E4=BC=A0w6=E9=87=8C=E7=9A=84?= =?UTF-8?q?=E6=89=80=E6=9C=89Java=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 动物叫声系统.java | 31 +++++++++++++++++++++++++++++++ 测试.java | 23 +++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 动物叫声系统.java create mode 100644 测试.java diff --git a/动物叫声系统.java b/动物叫声系统.java new file mode 100644 index 0000000..563e4ee --- /dev/null +++ b/动物叫声系统.java @@ -0,0 +1,31 @@ +abstract class Animal { + // 抽象方法:动物叫声 + public abstract void makeSound(); +} + +// 2. Swimmable 接口 +interface Swimmable { + // 游泳方法 + void swim(); +} + +// 3. Dog 类:继承 Animal,实现 Swimmable +class Dog extends Animal implements Swimmable { + @Override + public void makeSound() { + System.out.println("小狗汪汪叫:汪汪汪!"); + } + + @Override + public void swim() { + System.out.println("小狗在水里狗刨式游泳!"); + } +} + +// 4. Cat 类:继承 Animal,不实现 Swimmable +class Cat extends Animal { + @Override + public void makeSound() { + System.out.println("小猫喵喵叫:喵喵喵!"); + } +} \ No newline at end of file diff --git a/测试.java b/测试.java new file mode 100644 index 0000000..6abb25b --- /dev/null +++ b/测试.java @@ -0,0 +1,23 @@ +public class AnimalTest { + public static void main(String[] args) { + // 多态方式创建对象 + Animal dog = new Dog(); + Animal cat = new Cat(); + + // 调用 makeSound 方法(多态体现) + System.out.println("=== 动物叫声测试 ==="); + dog.makeSound(); + cat.makeSound(); + + // 测试 Swimable 接口(Dog 能游泳,Cat 不能) + System.out.println("\n=== 游泳能力测试 ==="); + if (dog instanceof Swimmable) { + ((Swimmable) dog).swim(); + } + if (cat instanceof Swimmable) { + ((Swimmable) cat).swim(); + } else { + System.out.println("小猫不会游泳哦!"); + } + } +} \ No newline at end of file From e95d7ecd943c8f546b1aad83d30fc79bd8742204 Mon Sep 17 00:00:00 2001 From: ZhangJinxuan <2194936226@qq.com> Date: Mon, 27 Apr 2026 14:20:44 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=EF=BC=9A=E4=B8=8A=E4=BC=A0w7=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A4=B9=E6=89=80=E6=9C=89=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ScoreAverge.java | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 ScoreAverge.java diff --git a/ScoreAverge.java b/ScoreAverge.java new file mode 100644 index 0000000..3716739 --- /dev/null +++ b/ScoreAverge.java @@ -0,0 +1,46 @@ +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +public class ScoreCalculator { + public static void main(String[] args) { + String fileName = "scores.txt"; + int sum = 0; + int count = 0; + + // try-with-resources 自动关闭流,无需手动写 br.close() + try (BufferedReader br = new BufferedReader(new FileReader(fileName))) { + String line; + while ((line = br.readLine()) != null) { + // 去除字符串前后空格,避免空行或空格影响解析 + line = line.trim(); + if (line.isEmpty()) { + continue; // 跳过空行 + } + + try { + // 解析成绩为整数 + int score = Integer.parseInt(line); + sum += score; + count++; + } catch (NumberFormatException e) { + // 数字格式错误处理 + System.err.println("警告:无法解析成绩 '" + line + "',跳过该数据"); + } + } + + // 计算平均分 + if (count > 0) { + double average = (double) sum / count; + System.out.println("平均分:" + average); + } else { + System.out.println("文件中没有有效成绩数据"); + } + + } catch (IOException e) { + // 文件不存在、读取错误处理 + System.err.println("读取文件时发生错误:" + e.getMessage()); + e.printStackTrace(); + } + } +} \ No newline at end of file