public class B
public static void main(String[] args)
Set<Teacher>set = new HashSet<Teacher>()
Iterator<Teacher>iterator = set.iterator()
while (iterator.hasNext())
System.out.println
class Teacher
private int id
public Teacher()
public Teacher(int id, String name, int age, boolean sex)
this.sex = sex
HashSet类
实现了Set接口,由哈希表(实际上是一个HashMap实例)支持。它不保证Set的迭代顺序,特别是它不保证该顺序持久不变,此类允许使用空中null元素。添加到由HashSet类实现的Set集合渣磨中的对象。需要重新实现equal()方法。
从而保证插入集合中对象的标识的唯一性。斗梁山HashSet类实现了Set接口,由哈希表(实际上是一个HashMap实例)支持。它不保证Set的迭代顺序,特别是它不保证该顺序持久不变。
package fanqiclass Teacher {private String name = nullpublic String getName() {
return name
} public void setName(String name) {
this.name = name
} public String getSex() {
return sex
} public void setSex(String sex) {
this.sex = sex
} public int getAge() {
return age
} public void setAge(int age) {
this.age = age
} public String getCourse() {
return course
} public boolean setCourse(String course) {
if (course == null || course.trim().length() == 0) {
System.out.println("设置课程失败!")
return false
}
this.course = course
return true
} public int getEffect() {
return effect
} public boolean setEffect(int effect) {
if (effect <1 || effect >4) {
System.out.println("设置失败!")
return false
} else {
this.effect = effect
return true
}
} private String sex = null
private int age = 0
private String course = null
private int effect = 1public Teacher(String name, String sex, int age, String course, int effect) {
this.name = name
this.sex = sex
this.age = age
this.course = course
this.effect = effect
} public String getDetails() {
return "姓名:" + getName() + "\n性别:" + getSex() + "\n年龄:" + getAge()
+ "族友并\n讲授告陵课程:" + getCourse()
}
}public class WuZiQi { public static void main(String[] args) {
Teacher t = new Teacher("张三"兆迹, "男", 35, "计算机基础", 2)
System.out.println(t.getDetails())
}
}
定义教师类此颤,public class TeacherInfo,里面添加变量,参考写法,private string teacherName;后面再封装一下,set和get方法,重写tostring方法,输出肆毕教师类对象信息就行森雹败了欢迎分享,转载请注明来源:内存溢出
评论列表(0条)