public class Student{ private int id; private String name; public Student(int id,String name){ this.id = id; this.name = name; } } public static void main(String[] args) { Set<Student> set = new TreeSet<>(); set.add(new Student(1,"xiaohei")); set.add(new Student(2,"xiaobai")); System.out.println(set.size()); }
时间: 2024-02-14 19:23:45 浏览: 27
这段代码创建了一个Student类,包含id和name属性以及一个构造方法。在主函数中,创建了一个TreeSet类型的set对象,用于存储Student对象,并向其中添加了两个Student对象。最后输出set的大小。
需要注意的是,由于Student类没有实现Comparable接口或提供Comparator比较器,因此在添加Student对象时,TreeSet无法确定它们之间的顺序。因此,当执行set.size()时,可能会抛出ClassCastException异常,提示无法将Student对象转换为java.lang.Comparable。为了解决这个问题,可以让Student类实现Comparable接口,并重写compareTo方法,或者在创建TreeSet对象时提供一个Comparator比较器。
相关问题
51、(1分)假设我们有以下代码: public class Student private String name; private int id; public Student(String name, int id) this.name name; this.id = id;
这段代码是不完整的,缺少类体部分和构造函数的结束符号。如果我们把类体部分补全,例如:
```
public class Student {
private String name;
private int id;
public Student(String name, int id) {
this.name = name;
this.id = id;
}
}
```
那么这段代码就定义了一个名为Student的类,包含两个私有属性name和id,以及一个公有的构造函数。构造函数接受两个参数name和id,并将它们分别赋值给类的属性name和id。这个类可以用于创建学生对象,每个学生对象都有一个name和一个id属性。
完善下列代码:public class Student { private int id; private String name; private int classId; private int teacherId; // 构造函数、Getter和Setter方法 } public class Class { private int id; private String name; private int teacherId; // 构造函数、Getter和Setter方法 } public class Teacher { private int id; private String name; // 构造函数、Getter和Setter方法 }
下面是完整的代码:
```java
public class Student {
private int id;
private String name;
private int classId;
private int teacherId;
public Student(int id, String name, int classId, int teacherId) {
this.id = id;
this.name = name;
this.classId = classId;
this.teacherId = teacherId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getClassId() {
return classId;
}
public void setClassId(int classId) {
this.classId = classId;
}
public int getTeacherId() {
return teacherId;
}
public void setTeacherId(int teacherId) {
this.teacherId = teacherId;
}
}
public class Class {
private int id;
private String name;
private int teacherId;
public Class(int id, String name, int teacherId) {
this.id = id;
this.name = name;
this.teacherId = teacherId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTeacherId() {
return teacherId;
}
public void setTeacherId(int teacherId) {
this.teacherId = teacherId;
}
}
public class Teacher {
private int id;
private String name;
public Teacher(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
```
在每个类中,我们定义了构造函数和Getter/Setter方法来设置和获取私有变量。这些方法使得我们可以封装这些类并从其他类中访问它们的属性。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)