Compare commits
No commits in common. 'main' and 'master' have entirely different histories.
@ -1,71 +0,0 @@ |
|||||
package com.example; |
|
||||
|
|
||||
import com.example.bean.Movie; |
|
||||
import com.example.crawler.MovieCrawler; |
|
||||
import com.example.chart.DrawChart; |
|
||||
import java.io.FileWriter; |
|
||||
import java.util.ArrayList; |
|
||||
import java.util.List; |
|
||||
import java.util.Map; |
|
||||
import java.util.stream.Collectors; |
|
||||
|
|
||||
public class Main { |
|
||||
public static void main(String[] args) { |
|
||||
|
|
||||
// 1. 爬取数据
|
|
||||
MovieCrawler crawler = new MovieCrawler(); |
|
||||
List<Movie> movies = crawler.start(); |
|
||||
|
|
||||
// 2. 数据清洗
|
|
||||
List<Movie> cleanData = new ArrayList<>(); |
|
||||
for (Movie movie : movies) { |
|
||||
if (movie.getTitle() != null && !movie.getTitle().isBlank()) { |
|
||||
cleanData.add(movie); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
// ===================== CSV 保存功能 恢复!=====================
|
|
||||
try (FileWriter writer = new FileWriter("movie_top100.csv")) { |
|
||||
writer.write("电影名称,评分,导演,上映年份\n"); |
|
||||
for (Movie m : cleanData) { |
|
||||
writer.write(m.getTitle() + "," + m.getScore() + "," + m.getDirector() + "," + m.getYear() + "\n"); |
|
||||
} |
|
||||
System.out.println("✅ CSV 文件已保存:movie_top100.csv"); |
|
||||
} catch (Exception e) { |
|
||||
e.printStackTrace(); |
|
||||
} |
|
||||
// ==============================================================
|
|
||||
|
|
||||
// 3. 评分统计
|
|
||||
Map<String, Long> scoreCount = cleanData.stream() |
|
||||
.collect(Collectors.groupingBy(Movie::getScore, Collectors.counting())); |
|
||||
|
|
||||
// 4. 控制台打印表格
|
|
||||
System.out.println("\n=========================================="); |
|
||||
System.out.println(" 电影评分统计表格 "); |
|
||||
System.out.println("=========================================="); |
|
||||
System.out.printf("%-10s %-10s %-10s%n", "评分", "数量(部)", "占比(%)"); |
|
||||
System.out.println("------------------------------------------"); |
|
||||
|
|
||||
double total = cleanData.size(); |
|
||||
for (Map.Entry<String, Long> entry : scoreCount.entrySet()) { |
|
||||
String score = entry.getKey(); |
|
||||
long count = entry.getValue(); |
|
||||
double rate = (count * 100.0) / total; |
|
||||
System.out.printf("%-12s %-12d %-10.2f%n", score, count, rate); |
|
||||
} |
|
||||
|
|
||||
System.out.println("------------------------------------------"); |
|
||||
System.out.printf("总计:%d 部电影%n", (long) total); |
|
||||
System.out.println("=========================================="); |
|
||||
|
|
||||
// 5. 生成正常折线图:按排名 1→100 顺序连线
|
|
||||
List<Double> scoreList = new ArrayList<>(); |
|
||||
for (Movie movie : cleanData) { |
|
||||
scoreList.add(Double.parseDouble(movie.getScore())); |
|
||||
} |
|
||||
|
|
||||
DrawChart drawing = new DrawChart(); |
|
||||
drawing.drawByOrder(scoreList); |
|
||||
} |
|
||||
} |
|
||||
|
@ -1,45 +0,0 @@ |
|||||
<?xml version="1.0" encoding="UTF-8"?> |
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" |
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|
||||
<modelVersion>4.0.0</modelVersion> |
|
||||
|
|
||||
<groupId>com.crawler</groupId> |
|
||||
<artifactId>movie-crawler</artifactId> |
|
||||
<version>1.0</version> |
|
||||
|
|
||||
<properties> |
|
||||
<maven.compiler.source>8</maven.compiler.source> |
|
||||
<maven.compiler.target>8</maven.compiler.target> |
|
||||
</properties> |
|
||||
|
|
||||
<dependencies> |
|
||||
<!-- Jsoup:网页爬虫+HTML解析 --> |
|
||||
<dependency> |
|
||||
<groupId>org.jsoup</groupId> |
|
||||
<artifactId>jsoup</artifactId> |
|
||||
<version>1.15.4</version> |
|
||||
</dependency> |
|
||||
|
|
||||
<!-- XChart:生成图表(简单易用) --> |
|
||||
<dependency> |
|
||||
<groupId>org.knowm.xchart</groupId> |
|
||||
<artifactId>xchart</artifactId> |
|
||||
<version>3.8.4</version> |
|
||||
</dependency> |
|
||||
|
|
||||
<!-- OpenCSV:读写CSV文件 --> |
|
||||
<dependency> |
|
||||
<groupId>com.opencsv</groupId> |
|
||||
<artifactId>opencsv</artifactId> |
|
||||
<version>5.6</version> |
|
||||
</dependency> |
|
||||
|
|
||||
<!-- MySQL驱动(可选,存数据库) --> |
|
||||
<dependency> |
|
||||
<groupId>mysql</groupId> |
|
||||
<artifactId>mysql-connector-java</artifactId> |
|
||||
<version>8.0.33</version> |
|
||||
</dependency> |
|
||||
</dependencies> |
|
||||
</project> |
|
||||
|
Before Width: | Height: | Size: 84 KiB |
|
Before Width: | Height: | Size: 25 KiB |
@ -1,78 +0,0 @@ |
|||||
package java01; |
|
||||
import java.util.Scanner; |
|
||||
|
|
||||
/** |
|
||||
* 温度转换器示例程序(Java 版) |
|
||||
* 支持摄氏度(C)与华氏度(F)之间互转 |
|
||||
* 与你提供的 Python 程序功能、逻辑完全一致 |
|
||||
*/ |
|
||||
public class TemperatureConverter { |
|
||||
|
|
||||
/** |
|
||||
* 将摄氏度转换为华氏度 |
|
||||
* @param c 摄氏温度 |
|
||||
* @return 对应的华氏温度 |
|
||||
*/ |
|
||||
public static double celsiusToFahrenheit(double c) { |
|
||||
return c * 9.0 / 5.0 + 32.0; |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* 将华氏度转换为摄氏度 |
|
||||
* @param f 华氏温度 |
|
||||
* @return 对应的摄氏温度 |
|
||||
*/ |
|
||||
public static double fahrenheitToCelsius(double f) { |
|
||||
return (f - 32.0) * 5.0 / 9.0; |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* 主逻辑:读取输入、解析、判断单位、转换并输出 |
|
||||
*/ |
|
||||
public static void main(String[] args) { |
|
||||
Scanner scanner = new Scanner(System.in); |
|
||||
|
|
||||
// 提示用户输入,格式示例:"36.6 C" 或 "97 F"
|
|
||||
System.out.print("请输入要转换的温度与单位(例如 36.6 C 或 97 F):"); |
|
||||
String s = scanner.nextLine().trim(); |
|
||||
|
|
||||
// 输入为空
|
|
||||
if (s.isEmpty()) { |
|
||||
System.out.println("输入为空,程序退出。"); |
|
||||
scanner.close(); |
|
||||
return; |
|
||||
} |
|
||||
|
|
||||
// 按空格拆分
|
|
||||
String[] parts = s.split("\\s+"); |
|
||||
double value; |
|
||||
String unit; |
|
||||
|
|
||||
try { |
|
||||
// 解析数值与单位
|
|
||||
value = Double.parseDouble(parts[0]); |
|
||||
if (parts.length > 1) { |
|
||||
unit = parts[1].toUpperCase(); |
|
||||
} else { |
|
||||
unit = "C"; // 默认单位 C
|
|
||||
} |
|
||||
} catch (Exception e) { |
|
||||
System.out.println("输入解析失败,请按示例输入数值与单位,例如:36.6 C"); |
|
||||
scanner.close(); |
|
||||
return; |
|
||||
} |
|
||||
|
|
||||
// 根据单位转换
|
|
||||
if (unit.startsWith("C")) { |
|
||||
double f = celsiusToFahrenheit(value); |
|
||||
System.out.printf("%.2f °C = %.2f °F%n", value, f); |
|
||||
} else if (unit.startsWith("F")) { |
|
||||
double c = fahrenheitToCelsius(value); |
|
||||
System.out.printf("%.2f °F = %.2f °C%n", value, c); |
|
||||
} else { |
|
||||
System.out.println("未知单位,请使用 C 或 F。"); |
|
||||
} |
|
||||
|
|
||||
scanner.close(); |
|
||||
} |
|
||||
} |
|
||||
@ -1,11 +0,0 @@ |
|||||
# 温度转换程序作业提交 |
|
||||
## 作业基础信息 |
|
||||
- **项目名称**:TemperatureConverter |
|
||||
- **开发环境**:Eclipse IDE + Java JDK 17 |
|
||||
- **核心功能**:实现摄氏度(C)与华氏度(F)双向转换,完全等效于原Python程序逻辑 |
|
||||
|
|
||||
## 编译与运行说明 |
|
||||
### Eclipse 运行 |
|
||||
1. 右键点击 `src/java01/TemperatureConverter.java` 文件; |
|
||||
2. 选择 `Run As` → `Java Application`; |
|
||||
3. 在底部 Console 面板输入格式:`数值 单位`(例:36.6 C),按回车查看结果。 |
|
||||
|
Before Width: | Height: | Size: 360 KiB |
@ -1 +0,0 @@ |
|||||
本次作业我将 Python 温度转换程序移植为等效 Java 程序。我首先向 AI 提供了完整的 Python 源码,要求生成功能、逻辑完全一致的 Java 代码,并保留注释与交互逻辑。在实现过程中,AI 帮助我解决了包名不匹配、方法名拼写错误、语法分号缺失等问题,并指导我在 Eclipse 中进行调试和运行。最终,程序成功实现了摄氏与华氏的互转,并通过了测试用例,顺利完成作业。 |
|
||||
@ -1,36 +0,0 @@ |
|||||
package java01; |
|
||||
public class DataCleaner { |
|
||||
public static void main(String[] args) { |
|
||||
int[] sensorData = {85, -5, 92, 0, 105, 999, 88, 76}; |
|
||||
int validSum = 0; // 有效数据总和
|
|
||||
int validCount = 0; // 有效数据个数
|
|
||||
|
|
||||
// 遍历数组
|
|
||||
for (int data : sensorData) { |
|
||||
// 规则3:遇到999,致命错误,终止循环
|
|
||||
if (data == 999) { |
|
||||
System.out.println("致命错误:传感器掉线,终止处理"); |
|
||||
break; |
|
||||
} |
|
||||
// 规则1:数据在1~100之间,计入有效数据
|
|
||||
if (data >= 1 && data <= 100) { |
|
||||
validSum += data; |
|
||||
validCount++; |
|
||||
} |
|
||||
// 规则2:无效数据,跳过并打印警告
|
|
||||
else if (data <= 0 || data > 100) { |
|
||||
System.out.println("警告:发现越界数据[" + data + "],已跳过"); |
|
||||
continue; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
// 规则4:最终输出平均值或无有效数据
|
|
||||
if (validCount > 0) { |
|
||||
// 注意:用(double)强制转换避免整数除法陷阱
|
|
||||
double average = (double) validSum / validCount; |
|
||||
System.out.println("有效数据平均值:" + average); |
|
||||
} else { |
|
||||
System.out.println("无有效数据"); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
Before Width: | Height: | Size: 360 KiB |
@ -1,76 +0,0 @@ |
|||||
package java01; |
|
||||
|
|
||||
public class BankAccount { |
|
||||
// 1. 私有成员变量(封装核心)
|
|
||||
private String accountNumber; // 账号(不可修改)
|
|
||||
private String ownerName; // 户主姓名(可修改)
|
|
||||
private double balance; // 余额(只能通过存取款修改)
|
|
||||
|
|
||||
// 2. 构造方法:创建账户时必须传入账号和户主姓名,余额初始为0
|
|
||||
public BankAccount(String accountNumber, String ownerName) { |
|
||||
this.accountNumber = accountNumber; |
|
||||
this.ownerName = ownerName; |
|
||||
this.balance = 0.0; |
|
||||
} |
|
||||
|
|
||||
// 3. Getter方法(对外提供查询)
|
|
||||
// 账号只有getter,没有setter,保证不可修改
|
|
||||
public String getAccountNumber() { |
|
||||
return accountNumber; |
|
||||
} |
|
||||
|
|
||||
public String getOwnerName() { |
|
||||
return ownerName; |
|
||||
} |
|
||||
|
|
||||
public double getBalance() { |
|
||||
return balance; |
|
||||
} |
|
||||
|
|
||||
// 4. Setter方法:只有户主姓名可以修改
|
|
||||
public void setOwnerName(String ownerName) { |
|
||||
this.ownerName = ownerName; |
|
||||
} |
|
||||
|
|
||||
// 5. 存款方法 deposit
|
|
||||
public void deposit(double amount) { |
|
||||
if (amount > 0) { |
|
||||
balance += amount; |
|
||||
System.out.println("存款成功!当前余额:" + balance); |
|
||||
} else { |
|
||||
System.out.println("存款失败!存款金额必须大于0。"); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
// 6. 取款方法 withdraw
|
|
||||
public void withdraw(double amount) { |
|
||||
if (amount > 0 && amount <= balance) { |
|
||||
balance -= amount; |
|
||||
System.out.println("取款成功!当前余额:" + balance); |
|
||||
} else if (amount <= 0) { |
|
||||
System.out.println("取款失败!取款金额必须大于0。"); |
|
||||
} else { |
|
||||
System.out.println("取款失败!余额不足。"); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
// 测试主方法(可选,用来验证功能)
|
|
||||
public static void main(String[] args) { |
|
||||
// 创建一个账户
|
|
||||
BankAccount account = new BankAccount("622202123456789", "张三"); |
|
||||
|
|
||||
// 测试查询
|
|
||||
System.out.println("账号:" + account.getAccountNumber()); |
|
||||
System.out.println("户主:" + account.getOwnerName()); |
|
||||
System.out.println("初始余额:" + account.getBalance()); |
|
||||
|
|
||||
// 测试存款
|
|
||||
account.deposit(500); |
|
||||
account.deposit(-100); // 测试错误存款
|
|
||||
|
|
||||
// 测试取款
|
|
||||
account.withdraw(200); |
|
||||
account.withdraw(400); // 测试余额不足
|
|
||||
account.withdraw(-50); // 测试错误取款
|
|
||||
} |
|
||||
} |
|
||||
@ -1,34 +0,0 @@ |
|||||
package java01; |
|
||||
|
|
||||
public class BankAccountTest { |
|
||||
public static void main(String[] args) { |
|
||||
System.out.println("========== 1. 测试账户创建 =========="); |
|
||||
BankAccount account = new BankAccount("622202123456789", "张三"); |
|
||||
System.out.println("账号:" + account.getAccountNumber()); |
|
||||
System.out.println("户主:" + account.getOwnerName()); |
|
||||
System.out.println("初始余额:" + account.getBalance()); |
|
||||
System.out.println(); |
|
||||
|
|
||||
System.out.println("========== 2. 测试修改户主姓名 =========="); |
|
||||
account.setOwnerName("张三丰"); |
|
||||
System.out.println("修改后户主:" + account.getOwnerName()); |
|
||||
System.out.println(); |
|
||||
|
|
||||
System.out.println("========== 3. 测试存款操作 =========="); |
|
||||
account.deposit(1000); |
|
||||
account.deposit(0); |
|
||||
account.deposit(-500); |
|
||||
System.out.println(); |
|
||||
|
|
||||
System.out.println("========== 4. 测试取款操作 =========="); |
|
||||
account.withdraw(300); |
|
||||
account.withdraw(800); |
|
||||
account.withdraw(0); |
|
||||
account.withdraw(-200); |
|
||||
System.out.println(); |
|
||||
|
|
||||
System.out.println("========== 5. 最终状态验证 =========="); |
|
||||
System.out.println("最终户主:" + account.getOwnerName()); |
|
||||
System.out.println("最终余额:" + account.getBalance()); |
|
||||
} |
|
||||
} |
|
||||
|
Before Width: | Height: | Size: 85 KiB |
@ -1,12 +0,0 @@ |
|||||
public class Circle extends Shape { |
|
||||
private double radius; |
|
||||
|
|
||||
public Circle(double radius) { |
|
||||
this.radius = radius; |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public double getArea() { |
|
||||
return Math.PI * radius * radius; |
|
||||
} |
|
||||
} |
|
||||
@ -1,155 +0,0 @@ |
|||||
|
|
||||
# 图形面积计算器重构 实验报告 |
|
||||
|
|
||||
## 一、实验目的 |
|
||||
1. 掌握 Java 抽象类与抽象方法的使用。 |
|
||||
2. 理解面向对象的继承、多态特性。 |
|
||||
3. 实现不同图形类的统一处理,提高代码扩展性。 |
|
||||
|
|
||||
## 二、实验任务 |
|
||||
1. 设计抽象类 `Shape`,包含抽象方法 `getArea()`。 |
|
||||
2. 让 `Circle`、`Rectangle`、`Triangle` 继承 `Shape` 并实现面积计算。 |
|
||||
3. 编写工具类 `ShapeUtil`,提供统一打印面积的方法。 |
|
||||
4. 绘制 UML 类图。 |
|
||||
5. 完成代码测试与实验报告。 |
|
||||
|
|
||||
## 三、实验环境 |
|
||||
- 开发工具:Eclipse |
|
||||
- 编程语言:Java |
|
||||
- 版本控制:Git |
|
||||
|
|
||||
## 四、项目结构 |
|
||||
``` |
|
||||
src/ |
|
||||
├── Shape.java // 抽象类 |
|
||||
├── Circle.java // 圆形类 |
|
||||
├── Rectangle.java // 矩形类 |
|
||||
├── Triangle.java // 三角形类 |
|
||||
├── ShapeUtil.java // 工具类 |
|
||||
└── ShapeTest.java // 测试主类 |
|
||||
``` |
|
||||
|
|
||||
## 五、核心代码实现 |
|
||||
|
|
||||
### 1. 抽象类 Shape |
|
||||
```java |
|
||||
public abstract class Shape { |
|
||||
// 抽象方法:计算面积,子类必须实现 |
|
||||
public abstract double getArea(); |
|
||||
} |
|
||||
``` |
|
||||
|
|
||||
### 2. 圆形类 Circle |
|
||||
```java |
|
||||
public class Circle extends Shape { |
|
||||
// 私有属性:半径 |
|
||||
private double radius; |
|
||||
|
|
||||
// 构造方法:初始化半径 |
|
||||
public Circle(double radius) { |
|
||||
this.radius = radius; |
|
||||
} |
|
||||
|
|
||||
// 重写父类抽象方法:计算圆面积 πr² |
|
||||
@Override |
|
||||
public double getArea() { |
|
||||
return Math.PI * radius * radius; |
|
||||
} |
|
||||
} |
|
||||
``` |
|
||||
|
|
||||
### 3. 矩形类 Rectangle |
|
||||
```java |
|
||||
public class Rectangle extends Shape { |
|
||||
// 私有属性:宽、高 |
|
||||
private double width; |
|
||||
private double height; |
|
||||
|
|
||||
// 构造方法:初始化宽和高 |
|
||||
public Rectangle(double width, double height) { |
|
||||
this.width = width; |
|
||||
this.height = height; |
|
||||
} |
|
||||
|
|
||||
// 重写父类抽象方法:计算矩形面积 宽×高 |
|
||||
@Override |
|
||||
public double getArea() { |
|
||||
return width * height; |
|
||||
} |
|
||||
} |
|
||||
``` |
|
||||
|
|
||||
### 4. 三角形类 Triangle |
|
||||
```java |
|
||||
public class Triangle extends Shape { |
|
||||
// 私有属性:底、高 |
|
||||
private double base; |
|
||||
private double height; |
|
||||
|
|
||||
// 构造方法:初始化底和高 |
|
||||
public Triangle(double base, double height) { |
|
||||
this.base = base; |
|
||||
this.height = height; |
|
||||
} |
|
||||
|
|
||||
// 重写父类抽象方法:计算三角形面积 底×高/2 |
|
||||
@Override |
|
||||
public double getArea() { |
|
||||
return base * height / 2; |
|
||||
} |
|
||||
} |
|
||||
``` |
|
||||
|
|
||||
### 5. 工具类 ShapeUtil |
|
||||
```java |
|
||||
public class ShapeUtil { |
|
||||
// 统一打印任意图形的面积(多态实现) |
|
||||
public static void printArea(Shape shape) { |
|
||||
System.out.println("图形面积:" + shape.getArea()); |
|
||||
} |
|
||||
} |
|
||||
``` |
|
||||
|
|
||||
### 6. 测试主类 ShapeTest |
|
||||
```java |
|
||||
public class ShapeTest { |
|
||||
public static void main(String[] args) { |
|
||||
// 1. 创建不同图形对象(向上转型为Shape父类) |
|
||||
Shape circle = new Circle(5); |
|
||||
Shape rectangle = new Rectangle(4, 6); |
|
||||
Shape triangle = new Triangle(3, 4); |
|
||||
|
|
||||
// 2. 统一调用工具类打印面积(多态自动调用子类方法) |
|
||||
System.out.print("圆形:"); |
|
||||
ShapeUtil.printArea(circle); |
|
||||
|
|
||||
System.out.print("矩形:"); |
|
||||
ShapeUtil.printArea(rectangle); |
|
||||
|
|
||||
System.out.print("三角形:"); |
|
||||
ShapeUtil.printArea(triangle); |
|
||||
} |
|
||||
} |
|
||||
``` |
|
||||
|
|
||||
## 六、UML 类图 |
|
||||
见提交作业 |
|
||||
|
|
||||
## 七、运行结果 |
|
||||
``` |
|
||||
圆形:图形面积:78.53981633974483 |
|
||||
矩形:图形面积:24.0 |
|
||||
三角形:图形面积:6.0 |
|
||||
``` |
|
||||
|
|
||||
## 八、实验总结 |
|
||||
1. **抽象类的作用**:`Shape` 作为抽象父类,定义了所有图形的统一行为规范,强制子类必须实现 `getArea()` 方法,保证了代码的一致性。 |
|
||||
2. **多态的优势**:通过父类引用指向子类对象,`ShapeUtil` 可以统一处理所有图形,无需为每个图形单独写打印方法,大幅提升了代码的扩展性。 |
|
||||
3. **面向对象思想**:本次实验完整实践了封装(私有属性+构造方法)、继承(子类继承父类)、多态(方法重写+父类引用)三大面向对象核心特性。 |
|
||||
4. **代码扩展性**:后续新增图形(如正方形、梯形),只需继承 `Shape` 并实现 `getArea()`,无需修改 `ShapeUtil` 等已有代码,符合开闭原则。 |
|
||||
|
|
||||
## 九、AI 使用情况 |
|
||||
1. 使用 AI 辅助理解抽象类、多态的核心概念,梳理代码结构。 |
|
||||
2. AI 提供了代码规范优化、类图绘制、实验报告撰写的指导。 |
|
||||
3. 自主完成代码编写、调试、测试,以及实验报告的最终整理。 |
|
||||
``` |
|
||||
@ -1,14 +0,0 @@ |
|||||
public class Rectangle extends Shape { |
|
||||
private double width; |
|
||||
private double height; |
|
||||
|
|
||||
public Rectangle(double width, double height) { |
|
||||
this.width = width; |
|
||||
this.height = height; |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public double getArea() { |
|
||||
return width * height; |
|
||||
} |
|
||||
} |
|
||||
@ -1,3 +0,0 @@ |
|||||
public abstract class Shape { |
|
||||
public abstract double getArea(); |
|
||||
} |
|
||||
@ -1,18 +0,0 @@ |
|||||
public class ShapeTest { |
|
||||
public static void main(String[] args) { |
|
||||
// 创建各个图形对象
|
|
||||
Shape c = new Circle(5); |
|
||||
Shape r = new Rectangle(4,6); |
|
||||
Shape t = new Triangle(3,4); |
|
||||
|
|
||||
//统一调用工具打印面积
|
|
||||
System.out.print("圆形:"); |
|
||||
ShapeUtil.printArea(c); |
|
||||
|
|
||||
System.out.print("矩形:"); |
|
||||
ShapeUtil.printArea(r); |
|
||||
|
|
||||
System.out.print("三角形:"); |
|
||||
ShapeUtil.printArea(t); |
|
||||
} |
|
||||
} |
|
||||
@ -1,5 +0,0 @@ |
|||||
public class ShapeUtil { |
|
||||
public static void printArea(Shape shape){ |
|
||||
System.out.println("图形面积:" + shape.getArea()); |
|
||||
} |
|
||||
} |
|
||||
@ -1,14 +0,0 @@ |
|||||
public class Triangle extends Shape { |
|
||||
private double base; |
|
||||
private double height; |
|
||||
|
|
||||
public Triangle(double base, double height) { |
|
||||
this.base = base; |
|
||||
this.height = height; |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public double getArea() { |
|
||||
return base * height / 2; |
|
||||
} |
|
||||
} |
|
||||
|
Before Width: | Height: | Size: 20 KiB |
|
Before Width: | Height: | Size: 238 KiB |
@ -1,6 +0,0 @@ |
|||||
public class Circle extends Shape { |
|
||||
@Override |
|
||||
public void draw() { |
|
||||
System.out.println("绘制一个圆形"); |
|
||||
} |
|
||||
} |
|
||||
@ -1,6 +0,0 @@ |
|||||
public class Rectangle extends Shape { |
|
||||
@Override |
|
||||
public void draw() { |
|
||||
System.out.println("绘制一个矩形"); |
|
||||
} |
|
||||
} |
|
||||
@ -1,5 +0,0 @@ |
|||||
public class Shape { |
|
||||
public void draw() { |
|
||||
System.out.println("绘制一个形状"); |
|
||||
} |
|
||||
} |
|
||||
@ -1,15 +0,0 @@ |
|||||
public class TestShape { |
|
||||
|
|
||||
public static void drawShape(Shape s) { |
|
||||
s.draw(); |
|
||||
} |
|
||||
|
|
||||
public static void main(String[] args) { |
|
||||
Shape shape = new Shape(); |
|
||||
Circle circle = new Circle(); |
|
||||
Rectangle rectangle = new Rectangle(); |
|
||||
drawShape(shape); |
|
||||
drawShape(circle); |
|
||||
drawShape(rectangle); |
|
||||
} |
|
||||
} |
|
||||
@ -1,6 +0,0 @@ |
|||||
public class Bike extends Vehicle { |
|
||||
@Override |
|
||||
public void run() { |
|
||||
System.out.println("自行车在慢慢骑行"); |
|
||||
} |
|
||||
} |
|
||||
@ -1,6 +0,0 @@ |
|||||
public class Car extends Vehicle { |
|
||||
@Override |
|
||||
public void run() { |
|
||||
System.out.println("小汽车在公路上行驶"); |
|
||||
} |
|
||||
} |
|
||||
@ -1,14 +0,0 @@ |
|||||
public class TestVehicle { |
|
||||
public static void main(String[] args) { |
|
||||
// 创建Vehicle数组,存放不同车辆
|
|
||||
Vehicle[] vehicles = new Vehicle[3]; |
|
||||
vehicles[0] = new Car(); |
|
||||
vehicles[1] = new Bike(); |
|
||||
vehicles[2] = new Truck(); |
|
||||
|
|
||||
// 遍历数组,调用run()
|
|
||||
for (Vehicle v : vehicles) { |
|
||||
v.run(); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,6 +0,0 @@ |
|||||
public class Truck extends Vehicle { |
|
||||
@Override |
|
||||
public void run() { |
|
||||
System.out.println("大卡车在载货行驶"); |
|
||||
} |
|
||||
} |
|
||||
@ -1,3 +0,0 @@ |
|||||
public abstract class Vehicle { |
|
||||
public abstract void run(); |
|
||||
} |
|
||||
|
Before Width: | Height: | Size: 13 KiB |
@ -1,46 +0,0 @@ |
|||||
package java01; |
|
||||
abstract class Animal { |
|
||||
public abstract void makeSound(); |
|
||||
} |
|
||||
interface Swimmable { |
|
||||
void swim(); |
|
||||
} |
|
||||
class Dog extends Animal implements Swimmable { |
|
||||
@Override |
|
||||
public void makeSound() { |
|
||||
System.out.println("狗:汪汪汪"); |
|
||||
} |
|
||||
|
|
||||
@Override |
|
||||
public void swim() { |
|
||||
System.out.println("狗会游泳"); |
|
||||
} |
|
||||
} |
|
||||
class Cat extends Animal { |
|
||||
@Override |
|
||||
public void makeSound() { |
|
||||
System.out.println("猫:喵喵喵"); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public class AnimalSystem { |
|
||||
public static void main(String[] args) { |
|
||||
Animal animal1 = new Dog(); |
|
||||
Animal animal2 = new Cat(); |
|
||||
animal1.makeSound(); |
|
||||
animal2.makeSound(); |
|
||||
|
|
||||
System.out.println("----------"); |
|
||||
if (animal1 instanceof Swimmable) { |
|
||||
Swimmable swimmer = (Swimmable) animal1; |
|
||||
swimmer.swim(); |
|
||||
} |
|
||||
|
|
||||
if (animal2 instanceof Swimmable) { |
|
||||
Swimmable swimmer = (Swimmable) animal2; |
|
||||
swimmer.swim(); |
|
||||
} else { |
|
||||
System.out.println("猫不会游泳"); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
Before Width: | Height: | Size: 24 KiB |