“考虑一个类注册。它有两组getter和setter。一组用于教授,一组用于学生。学生可以注册自己但无法获得注册数据。另一方面,教授可以注册并查看其数据和学生数据。如何通过公共方法实现这一目标。“
以下是我对上述问题的解决方案。解决方案是正确的,如果不是为什么以及什么是解决这个问题的正确解决方案。
public class Register {
private String professor;
private String student;
public String getProfessor() {
return professor;
}
public void setProfessor(String professor) {
this.professor = professor;
}
public String getStudent() {
return student;
}
public void setStudent(String student) {
this.student = student;
}
}
public class Student extends Register {
public Student(String student) {
super.setStudent(student);
}
@Override
public String getProfessor() {
return null;
}
@Override
public String getStudent() {
return null;
}
@Override
public void setProfessor(String professor) {
super.setProfessor(null);
}
}
public class Professor extends Register {
public Professor(String student, String professor) {
super.setStudent(student);
super.setProfessor(professor);
}
}
public class Main {
public static void main(String[] args) {
Student std1 = new Student("Rumman");
System.out.println("Data from Student Object");
System.out.println("Student Name = " + std1.getStudent());
System.out.println("Professor Name = " + std1.getProfessor());
System.out.println("--------------------------");
Professor prof1 = new Professor("Rumman", "Sadiq");
System.out.println("Data from Professor Object");
System.out.println("Student Name = " + prof1.getStudent());
System.out.println("Professor Name = " + prof1.getProfessor());
}
}
输出:
Data from Student Object
Student Name = null
Professor Name = null
--------------------------
Data from Professor Object
Student Name = Rumman
Professor Name = Sadiq
答案 0 :(得分:0)
当学生尝试更改某些感知数据时,创建在实际注册时保护的代理:
注册界面
public interface Register {
String getProfessor();
void setProfessor(String professor);
String getStudent();
void setStudent(String student);
}
真实注册
public class RegisterImpl implements Register {
private String professor;
private String student;
@Override
public String getProfessor() {
return professor;
}
@Override
public void setProfessor(String professor) {
this.professor = professor;
}
@Override
public String getStudent() {
return student;
}
@Override
public void setStudent(String student) {
this.student = student;
}
}
安全注册
public class StudentRegister implements Register {
private final Register register;
public StudentRegister(Register register) {
this.register = register;
}
@Override
public String getProfessor() {
throw new SecurityException("this is not allowed for students");
}
@Override
public void setProfessor(String professor) {
throw new SecurityException("this is not allowed for students");
}
@Override
public String getStudent() {
throw new SecurityException("this is not allowed for students");
}
@Override
public void setStudent(String student) {
register.setStudent(student);
}
}
和管理员的控制寄存器
public final class Manager {
private final Register register;
public Manager(Register register) {
this.register = register;
}
public Register getRegister(String password) {
if (password.equals("professor password")) {
return register;
} else {
return new StudentRegister(register);
}
}
}
测试课
public class Main {
private final static Manager manager = new Manager(new RegisterImpl());
public static void main(String[] args) {
professorUser();
studentUser();
}
public static void professorUser() {
Register register = manager.getRegister("professor password");
register.setProfessor("James Gosling");
register.setStudent("Duke");
System.out.println(register.getProfessor());
System.out.println(register.getStudent());
}
public static void studentUser() {
Register register = manager.getRegister("student password");
register.setStudent("Duke"); // <- allowed
// all actions below not allowed and cause exception when student try to do it
register.setProfessor("None");
register.getStudent();
register.getProfessor();
}
}