56 changed files with 748 additions and 3 deletions
@ -1,2 +0,0 @@ |
|||||
# java |
|
||||
|
|
||||
@ -0,0 +1,7 @@ |
|||||
|
AI协助记录: |
||||
|
|
||||
|
在完成这次Java作业过程中,我使用了AI助手。首先,我请AI将Python温度转换程序改写为Java版本,AI提供了完整的代码和中文注释。 |
||||
|
|
||||
|
在编译运行过程中遇到输入格式错误的问题,AI一步步指导我修改代码,采用分步输入温度和单位的方式,最终程序成功运行,可以正确转换摄氏度和华氏度。 |
||||
|
|
||||
|
AI还帮我创建了README.md说明文档和output.txt运行结果文件,指导我完成所有作业要求。 |
||||
@ -0,0 +1,22 @@ |
|||||
|
\# 温度转换程序 |
||||
|
|
||||
|
|
||||
|
|
||||
|
\## 编译方法 |
||||
|
|
||||
|
javac TemperatureConverter.java |
||||
|
|
||||
|
|
||||
|
|
||||
|
\## 运行方法 |
||||
|
|
||||
|
java TemperatureConverter |
||||
|
|
||||
|
|
||||
|
|
||||
|
\## 运行示例 |
||||
|
|
||||
|
输入:36.6 C |
||||
|
|
||||
|
输出:36.6℃ = 97.88℉ |
||||
|
|
||||
@ -0,0 +1,23 @@ |
|||||
|
import java.util.Scanner; |
||||
|
|
||||
|
public class TemperatureConverter { |
||||
|
public static void main(String[] args) { |
||||
|
Scanner scanner = new Scanner(System.in); |
||||
|
|
||||
|
System.out.println("请输入温度值:"); |
||||
|
double value = scanner.nextDouble(); |
||||
|
|
||||
|
System.out.println("请输入单位(C表示摄氏度,F表示华氏度):"); |
||||
|
String unit = scanner.next().toUpperCase(); |
||||
|
|
||||
|
if (unit.equals("C")) { |
||||
|
double f = value * 9.0 / 5.0 + 32.0; |
||||
|
System.out.println(value + "℃ = " + f + "℉"); |
||||
|
} else if (unit.equals("F")) { |
||||
|
double c = (value - 32.0) * 5.0 / 9.0; |
||||
|
System.out.println(value + "℉ = " + c + "℃"); |
||||
|
} |
||||
|
|
||||
|
scanner.close(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,7 @@ |
|||||
|
AI协助记录: |
||||
|
|
||||
|
本次作业使用AI助手将Python温度转换程序改写为Java版本。AI提供了完整的Java代码和中文注释。 |
||||
|
|
||||
|
在运行过程中遇到输入格式问题,AI指导修改代码,采用分步输入的方式,解决了特殊符号解析问题。程序最终成功运行,能正确转换摄氏度和华氏度。 |
||||
|
|
||||
|
AI还协助创建了README.md说明文档和output.txt运行结果文件,帮助完成所有作业要求。 |
||||
@ -0,0 +1,29 @@ |
|||||
|
public class StudentTest {public static void main(String[] args) { |
||||
|
Student stu1 = new Student(); |
||||
|
stu1.studentId = "001"; |
||||
|
stu1.name = "Wang"; |
||||
|
stu1.score = 85.5; |
||||
|
|
||||
|
Student stu2 = stu1; |
||||
|
|
||||
|
stu1.study(); |
||||
|
stu2.study(); |
||||
|
|
||||
|
System.out.println("stu1 的名字: " + stu1.name); |
||||
|
System.out.println("stu2 的名字: " + stu2.name); |
||||
|
|
||||
|
stu2.name = "Li"; |
||||
|
|
||||
|
System.out.println("修改后 stu1 的名字: " + stu1.name); |
||||
|
System.out.println("修改后 stu2 的名字: " + stu2.name); |
||||
|
} |
||||
|
} |
||||
|
class Student {String studentId; |
||||
|
String name; |
||||
|
double score; |
||||
|
|
||||
|
public void study() { |
||||
|
System.out.println(name + " 正在学习"); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,4 @@ |
|||||
|
package PACKAGE_NAME; |
||||
|
|
||||
|
public class AnalyzeCommand { |
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
import java.util.Date; |
||||
|
|
||||
|
public class Article { |
||||
|
private String title; |
||||
|
private String content; |
||||
|
private String url; |
||||
|
private String author; // 新增
|
||||
|
private Date publishDate; // 新增
|
||||
|
|
||||
|
// 构造器
|
||||
|
public Article(String title, String content, String url, String author, Date publishDate) { |
||||
|
this.title = title; |
||||
|
this.content = content; |
||||
|
this.url = url; |
||||
|
this.author = author; |
||||
|
this.publishDate = publishDate; |
||||
|
} |
||||
|
|
||||
|
// Getters and Setters
|
||||
|
public String getAuthor() { return author; } |
||||
|
public void setAuthor(String author) { this.author = author; } |
||||
|
|
||||
|
public Date getPublishDate() { return publishDate; } |
||||
|
public void setPublishDate(Date publishDate) { this.publishDate = publishDate; } |
||||
|
|
||||
|
// 原有字段的 getter/setter...
|
||||
|
} |
||||
@ -0,0 +1,4 @@ |
|||||
|
package PACKAGE_NAME; |
||||
|
|
||||
|
public class articlerepository { |
||||
|
} |
||||
@ -0,0 +1,27 @@ |
|||||
|
import java.util.Date; |
||||
|
|
||||
|
public class article { |
||||
|
private String title; |
||||
|
private String content; |
||||
|
private String url; |
||||
|
private String author; // 新增
|
||||
|
private Date publishDate; // 新增
|
||||
|
|
||||
|
// 构造器
|
||||
|
public article(String title, String content, String url, String author, Date publishDate) { |
||||
|
this.title = title; |
||||
|
this.content = content; |
||||
|
this.url = url; |
||||
|
this.author = author; |
||||
|
this.publishDate = publishDate; |
||||
|
} |
||||
|
|
||||
|
// Getters and Setters
|
||||
|
public String getAuthor() { return author; } |
||||
|
public void setAuthor(String author) { this.author = author; } |
||||
|
|
||||
|
public Date getPublishDate() { return publishDate; } |
||||
|
public void setPublishDate(Date publishDate) { this.publishDate = publishDate; } |
||||
|
|
||||
|
// 原有字段的 getter/setter...
|
||||
|
} |
||||
@ -0,0 +1,4 @@ |
|||||
|
package PACKAGE_NAME; |
||||
|
|
||||
|
public class ArticleRepository { |
||||
|
} |
||||
@ -0,0 +1,4 @@ |
|||||
|
package PACKAGE_NAME; |
||||
|
|
||||
|
public class BlogStrategy { |
||||
|
} |
||||
@ -0,0 +1,4 @@ |
|||||
|
package PACKAGE_NAME; |
||||
|
|
||||
|
public class Command { |
||||
|
} |
||||
@ -0,0 +1,4 @@ |
|||||
|
package PACKAGE_NAME; |
||||
|
|
||||
|
public class CrawlCommand { |
||||
|
} |
||||
@ -0,0 +1,4 @@ |
|||||
|
package PACKAGE_NAME; |
||||
|
|
||||
|
public class CrawlStrategy { |
||||
|
} |
||||
@ -0,0 +1,4 @@ |
|||||
|
package PACKAGE_NAME; |
||||
|
|
||||
|
public class CrawlerException { |
||||
|
} |
||||
@ -0,0 +1,4 @@ |
|||||
|
package PACKAGE_NAME; |
||||
|
|
||||
|
public class HnuNewsStrategy { |
||||
|
} |
||||
@ -0,0 +1,4 @@ |
|||||
|
package PACKAGE_NAME; |
||||
|
|
||||
|
public class Main { |
||||
|
} |
||||
@ -0,0 +1,4 @@ |
|||||
|
package PACKAGE_NAME; |
||||
|
|
||||
|
public class NetworkException { |
||||
|
} |
||||
@ -0,0 +1,4 @@ |
|||||
|
package PACKAGE_NAME; |
||||
|
|
||||
|
public class NewsStrategy { |
||||
|
} |
||||
@ -0,0 +1,4 @@ |
|||||
|
package PACKAGE_NAME; |
||||
|
|
||||
|
public class ParseException { |
||||
|
} |
||||
@ -0,0 +1,32 @@ |
|||||
|
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) { |
||||
|
if (data == 999) { |
||||
|
System.out.println("致命错误:传感器掉线,终止处理"); |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
if (data <= 0 || data > 100) { |
||||
|
System.out.println("警告:发现越界数据 " + data + ",已跳过"); |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
// 正常范围:1 <= data <= 100
|
||||
|
validSum += data; |
||||
|
validCount++; |
||||
|
} |
||||
|
|
||||
|
// 最终输出
|
||||
|
if (validCount > 0) { |
||||
|
double average = (double) validSum / validCount; |
||||
|
System.out.println("有效数据的平均值:" + average); |
||||
|
} else { |
||||
|
System.out.println("无有效数据"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
@ -0,0 +1,56 @@ |
|||||
|
public class BankAccount { |
||||
|
private final String accountNumber; |
||||
|
private String ownerName; |
||||
|
private double balance; |
||||
|
|
||||
|
public BankAccount(){ |
||||
|
this("000000","未命名"); |
||||
|
} |
||||
|
|
||||
|
public BankAccount(String accountNumber, String ownerName) { |
||||
|
this.accountNumber = accountNumber; |
||||
|
this.ownerName = ownerName; |
||||
|
this.balance=0.0; |
||||
|
} |
||||
|
|
||||
|
public void setOwnerName(String ownerName) { |
||||
|
this.ownerName = ownerName; |
||||
|
} |
||||
|
|
||||
|
public void deposit(double amount) { |
||||
|
if(amount>0){ |
||||
|
this.balance += amount; |
||||
|
System.out.println("存款成功,当前余额:" +this.balance); |
||||
|
}else{ |
||||
|
System.out.println("存款必须大于0"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void withdraw(double amount) { |
||||
|
if(amount<=0){ |
||||
|
System.out.println("取款金额必须大于0"); |
||||
|
}else if(amount>this.balance){ |
||||
|
System.out.println("金额不足,当前余额:"+this.balance); |
||||
|
}else { |
||||
|
this.balance -= amount; |
||||
|
System.out.println("取款成功!当前余额:" + this.balance); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 查询余额的方法
|
||||
|
public double getBalance() { |
||||
|
return this.balance; |
||||
|
} |
||||
|
|
||||
|
// 获取账户号的方法(因为账户号不可修改,只提供getter)
|
||||
|
public String getAccountNumber() { |
||||
|
return this.accountNumber; |
||||
|
} |
||||
|
|
||||
|
// 获取户主姓名的方法
|
||||
|
public String getOwnerName() { |
||||
|
return this.ownerName; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
@ -0,0 +1,82 @@ |
|||||
|
public class Employee { |
||||
|
private String id; |
||||
|
private String name; |
||||
|
private String department; |
||||
|
private double salary; |
||||
|
|
||||
|
// 全参构造方法
|
||||
|
public Employee(String id, String name, String department, double salary) { |
||||
|
this.id = id; |
||||
|
this.name = name; |
||||
|
this.department = department; |
||||
|
setSalary(salary); |
||||
|
} |
||||
|
|
||||
|
// getter和setter
|
||||
|
public String getId() { |
||||
|
return id; |
||||
|
} |
||||
|
|
||||
|
public void setId(String id) { |
||||
|
this.id = id; |
||||
|
} |
||||
|
|
||||
|
public String getName() { |
||||
|
return name; |
||||
|
} |
||||
|
|
||||
|
public void setName(String name) { |
||||
|
this.name = name; |
||||
|
} |
||||
|
|
||||
|
public String getDepartment() { |
||||
|
return department; |
||||
|
} |
||||
|
|
||||
|
public void setDepartment(String department) { |
||||
|
this.department = department; |
||||
|
} |
||||
|
|
||||
|
public double getSalary() { |
||||
|
return salary; |
||||
|
} |
||||
|
|
||||
|
public void setSalary(double salary) { |
||||
|
if (salary >= 2000) { |
||||
|
this.salary = salary; |
||||
|
} else { |
||||
|
System.out.println("工资不能低于2000!"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 涨薪方法
|
||||
|
public void raiseSalary(double percent) { |
||||
|
double newSalary = salary * (1 + percent / 100); |
||||
|
setSalary(newSalary); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 测试类
|
||||
|
class TestEmployee { |
||||
|
public static void main(String[] args) { |
||||
|
Employee emp1 = new Employee("1001", "张三", "技术部", 5000); |
||||
|
Employee emp2 = new Employee("1002", "李四", "销售部", 3000); |
||||
|
|
||||
|
System.out.println("=== 初始信息 ==="); |
||||
|
System.out.println(emp1.getName() + " - " + emp1.getDepartment() + " - 工资:" + emp1.getSalary()); |
||||
|
System.out.println(emp2.getName() + " - " + emp2.getDepartment() + " - 工资:" + emp2.getSalary()); |
||||
|
|
||||
|
System.out.println("\n=== 涨薪后 ==="); |
||||
|
emp1.raiseSalary(10); |
||||
|
System.out.println(emp1.getName() + "涨薪后工资:" + emp1.getSalary()); |
||||
|
|
||||
|
System.out.println("\n=== 修改部门 ==="); |
||||
|
emp2.setDepartment("市场部"); |
||||
|
System.out.println(emp2.getName() + "新部门:" + emp2.getDepartment()); |
||||
|
|
||||
|
System.out.println("\n=== 测试非法工资 ==="); |
||||
|
emp2.setSalary(1500); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
@ -0,0 +1,12 @@ |
|||||
|
public class Circle extends Shape { |
||||
|
private double radius; |
||||
|
|
||||
|
public Circle(double radius) { |
||||
|
this.radius = radius; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return Math.PI * radius * radius; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
public class Main { |
||||
|
public static void main(String[] args) { |
||||
|
Shape circle = new Circle(5); |
||||
|
Shape rectangle = new Rectangle(4, 6); |
||||
|
Shape triangle = new Triangle(3, 4, 5); |
||||
|
|
||||
|
ShapeUtil.printArea(circle); |
||||
|
ShapeUtil.printArea(rectangle); |
||||
|
ShapeUtil.printArea(triangle); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
public class Rectangle extends Shape { |
||||
|
private double width, height; |
||||
|
|
||||
|
public Rectangle(double width, double height) { |
||||
|
this.width = width; |
||||
|
this.height = height; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return width * height; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,3 @@ |
|||||
|
public abstract class Shape { |
||||
|
public abstract double getArea(); |
||||
|
} |
||||
@ -0,0 +1,5 @@ |
|||||
|
public class ShapeUtil { |
||||
|
public static void printArea(Shape shape) { |
||||
|
System.out.println("面积为: " + shape.getArea()); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,15 @@ |
|||||
|
public class Triangle extends Shape { |
||||
|
private double a, b, c; |
||||
|
|
||||
|
public Triangle(double a, double b, double c) { |
||||
|
this.a = a; |
||||
|
this.b = b; |
||||
|
this.c = c; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
double s = (a + b + c) / 2; |
||||
|
return Math.sqrt(s * (s - a) * (s - b) * (s - c)); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,6 @@ |
|||||
|
public class A_Circlew5 extends A_Graphic { |
||||
|
@Override |
||||
|
public void draw() { |
||||
|
System.out.println("绘制一个圆形"); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,5 @@ |
|||||
|
public class A_Graphic { |
||||
|
public void draw() { |
||||
|
System.out.println("绘制一个图形"); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,15 @@ |
|||||
|
public class A_Main1 {public static void drawGraphic(A_Graphic g) { |
||||
|
g.draw(); |
||||
|
} |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
A_Graphic graphic = new A_Graphic(); |
||||
|
A_Circlew5 circle = new A_Circlew5(); |
||||
|
A_Rectanglew5 rectangle = new A_Rectanglew5(); |
||||
|
|
||||
|
drawGraphic(graphic); |
||||
|
drawGraphic(circle); |
||||
|
drawGraphic(rectangle); |
||||
|
} |
||||
|
} |
||||
|
|
||||
@ -0,0 +1,6 @@ |
|||||
|
public class A_Rectanglew5 extends A_Graphic { |
||||
|
@Override |
||||
|
public void draw() { |
||||
|
System.out.println("绘制一个矩形"); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,6 @@ |
|||||
|
public class Bike extends Vehicle { |
||||
|
@Override |
||||
|
public void run() { |
||||
|
System.out.println("自行车在自行车道上骑行"); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,6 @@ |
|||||
|
public class Car extends Vehicle { |
||||
|
@Override |
||||
|
public void run() { |
||||
|
System.out.println("汽车在公路上行驶"); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,12 @@ |
|||||
|
public class Main2 { |
||||
|
public static void main(String[] args) { |
||||
|
Vehicle[] vehicles = new Vehicle[3]; |
||||
|
vehicles[0] = new Car(); |
||||
|
vehicles[1] = new Bike(); |
||||
|
vehicles[2] = new Truck(); |
||||
|
|
||||
|
for (Vehicle v : vehicles) { |
||||
|
v.run(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,6 @@ |
|||||
|
public class Truck extends Vehicle { |
||||
|
@Override |
||||
|
public void run() { |
||||
|
System.out.println("卡车在高速上运输货物"); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,3 @@ |
|||||
|
public abstract class Vehicle { |
||||
|
public abstract void run(); |
||||
|
} |
||||
@ -0,0 +1,32 @@ |
|||||
|
// 抽象类 Animal
|
||||
|
abstract class Animal { |
||||
|
public abstract void makeSound(); |
||||
|
} |
||||
|
|
||||
|
// 接口 Swimmable
|
||||
|
interface Swimmable { |
||||
|
void swim(); |
||||
|
} |
||||
|
|
||||
|
// Dog 类继承 Animal 并实现 Swimmable
|
||||
|
class Dog extends Animal implements Swimmable { |
||||
|
@Override |
||||
|
public void makeSound() { |
||||
|
System.out.println("狗叫:汪汪汪!"); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void swim() { |
||||
|
System.out.println("狗会游泳,正在狗刨..."); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Cat 类继承 Animal,不实现 Swimmable
|
||||
|
class Cat extends Animal { |
||||
|
@Override |
||||
|
public void makeSound() { |
||||
|
System.out.println("猫叫:喵喵喵~"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
@ -0,0 +1,21 @@ |
|||||
|
// 测试类
|
||||
|
public class AnimalTest { |
||||
|
public static void main(String[] args) { |
||||
|
// 多态调用
|
||||
|
Animal dog = new Dog(); |
||||
|
Animal cat = new Cat(); |
||||
|
|
||||
|
dog.makeSound(); |
||||
|
cat.makeSound(); |
||||
|
|
||||
|
// 判断 Dog 是否实现了 Swimmable 接口并调用
|
||||
|
if (dog instanceof Swimmable) { |
||||
|
((Swimmable) dog).swim(); |
||||
|
} |
||||
|
|
||||
|
// Cat 不能游泳
|
||||
|
if (!(cat instanceof Swimmable)) { |
||||
|
System.out.println("猫不会游泳"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
After Width: | Height: | Size: 511 KiB |
|
After Width: | Height: | Size: 71 KiB |
@ -0,0 +1,71 @@ |
|||||
|
import java.io.BufferedReader; |
||||
|
import java.io.FileReader; |
||||
|
import java.io.IOException; |
||||
|
import java.nio.file.Files; |
||||
|
import java.nio.file.Path; |
||||
|
import java.nio.file.Paths; |
||||
|
|
||||
|
public class ScoreCalculator { |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
String fileName = "java/w7/ scores"; |
||||
|
double average = calculateAverageScore(fileName); |
||||
|
|
||||
|
if (!Double.isNaN(average)) { |
||||
|
System.out.printf("平均分: %.2f%n", average); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 计算成绩文件的平均分 |
||||
|
* @param fileName 文件名 |
||||
|
* @return 平均分,如果出错返回 NaN |
||||
|
*/ |
||||
|
public static double calculateAverageScore(String fileName) { |
||||
|
int sum = 0; |
||||
|
int count = 0; |
||||
|
|
||||
|
// 1. 检查文件是否存在
|
||||
|
Path filePath = Paths.get(fileName); |
||||
|
if (!Files.exists(filePath)) { |
||||
|
System.err.println("错误: 文件 '" + fileName + "' 不存在!"); |
||||
|
return Double.NaN; |
||||
|
} |
||||
|
|
||||
|
// 2. 使用 try-with-resources 确保流自动关闭
|
||||
|
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) { |
||||
|
String line; |
||||
|
|
||||
|
while ((line = br.readLine()) != null) { |
||||
|
// 跳过空行
|
||||
|
if (line.trim().isEmpty()) { |
||||
|
continue; |
||||
|
} |
||||
|
|
||||
|
try { |
||||
|
int score = Integer.parseInt(line.trim()); |
||||
|
sum += score; |
||||
|
count++; |
||||
|
} catch (NumberFormatException e) { |
||||
|
// 数字格式错误,记录错误但继续处理其他行
|
||||
|
System.err.println("警告: 无效的数据格式 '" + line + "',已跳过此条记录"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 检查是否有有效数据
|
||||
|
if (count == 0) { |
||||
|
System.err.println("错误: 文件中没有有效的成绩数据!"); |
||||
|
return Double.NaN; |
||||
|
} |
||||
|
|
||||
|
return (double) sum / count; |
||||
|
|
||||
|
} catch (IOException e) { |
||||
|
// 读取错误
|
||||
|
System.err.println("错误: 读取文件 '" + fileName + "' 时发生异常!"); |
||||
|
System.err.println("详细信息: " + e.getMessage()); |
||||
|
return Double.NaN; |
||||
|
} |
||||
|
// 无需显式 close(),try-with-resources 会自动处理
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,5 @@ |
|||||
|
85 |
||||
|
90 |
||||
|
78 |
||||
|
92 |
||||
|
88 |
||||
@ -0,0 +1,5 @@ |
|||||
|
3.AI协同学习:泛型擦除后如何通过反射获取泛型信息? |
||||
|
答案:擦除后,运行时拿不到类型参数k、V的实际值。但可以通过反射获取字段、方法参数/返回值上声明的泛型信息(如List<String>中的String) |
||||
|
4.思考题:为什么Java泛型不支持基本类型? |
||||
|
根本原因:Java泛型通过类型擦除实现,编译后所有泛型参数都被替换为Object(或上界类型)。而基本类型(int、double等)不继承自 object,无法直接放入0bject容器中。 |
||||
|
解法:使用对应的包装类(Integer Double).依赖自动装箱/拆箱 |
||||
@ -0,0 +1,26 @@ |
|||||
|
import java.util.HashMap; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
public class Cache<K, V> { |
||||
|
private final Map<K, V> cache = new HashMap<>(); |
||||
|
|
||||
|
public void put(K key, V value) { |
||||
|
cache.put(key, value); |
||||
|
} |
||||
|
|
||||
|
public V get(K key) { |
||||
|
return cache.get(key); |
||||
|
} |
||||
|
|
||||
|
public boolean containsKey(K key) { |
||||
|
return cache.containsKey(key); |
||||
|
} |
||||
|
|
||||
|
public int size() { |
||||
|
return cache.size(); |
||||
|
} |
||||
|
|
||||
|
public void clear() { |
||||
|
cache.clear(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,23 @@ |
|||||
|
public class Pair<K, V> { |
||||
|
private K key; |
||||
|
private V value; |
||||
|
|
||||
|
public Pair(K key, V value) { |
||||
|
this.key = key; |
||||
|
this.value = value; |
||||
|
} |
||||
|
|
||||
|
public K getKey() { return key; } |
||||
|
public V getValue() { return value; } |
||||
|
|
||||
|
// 交换后 key/value 类型可能变化,所以返回新的 Pair<V, K>
|
||||
|
public Pair<V, K> swap() { |
||||
|
return new Pair<>(this.value, this.key); |
||||
|
} |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
Pair<String, Integer> p = new Pair<>("Age", 25); |
||||
|
Pair<Integer, String> swapped = p.swap(); |
||||
|
System.out.println(swapped.getKey() + "=" + swapped.getValue()); // 25=Age
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,14 @@ |
|||||
|
3. AI 架构审计(MVC 三层检查) |
||||
|
发送给 AI 的指令模板: |
||||
|
作为 Java 架构审计师,请检查我的 MVC 三层划分是否存在越权行为? |
||||
|
|
||||
|
我的项目结构: |
||||
|
· Model:Article(数据实体)、Storage(数据存取) |
||||
|
· View:ConsoleView(控制台输入输出) |
||||
|
· Controller:CommandController(解析命令、调用 Model) |
||||
|
|
||||
|
请从以下角度审计: |
||||
|
1. View 是否直接调用了 Model 层?(越权) |
||||
|
2. Controller 是否做了 View 的工作?(职责混淆) |
||||
|
3. Model 层是否包含了业务逻辑而非仅仅是数据存取? |
||||
|
4. 各层之间的依赖方向是否正确?(View → Controller → Model) |
||||
@ -0,0 +1,27 @@ |
|||||
|
import java.util.Date; |
||||
|
|
||||
|
public class Article { |
||||
|
private String title; |
||||
|
private String content; |
||||
|
private String url; |
||||
|
private String author; // 新增
|
||||
|
private Date publishDate; // 新增
|
||||
|
|
||||
|
// 构造器
|
||||
|
public Article(String title, String content, String url, String author, Date publishDate) { |
||||
|
this.title = title; |
||||
|
this.content = content; |
||||
|
this.url = url; |
||||
|
this.author = author; |
||||
|
this.publishDate = publishDate; |
||||
|
} |
||||
|
|
||||
|
// Getters and Setters
|
||||
|
public String getAuthor() { return author; } |
||||
|
public void setAuthor(String author) { this.author = author; } |
||||
|
|
||||
|
public Date getPublishDate() { return publishDate; } |
||||
|
public void setPublishDate(Date publishDate) { this.publishDate = publishDate; } |
||||
|
|
||||
|
// 原有字段的 getter/setter...
|
||||
|
} |
||||
@ -0,0 +1,8 @@ |
|||||
|
public class Controller { |
||||
|
private HistoryCommand history = new HistoryCommand(); |
||||
|
|
||||
|
public void executeCommand(String command) { |
||||
|
history.addCommand(command); // 记录每一条命令
|
||||
|
// ... 原有命令处理逻辑
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
public class HistoryCommand { |
||||
|
private List<String> commandHistory = new ArrayList<>(); |
||||
|
|
||||
|
// 添加命令到历史
|
||||
|
public void addCommand(String command) { |
||||
|
commandHistory.add(command); |
||||
|
} |
||||
|
|
||||
|
// 获取所有历史命令
|
||||
|
public List<String> getHistory() { |
||||
|
return new ArrayList<>(commandHistory); // 返回副本,保护原数据
|
||||
|
} |
||||
|
|
||||
|
// 打印历史记录
|
||||
|
public void printHistory() { |
||||
|
for (int i = 0; i < commandHistory.size(); i++) { |
||||
|
System.out.println((i + 1) + ". " + commandHistory.get(i)); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 清空历史
|
||||
|
public void clear() { |
||||
|
commandHistory.clear(); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue