【发布时间】:2020-04-16 08:41:07
【问题描述】:
谁能告诉我,我在构造函数内部使用“this”构造函数时犯了什么错误 公共学生()。请告诉我如何纠正它。编译器显示此错误 -
错误:(10, 11) java: com.shreyansh.Student 类中的构造函数 Student 不能应用于给定类型; 必需:无参数 找到:int,java.lang.String 原因:实际参数列表和形式参数列表的长度不同
****此处显示代码****
package com.shreyansh;
import java.util.Scanner;
public class Student {
private int rno;
private String name;
public Student() {
this(0, "Not defined"); //what is the error in this line
}
public void enter() {
System.out.println("Enter name of the student - ");
Scanner scanner = new Scanner(System.in);
this.name=scanner.nextLine();
System.out.println("Enter the roll number - ");
this.rno=scanner.nextInt();
scanner.close();
}
public void show() {
System.out.println("The name of the student is - "+name);
System.out.println("And the roll number is - "+rno);
}
}
【问题讨论】:
-
在构造函数中调用
this(..)意味着你调用了另一个构造函数。但是您要调用的那个不存在。Student类没有将 int 和 String 作为参数的构造函数。 -
没有带两个参数的构造函数。你需要定义它。
标签: java constructor