避免对象中的参数参数具有相同的值

假设我像这样用Java创建一个对象

Student std1 = new Student("Tom", "male");

我需要防止另一个对象(例如:std2、std3 等)与 std1 具有相同的值。怎么做?

[编辑]我想要的例子是:

不允许: Student std2 = new Student("Tom", "male");

允许: Student std2 = new Student("Not Tom", "male");Student std2 = new Student("Tom", "female");Student std2 = new Student("John", "male");

谢谢


动漫人物
浏览 173回答 4
4回答

吃鸡游戏

您可以通过添加串联的姓名、性别来创建静态字符串哈希集。class YourClass {&nbsp; &nbsp;public static Set<String> studentSet = new HashSet<>();&nbsp; &nbsp;public static void addStudent(String name, String gender) {&nbsp; &nbsp; &nbsp;YourClass.studentSet.add(name + "," + gender);&nbsp; &nbsp;}&nbsp; &nbsp;public static Boolean studentExists(String name, String gender) {&nbsp; &nbsp; &nbsp;return YourClass.studentSet.constains(name + "," + gender);}因此,您的 HashSet 中的数据将类似于 Tom,male 、Tom,female、John,male 。class Student {...public Student(String name, String gender) {&nbsp; &nbsp; this.name = name;&nbsp; &nbsp; this.gender = gender;&nbsp; &nbsp; YourClass.addStudent(name, gender);&nbsp; }}在创建 Student 实例时,您可以检查 HashSet 中是否没有 Name、Gender,然后实例化和对象。if(!YourClass.studentExists(name, gender)) {&nbsp; &nbsp; Student student = new Student(name, gender);}我希望这能解决你的问题

暮色呼如

您可以创建一个类来控制对象的创建 - 基本上使用享元模式。步骤 1. 将 Student 包的构造函数设置为私有public class Student {&nbsp; &nbsp; Student(String name, String gender) {&nbsp; &nbsp; ...步骤2.在Student中实现equals方法&nbsp; &nbsp;public boolean equals (Object other) {&nbsp; &nbsp; &nbsp; &nbsp;if (!(other instanceof Student)) return false;&nbsp; &nbsp; &nbsp; &nbsp;Student otherStudent = (Student) other;&nbsp; &nbsp; &nbsp; &nbsp;return Objects.equals(otherStudent.getName(), this.getName()) && Objects.equals(otherStudent.getGender(), this.getGender());&nbsp; &nbsp;}步骤 3. 在同一包中实现 Student 对象池public class StudentPool {&nbsp; &nbsp; private List<Student> students = new ArrayList<>();&nbsp; &nbsp; public Student getOrCreate(String name, String gender) {&nbsp; &nbsp; &nbsp; &nbsp; Student student = new Student(name, gender);&nbsp; &nbsp; &nbsp; &nbsp; return students.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(s -> Objects.equals(s, student))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .findFirst()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .orElseGet(() -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; students.add(student);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return student;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; public List<Student> get(String name) {&nbsp; &nbsp; &nbsp; &nbsp; return students.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(student -> Objects.equals(student.getName(), name))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());&nbsp; &nbsp; }}

婷婷同学_

假设您所说的“值”指的是学生的姓名,我建议您编写一个小函数,如果该姓名已被占用,该函数将返回 true。为此,您可以将所有“学生”存储在 ArrayList 中:ArrayList<Student> students = new ArrayList<Student>();现在您可以将学生添加到此 ArrayList 中,如下所示:private boolean nameIsAlreadyTaken(ArrayList<Student> students, String newName){&nbsp; &nbsp; for (int i = 0; i < student.size(); i++){&nbsp; &nbsp; &nbsp; &nbsp; if(students.get(i).getName().equals(newName)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return false;}(您需要在学生类中定义一个 getName() 函数才能使其工作。)那么你可以这样做:字符串 newName = "迈克尔"; String newGender = "男";if (!nameIsAlreadyTaken(students, newName)){students.add(new Student(newName, newGender));} else{&nbsp; &nbsp; //something you want to to if the name is already taken}您可以在不将 Students-ArrayList 传递给函数的情况下执行此操作,但这取决于您。

偶然的你

我想说你需要重写Student和equals方法hashCode,然后检查构造函数是否存在这样的学生。关于这一点的好答案:链接。import java.util.*;public class Student {&nbsp; &nbsp; private static final Set<Student> REGISTERED_STUDENTS = new HashSet<>();&nbsp; &nbsp; private String name;&nbsp; &nbsp; private String gender;&nbsp; &nbsp; public static Collection<Student> getRegisteredStudents() {&nbsp; &nbsp; &nbsp; &nbsp; return Collections.unmodifiableCollection(REGISTERED_STUDENTS);&nbsp; &nbsp; }&nbsp; &nbsp; public Student(final String name, final String gender) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; this.gender = gender;&nbsp; &nbsp; &nbsp; &nbsp; if (REGISTERED_STUDENTS.contains(this))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw DuplicateStudentException();&nbsp; &nbsp; &nbsp; &nbsp; REGISTERED_STUDENTS.add(this);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int hashCode() {&nbsp; &nbsp; &nbsp; &nbsp; return Objects.hash(name, gender);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean equals(final Object o) {&nbsp; &nbsp; &nbsp; &nbsp; if (this == o) return true;&nbsp; &nbsp; &nbsp; &nbsp; if (!(o instanceof Student)) return false;&nbsp; &nbsp; &nbsp; &nbsp; final Student student = (Student) o;&nbsp; &nbsp; &nbsp; &nbsp; return Objects.equals(name, student.name) && Objects.equals(gender, student.gender);&nbsp; &nbsp; }}请注意,这个示例不是线程安全的,并且使用了在构造函数中抛出异常的有争议的解决方案。您可能需要一个工厂方法和ConcurrentSkipListSet/或另一个线程安全集合,如下所示:class Student {&nbsp; &nbsp; private static final Set<Student> REGISTERED_STUDENTS = new ConcurrentSkipListSet<>(Comparator.comparing(Student::getName).thenComparing(Student::getGender));&nbsp; &nbsp; private String name;&nbsp; &nbsp; private String gender;&nbsp; &nbsp; public static Set<Student> getRegisteredStudents() {&nbsp; &nbsp; &nbsp; &nbsp; return Collections.unmodifiableSet(REGISTERED_STUDENTS);&nbsp; &nbsp; }&nbsp; &nbsp; public static void addStudent(final String name, final String gender) {&nbsp; &nbsp; &nbsp; &nbsp; Student probablyExists = new Student(name, gender);&nbsp; &nbsp; &nbsp; &nbsp; REGISTERED_STUDENTS.add(probablyExists);&nbsp; &nbsp; }&nbsp; &nbsp; private Student(final String name, final String gender) {&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; &nbsp; &nbsp; this.gender = gender;&nbsp; &nbsp; }&nbsp; &nbsp; public String getName() {&nbsp; &nbsp; &nbsp; &nbsp; return name;&nbsp; &nbsp; }&nbsp; &nbsp; public String getGender() {&nbsp; &nbsp; &nbsp; &nbsp; return gender;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int hashCode() {&nbsp; &nbsp; &nbsp; &nbsp; return Objects.hash(name, gender);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean equals(final Object o) {&nbsp; &nbsp; &nbsp; &nbsp; if (this == o) return true;&nbsp; &nbsp; &nbsp; &nbsp; if (!(o instanceof Student)) return false;&nbsp; &nbsp; &nbsp; &nbsp; final Student student = (Student) o;&nbsp; &nbsp; &nbsp; &nbsp; return Objects.equals(name, student.name) && Objects.equals(gender, student.gender);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java