如何为实现接口的类创建一个Java构造函数

huangapple go评论72阅读模式
英文:

How to make a Java constructor for a class that implements an interface

问题

我知道要使用一个接口,我需要将其实现为一个类。然而,我不理解如何为该类创建构造函数?我没有其他要添加到接口Classroom的方法或变量,我只需要为它创建一个构造函数,以便我可以使用它并创建一个Classroom对象。

当我尝试使用接口时,比如 Classroom classroom = new Classroom(),我知道这是错误的,但我的IDE会通过以下方式修复它:

Classroom classroom = new Classroom() {
    @Override
    public Map<String, String> students() {
        return null;
    }

    @Override
    public String grade() {
        return null;
    }

    @Override
    public Optional<String> teacher() {
        return Optional.empty();
    }
}

我不知道如何解释来自我的IDE的建议,我应该为students()grade()等编写方法吗?我只需要使用我的自定义值为students、grade和teacher创建一个Classroom对象。

我是Java的新手。

英文:

I have an interface I need to use that looks similar to:

public interface Classroom {
    enum Grade {
        Primary, Elementary
    }

    Map&lt;String, String&gt; students();

    String grade();

    Optional&lt;String&gt; teacher();
}

I know in order to use an interface, I need to implement it as a class. However, I don't understand how to create the constructor for the class? I don't have any other methods or variables to add to the interface Classroom, I just need a constructor for it so I can use it and create a Classroom object.

public class BiologyClassroom implements Classroom {
    public BiologyClassroom(Map&lt;String, String&gt; students, String grade, Optional&lt;String&gt; teacher) {
        // not sure what to put here?
    }
}

When I do try to use the interface by doing Classroom classroom = new Classroom() which I know is wrong, my IDE will fix it by by replacing it with:

Classroom classroom = new Classroom() {
            @Override
            public Map&lt;String, String&gt; students() {
                return null;
            }

            @Override
            public Grade grade() {
                return null;
            }

            @Override
            public Optional&lt;String&gt; teacher() {
                return Optional.empty();
            }
        }

I don't know how to interpret this suggestion from my IDE, am I supposed to write methods for students(), grade() etc? I just need to make a Classroom object using my own values for students, grade and teacher.

I am new to Java.

答案1

得分: 0

你可以将接口中的方法实际上视为获取器(不以 "get" 开头),因此,如果你想避免重复太多代码,可以使用抽象类:

public abstract class AbstractClassroom implements Classroom {
    private final Map<String, String> students;
    private final Grade grade;
    private final Optional<String> teacher;

    protected AbstractClassroom(Map<String, String> students, Grade grade, Optional<String> teacher) {
        this.students = students;
        this.grade = grade;
        this.teacher = teacher;
    }

    @Override
    public Map<String, String> students() {
        return students;
    }

    @Override
    public Grade grade() {
        return grade;
    }

    @Override
    public Optional<String> teacher() {
        return teacher;
    }
}

然后,你可以按照以下方式编写你的 BiologyClassroom 类:

public class BiologyClassroom extends AbstractClassroom {
    public BiologyClassroom(Map<String, String> students, Grade grade, Optional<String> teacher) {
        super(students, grade, teacher);
    }
}

你还可以考虑放弃接口,直接使用抽象类。

英文:

The methods in your interface are actually getters (that don't start with "get"), so if you want to avoid repeating too much code, you can use an abstract class:

public abstract class AbstractClassroom implements Classroom {
    private final Map&lt;String, String&gt; students;
    private final Grade grade;
    private final Optional&lt;String&gt; teacher;

    protected AbstractClassroom(Map&lt;String, String&gt; students, Grade grade, Optional&lt;String&gt; teacher) {
        this.students = students;
        this.grade = grade;
        this.teacher = teacher;
    }

    @Override
    public Map&lt;String, String&gt; students() {
        return students;
    }

    @Override
    public Grade grade() {
        return grade;
    }

    @Override
    public Optional&lt;String&gt; teacher() {
        return teacher;
    }
}

You can then write your class BiologyClassroom as follows:

public class BiologyClassroom extends AbstractClassroom {
    public BiologyClassroom(Map&lt;String, String&gt; students, String grade, Optional&lt;String&gt; teacher) {
        super(students, grade, teacher);
    }
}

You can also consider dropping the interface and use the abstract class instead.

答案2

得分: 0

在你的 interface 中,你已经将 studentsgradesteacher 声明为方法。我认为你打算将它们用作 variables

只需使用基本的继承;没有必要特别使用 interface

class Classroom {
    enum Grade {
        Primary, Elementary
    }

    Map&lt;String, String&gt; students;
    String grade;
    Optional&lt;String&gt; teacher;

    public Classroom(Map&lt;String, String&gt; students, String grade, Optional&lt;String&gt; teacher) {
        this.students = students;
        this.grade = grade;
        this.teacher = teacher;
    }
}
class BiologyClassroom extends Classroom {
    public BiologyClassroom(Map&lt;String, String&gt; students, String grade, Optional&lt;String&gt; teacher) {
        super(students, grade, teacher);
    }
}

这是关于继承的 Java 教程链接。Inheritance (The Java&trade; Tutorials &gt; Learning the Java Language &gt; Interfaces and Inheritance)

英文:

In your interface you have declared students, grades, and teacher as methods.
I believe you are intending to use them as variables.

Just use basic inheritance; there is no need for an interface, per se.

class Classroom {
    enum Grade {
        Primary, Elementary
    }

    Map&lt;String, String&gt; students;
    String grade;
    Optional&lt;String&gt; teacher;

    public Classroom(Map&lt;String, String&gt; students, String grade, Optional&lt;String&gt; teacher) {
        this.students = students;
        this.grade = grade;
        this.teacher = teacher;
    }
}
class BiologyClassroom extends Classroom {
    public BiologyClassroom(Map&lt;String, String&gt; students, String grade, Optional&lt;String&gt; teacher) {
        super(students, grade, teacher);
    }
}

Here is the Java tutorial on inheritance.
Inheritance (The Java&trade; Tutorials &gt; Learning the Java Language &gt; Interfaces and Inheritance).

huangapple
  • 本文由 发表于 2023年7月10日 14:32:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定