简体   繁体   English

我无法使用 super() 在子类中的超类之外创建 2 个对象;

[英]I Can't create 2 objects off superclass in subclass using super();

I have a Superclass 'Telefoon' (english: Telephone).我有一个超类“电话”(英语:电话)。 this superclass has a child/subclass 'ContactGegevens' (Egnlish Contact data/user Information).这个超类有一个子类/子类“ContactGegevens”(英语联系数据/用户信息)。 This subclass 'ContactGegevnes' is a superclass of a child/subclass as well but is not relevant for my question.这个子类“ContactGegevnes”也是子类/子类的超类,但与我的问题无关。

In Super class 'Telefoon' (Telephone).在超级班“电话”(电话)中。

public class Telefoon {

//Properties.
private String soort; //Enlgish = Type (Mobile or landline)
private String nummer; //English = Number (Just the number).

//Getters and Setters.
//Just normal getters and setters of all properties. not relevant to show here.    

//Constructors.
public Telefoon(String soort, String nummer) {
    this.nummer = nummer;
    this.soort = soort;
}

/** This constructor does nothing but when I don't have this one it gives and error in the child 
class??? */
public Telefoon() {
    this.nummer = "000000000";
    this.soort = "vast";
}

//Methods.
//Not relevant for question.

Child/Subclass 'ContactGegevens'.子类/子类“ContactGegevens”。

public class ContactGegevens extends Telefoon {

//Properties.
private String eMail;         //Email.
private Telefoon telefoon;    //Landline number.
private Telefoon gsm;         //Mobile phone number.

//Getters and Setters.
//Getters and Setters of all 3 properties. irrelevant to show here

//Constructors.
public ContactGegevens(String vast, String mobiel, String eMail) {
    /**
    *This works but in this case the 'extends Telefoon' is useless???!!
    As you can see I create 2 objects of superClass Telefoon. But then the 'extends Telefoon'
    is unnecessary, is there a Way i can do something like 

    telefoon = Super();
    gsm = Super(); 

    */
    telefoon = new Telefoon(vast,"vast");
    gsm = new Telefoon(mobiel, "mobiel");
    this.eMail = eMail;
}

//Methods.
public String toString() {
    return String.format("E-mail: %-15s %nTel: %-12s %nGSM: %-12s",eMail,telefoon.getNummer(), gsm.getNummer());
}

My problem is that The class 'ContactGegevens' extends Telefoon But in the constructor of ContactGegevens I still have to create 2 objects of class Telefoon rendering 'Extends Telefoon' useless.我的问题是“ContactGegevens”类扩展了 Telefoon 但是在 ContactGegevens 的构造函数中,我仍然必须创建 Telefoon 类的 2 个对象,使“Extends Telefoon”变得无用。 Can I do something like Telefoon telefoon = Super(//Param here);?我可以做类似 Telefoon telefoon = Super(//Param here); 之类的事情吗?

And also Why do I need a default constructor in Telefoon otherwise I get an error when instantiating the object 'Telefoon' in the constructor of 'ContactGegevens'.还有为什么我需要 Telefoon 中的默认构造函数,否则在“ContactGegevens”的构造函数中实例化对象“Telefoon”时会出错。

As noted, the ContactGegevens class shouldn't inherit from Telefoon , because it does not satisfy the “is-a” logic of inheritance.如前所述, ContactGegevens类不应从Telefoon继承,因为它不满足继承的“is-a”逻辑。 Instead, it has multiple phone numbers.相反,它多个电话号码。

But given the constraint you're operating with, I would suggest removing the telefoon member from ContactGegevens : it's replaced by the inheritance.但是考虑到您操作的限制,我建议从ContactGegevens删除telefoon成员:它被继承所取代。 This leaves us with:这给我们留下了:

public class ContactGegevens extends Telefoon {
    private String eMail;         //Email.
    private Telefoon gsm;         //Mobile phone number.

    public ContactGegevens(String vast, String mobiel, String eMail) {
        super("vast", vast);
        this.eMail = eMail;
        gsm = new Telefoon("mobiel", mobiel);
    }

    public String toString() {
        return String.format("E-mail: %-15s %nTel: %-12s %nGSM: %-12s", eMail, getNummer(), gsm.getNummer());
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM