5 changed files with 187 additions and 0 deletions
@ -0,0 +1,70 @@ |
|||
package com.example.datacollect.model; |
|||
import java.util.Date |
|||
public class Article { |
|||
private String title; |
|||
private String url; |
|||
private String content; |
|||
private String author; |
|||
<<<<<<< HEAD |
|||
private Date publishDate; |
|||
======= |
|||
private String Date publishDate; |
|||
>>>>>>> 81e72ba458591eeb2c3e547f9801e93be758f6eb |
|||
public Article(String title, String url, String content,String author,Date publishDate) { |
|||
this.title = title; |
|||
this.url = url; |
|||
this.content = content; |
|||
this.author = author; |
|||
this.publishDate= publishDate; |
|||
} |
|||
|
|||
public String getTitle() { |
|||
return title; |
|||
} |
|||
|
|||
public void setTitle(String title) { |
|||
this.title = title; |
|||
} |
|||
|
|||
public String getUrl() { |
|||
return url; |
|||
} |
|||
|
|||
public void setUrl(String url) { |
|||
this.url = url; |
|||
} |
|||
|
|||
public String getContent() { |
|||
return content; |
|||
} |
|||
|
|||
public void setContent(String content) { |
|||
this.content = content; |
|||
} |
|||
public String getAuthor(){ |
|||
return author; |
|||
} |
|||
public void setAuthor(String author){ |
|||
this.author= author; |
|||
} |
|||
<<<<<<< HEAD |
|||
public Date getPublishDate() { |
|||
return publishDate; |
|||
} |
|||
public void setPublishDate(Date publishDate) { |
|||
this.publishDate = publishDate; |
|||
} |
|||
======= |
|||
>>>>>>> 81e72ba458591eeb2c3e547f9801e93be758f6eb |
|||
@Override |
|||
public String toString() { |
|||
return "Article{" |
|||
+ "title='" + title + '\'' |
|||
+ ", url='" + url + '\'' |
|||
+ '}'; |
|||
} |
|||
<<<<<<< HEAD |
|||
} |
|||
======= |
|||
} |
|||
>>>>>>> 81e72ba458591eeb2c3e547f9801e93be758f6eb |
|||
@ -0,0 +1,17 @@ |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
public class HistoryCommand { |
|||
// 存储所有输入过的命令
|
|||
private final List<String> commandHistory = new ArrayList<>(); |
|||
|
|||
// 添加命令
|
|||
public void addCommand(String cmd) { |
|||
commandHistory.add(cmd); |
|||
} |
|||
|
|||
// 获取全部历史
|
|||
public List<String> getHistory() { |
|||
return new ArrayList<>(commandHistory); // 防御性拷贝,避免外部修改
|
|||
} |
|||
} |
|||
@ -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(); |
|||
} |
|||
} |
|||
} |
|||
@ -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("小猫喵喵叫:喵喵喵!"); |
|||
} |
|||
} |
|||
@ -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("小猫不会游泳哦!"); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue