抽象类 - 超级构造函数 (Java) [英] Abstract Classes - Super Constructor (Java)

查看:37
本文介绍了抽象类 - 超级构造函数 (Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的问题:

I have a really simple question:

假设我有一个抽象类,它代表酒吧中的一个人.

Let's say I have an abstract class which represents a person in a bar.

public class Person {
    protected String firstName;
    protected String lastName;

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

而且我还有 2 个扩展 Person 的类,假设一个类用于调酒师,一个类用于客户.

And I also have 2 classes that extend Person, let's say a class for bartenders and a class for customers.

在客户类中,我还想要一个 int 表示他的年龄作为一个字段.在调酒师班,我们没有.

In the class for customers, I also want an int representing his age as a field. In the bartender class, we don't.

另外,对于客户类,我想要一个方法 isAdult().

Also, for the customer class, I want a method isAdult().

public class Bartender extends Person {
    public Bartender(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

public class Customer extends Person {
private int age;    

    public Customer(String firstName, String lastName, int age) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }
}

我现在有两个问题:

1) 这不起作用,因为我收到消息隐式超级构造函数Passenger() 未定义.必须显式调用另一个构造函数".这究竟是什么意思?2)对于方法isAdult(),我觉得最好的方法是在抽象类Person中实现它,如下所示:

1) This doesn't work as I get the message "Implicit super constructor Passenger() is undefined. Must explicitly invoke another constructor". What does this exactly mean? 2) For the method isAdult(), I feel like the best way to do it is to implement it in the abstract class Person like this:

public abstract boolean isAdult();

然后为始终返回 true 的调酒师和检查年龄的客户实施它.

And then implement it for Bartenders returning always true and for Customers checking their age.

另一种方法是直接从类 Person 中实现它,如下所示:

Another way is implementing it from the class Person directly like this:

public boolean isAdult() {
    return (this instanceof Bartender || age > 18);
}

那行得通吗?哪种方式更好?

Would that work and which way would be better?

推荐答案

Person 有一个使用构造函数定义的参数,并且没有默认的无参数构造函数.由于子类的超级构造函数必须始终在子构造函数中调用,并且 Person 没有默认构造函数,因此您必须显式在子构造函数中调用它:

Person has a defined parameter using constructor and no default parameterless constructor. Since a child class's super constructor must always be called in the child constructor, and since Person has no default constructor, you must explicitly call it in the child constructor:

public Bartender(String firstName, String lastName) {
    super(firstName, lastName);

    // don't do this:
    // this.firstName = firstName;
    // this.lastName = lastName;
}

public Customer(String firstName, String lastName, int age) {
    super(firstName, lastName);
    this.age = age;
}

关于 isAdult(),您可以只在 Customer 类中使用此方法,而在 Bartender 中不使用它.或者,如果 super 必须有这个方法,如果有人在 Bartender 上调用它,你可以抛出一个异常,因为它不应该被那样调用.

Regarding isAdult(), you could just have this method in the Customer class and not have it in Bartender. Alternatively if the super must have this method, you could throw an exception if someone calls it on a Bartender, since it should not be called that way.

这篇关于抽象类 - 超级构造函数 (Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆