首先,我想在我的应用程序中表示两个不同的用户(管理和客户端),它们可以通过两个不同的图形接口访问两个不同的类.
我的想法是忠实地表示e/r中的内容,我希望使用一个接口来概括共享信息(如nick,--电子邮件),并将特定的信息表示到两个子类中。
要存储信息,我使用一个数据库,e/r的一部分是:

现在,在我的代码中,我以这样的方式实现了它:
Utente实体--使用Interface表示:
package users;
/**
* Interface which represent a general user (both admin either client)
*
* @author Federico Cuozzo
*
*/
public interface User {
public String getNick();
public String getPw();
public String getEmail();
}Admin实体--使用带有覆盖的Class表示:
package users;
import global.Globals;
import java.util.ArrayList;
/**
* Represents the admin user instance and his all information.
*
* @author Federico Cuozzo
*
*/
public class Admin implements User {
private String nick;
private String pw;
private String email;
private ArrayList<Integer> authorizations = new ArrayList<Integer>(); // List of authorizations rwx in integer (r-- 4, rwx 7)
public Admin(String aNick, String aPw, String anEmail) {
this.nick = aNick;
this.pw = aPw;
this.email = anEmail;
this.authorizations.add((int) Globals.r.nextInt((7-4) + 1) + 4);
this.authorizations.add((int) Globals.r.nextInt((7-4) + 1) + 4);
this.authorizations.add((int) Globals.r.nextInt((7-4) + 1) + 4);
}
// Accessor methods for private fields
@Override
public String getNick() { return this.nick; }
@Override
public String getPw() { return this.pw; }
@Override
public String getEmail() { return this.email; }
public ArrayList<Integer> getAuthorizations() { return this.authorizations; }
}Client实体--使用带有覆盖的Class表示:
package users;
/**
* Represents the client user instance and his all information.
*
* @author Federico Cuozzo
*
*/
public class Client implements User {
private String nick;
private String pw;
private String name;
private String surname;
private String email;
private String street;
private String city;
private String cap;
private String phoneNumber;
public Client(String aNick, String aPw, String anEmail, String aName, String aSurname, String aStreet, String aCity, String aCap, String aPhoneNumber) {
this.nick = aNick;
this.pw = aPw;
this.name = aName;
this.surname = aSurname;
this.email = anEmail;
this.street = aStreet;
this.city = aCity;
this.cap = aCap;
this.phoneNumber = aPhoneNumber;
}
// Accessor methods for private fields
@Override
public String getNick() { return this.nick; }
@Override
public String getPw() { return this.pw; }
@Override
public String getEmail() { return this.email; }
public String getName() { return this.name; }
public String getSurname() { return this.surname; }
public String getStreet() { return this.street; }
public String getCity() { return this.city; }
public String getCap() { return this.cap; }
public String getPhoneNumber() { return this.phoneNumber; }
}这是正确的吗?使用一个接口表示主类,两个具体类表示子类.
发布于 2016-08-16 18:22:43
虽然实现看起来在逻辑上是正确的,但是关于代码样式的一些注释:
com.your.company.users、com.your.company.globala前缀参数,您可以使用与对象字段相同的名称,因为通过this可以确定引用哪一个。例如:
public Client(String nick, ...) {
this.nick = nick;
// ...
}https://codereview.stackexchange.com/questions/138859
复制相似问题