首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >管理员和客户端“是”用户

管理员和客户端“是”用户
EN

Code Review用户
提问于 2016-08-16 16:47:33
回答 1查看 353关注 0票数 0

首先,我想在我的应用程序中表示两个不同的用户(管理和客户端),它们可以通过两个不同的图形接口访问两个不同的类.

我的想法是忠实地表示e/r中的内容,我希望使用一个接口来概括共享信息(如nick,--电子邮件),并将特定的信息表示到两个子类中。

要存储信息,我使用一个数据库,e/r的一部分是:

现在,在我的代码中,我以这样的方式实现了它:

Utente实体--使用Interface表示:

代码语言:javascript
运行
复制
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表示:

代码语言:javascript
运行
复制
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表示:

代码语言:javascript
运行
复制
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; }
}

这是正确的吗?使用一个接口表示主类,两个具体类表示子类.

EN

回答 1

Code Review用户

发布于 2016-08-16 18:22:43

虽然实现看起来在逻辑上是正确的,但是关于代码样式的一些注释:

  • 包名应该包括您的组织,例如com.your.company.userscom.your.company.global
  • 不需要使用a前缀参数,您可以使用与对象字段相同的名称,因为通过this可以确定引用哪一个。

例如:

代码语言:javascript
运行
复制
public Client(String nick, ...) {
    this.nick = nick;
    // ...
}
票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/138859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档