JavaParser 两个java文件的差异
时间: 2023-09-09 18:08:07 浏览: 168
文件比较,可以比较两个文件的不同地方
4星 · 用户满意度95%
好的,以下是比较两个 Java 文件的差异化代码示例:
```java
import java.util.List;
public class MyClass {
private String name;
private int age;
private List<String> hobbies;
public MyClass(String name, int age, List<String> hobbies) {
this.name = name;
this.age = age;
this.hobbies = hobbies;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public List<String> getHobbies() {
return hobbies;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public void introduceMyself() {
System.out.println("Hi, my name is " + name + ", I am " + age + " years old.");
System.out.println("My hobbies are: ");
for (String hobby : hobbies) {
System.out.println("- " + hobby);
}
}
}
```
```java
import java.util.List;
public class MyClass {
private String name;
private int age;
private List<String> hobbies;
public MyClass(String name, int age, List<String> hobbies) {
this.name = name;
this.age = age;
this.hobbies = hobbies;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public List<String> getHobbies() {
return hobbies;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public void introduceMyself() {
System.out.println("Hello, my name is " + name + ", I am " + age + " years old.");
System.out.println("My hobbies are: ");
for (String hobby : hobbies) {
System.out.println("- " + hobby);
}
}
public static void main(String[] args) {
MyClass myClass = new MyClass("Tom", 25, List.of("Swimming", "Basketball", "Reading"));
myClass.introduceMyself();
}
}
```
这两个 Java 文件的主要差异在于:
1. `introduceMyself` 方法中输出的字符串不同,第一个文件输出 "Hi, my name is ...",而第二个文件输出 "Hello, my name is ..."。
2. 第二个文件增加了一个 `main` 方法用于测试 `MyClass` 类的功能,该方法会创建一个 `MyClass` 对象,并调用其 `introduceMyself` 方法。
阅读全文