类中的java构造函数不能应用于给定的类型(java constructor in class cannot be applied to given types)

编程入门 行业动态 更新时间:2024-10-23 01:34:17
类中的java构造函数不能应用于给定的类型(java constructor in class cannot be applied to given types)

我有2个子类:工作人员,学生他们属于超级人物。

这是我的老师给出的代码(任务):


public class Person { private String name; private int yearOfBirth; /** * Create a person with given name and age. */ Person(String name, int yearOfBirth) { this.name = name; this.yearOfBirth = yearOfBirth; } }
class Student extends Person { private String SID; // student ID number /** * Create a student with no parameters. */ Student() { //task. } }
public class Staff extends Person { private String roomNumber; /** * Construct a staff member with field values and no pamaeters. */ public Staff() { //task } }

我不知道我能输入什么才能创建一个没有参数的对象。 它总是出现如下错误:构造函数类Person中的Person不能应用于给定的类型; required:java.lang.String,int;

我在网上查了一下,有两种方法可以解决这个问题:

在超类中添加一个默认值:Person()//不带参数。 在学生的子类中:

Student() { Person astudent = new Student() //I guess. }

在子类中添加一个super(): Student() { super("xxx")//I guess. } Student() { super("xxx")//I guess. }

我不知道该怎么办。 我是学习BlueJ的入门者。 希望任何人都可以帮助我。 非常感谢你。

I have 2 subclasses: Staff, Student they belong to superclass Person.

Here is the code(tasks) which is given by my teacher:


public class Person { private String name; private int yearOfBirth; /** * Create a person with given name and age. */ Person(String name, int yearOfBirth) { this.name = name; this.yearOfBirth = yearOfBirth; } }
class Student extends Person { private String SID; // student ID number /** * Create a student with no parameters. */ Student() { //task. } }
public class Staff extends Person { private String roomNumber; /** * Construct a staff member with field values and no pamaeters. */ public Staff() { //task } }

I don't know what can I type in order to create an object without parameter. It always appears an error like: constructor Person in class Person cannot be applied to given types; required: java.lang.String,int;

I have checked online that there are 2 ways to solve the problem:

add a default value in the superclass: Person()//without parameter. In the subclass Student:

Student() { Person astudent = new Student() //I guess. }

add a super() in the subclass: Student() { super("xxx")//I guess. }

I don't know what to do. I an a starter in learning BlueJ. Hope anyone can help me. Thank you very much.

最满意答案

由于您的超类Person没有默认构造函数,因此在您的子类( Student和Staff )中,必须调用超类构造函数作为第一条语句。

你应该像这样定义你的子类构造函数:

Student() { super("a_string_value", an_int_value);// You have to pass String and int values to super class }
Staff() { super("a_string_value", an_int_value); // You have to pass String and int values to super class }

Since your super class Person doesn't have a default constructor, in your sub classes (Student and Staff), you must call the super class constructor as the first statement.

You should define your sub class constructors like this:

Student() { super("a_string_value", an_int_value);// You have to pass String and int values to super class }
Staff() { super("a_string_value", an_int_value); // You have to pass String and int values to super class }

更多推荐

本文发布于:2023-07-25 19:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1265207.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用于   函数   类中   类型   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!