0

因此,在深入学习课程之前,我试图理解压倒一切。我看过 TheNewBoston (bucky) 解释压倒一切,但我不确定我是否做得对。有人会检查我并让我走上正确的道路吗?

我不记得 bucky 必须在他的超类中保护他的任何实例变量。但是当我构建我的子类时,eclipse 让我将我的实例变量转换为受保护的。

超类联系人:

public class Contacts
{
    protected String fname;
    protected String lname;
    protected String email;
    protected String phone;

    public Contacts(String fname, String lname, String email, String phone)
    {
        this.fname=fname;
        this.lname=lname;
        this.email=email;
        this.phone=phone;

    }

    public String getfname()
    {
        return fname;
    }

    public void setfname(String first)
    {
        this.fname=first;       
    }

    public String getlname()
    {
        return lname;
    }

    public void setlname(String last)
    {
        this.lname=last;
    }

    public String getemail()
    {
        return email;
    }

    public void setemail(String e)
    {
        this.email=e;
    }

    public String getphone()
    {
        return phone;
    }

    public void setphone(String num)
    {
        this.phone=num;
    }

    public String getFullName()
    {
        String full=fname+" "+lname;
        return full;
    }

子类朋友:

public class Friend extends Contacts
{
    private String dob;


    public Friend(String fname, String lname, String email, String phone)
    {
        super(fname, lname, email, phone);
    }


    public String getDob()
    {
        return dob;
    }


    public void setDob(String dob)
    {
        this.dob = dob;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString()
    {
        return fname+", "+lname+", "+email+", "+phone+", "+dob;
    }




}

子类 BusinessAssociate:

public class BusinessAssociate extends Contacts
{
    private String title;
    private String position;
    private String company;
    private String full;

    public BusinessAssociate(String fname, String lname, String email,
            String phone)
    {
        super(fname, lname, email, phone);

    }

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String title)
    {
        this.title=title;
    }

    public String getCompany()
    {
        return company;
    }

    public void setCompany(String company)
    {
        this.company=company;
    }

    public String getPosition()
    {
        return position;
    }

    public void setPosition(String position)
    {
        this.position=position;
    }

    /* (non-Javadoc)
     * @see Contacts#getFullName()
     */
    @Override
    public String getFullName()
    {
        full=title+" "+fname+" "+lname;
        return full;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString()
    {
        return full+", "+email+", "+phone+", "+company+", "+position;
    }




}
4

1 回答 1

2

您的代码中的覆盖对我来说看起来是正确的。

但请注意,您只能覆盖实例方法。构造函数、字段和静态方法不能被覆盖。

有评论问:

“字段不能被覆盖”是什么意思?这对我来说似乎是一种替代:

    class A { protected int a = 1; } 
    class B extends A { protected int a = 2; }`. 

你能澄清一下吗?

这不是一个覆盖。您在这里拥有的是子类中的一个字段,B隐藏了超类中的字段A。如果您创建它的一个实例,B它将有两个不同的a字段,具有不同的值。

参考:https ://stackoverflow.com/a/11971061/139985


...但是为什么eclipse让我把构造函数放在每个子类中。youtube 视频上不是这样的。

因为 Eclipse 实现了 JLS,而 JLS 需要它。YouTube 视频中的示例可能有所不同。

您的示例的设计方式,基类Contacts具有子类共有/使用的字段,并且它(自然)在其构造函数中初始化这些字段。您没有(也不应该)有一个无参数的构造函数,Contacts因为这将允许人们创建Constants具有null名称、电子邮件和电话字段的无用实例。

然而,没有无参数构造函数的事实Contacts意味着任何子类都需要显式的构造函数声明。Java 的工作方式是,如果您不声明任何构造函数,则一个类将获得一个默认的无参数构造函数。但是只有当超类有一个 no0args 构造函数时,这个默认构造函数才可能。在这种情况下,它不会。

So Eclipse was correct in insisting that your subclasses declared constructors. The Java compiler would also have been happy with the alternative of adding a no-args constructor to Contacts. But that would be the wrong solution, because it would result in BusinessAssociate and Friend instances being created with null fields.


But when I was building my subclasses, eclipse made me convert my instance variables to protected.

It is not entirely clear why that happened ('cos we don't know what you had before!!), but if your subclasses refer directly to fields of the superclass, then the superclass fields must have the right "access" modifiers to allow this. (For instance, a subclass cannot ever refer directly to a superclasses private fields. The Java Language Specification forbids this.)

于 2013-03-30T02:53:56.320 回答