【发布时间】:2014-10-23 18:13:45
【问题描述】:
我正在尝试使用构造函数、修改器和访问器构建一个类。通过阅读书籍和在线阅读,我了解到您可以调用带参数或不带参数的构造函数。但是,我下面的情况似乎不起作用。我什至无法编译没有错误。它在我使用 student jane = new student("") 时有效。我做错了什么?
Devolution.java:6: cannot find symbol
symbol : constructor student()
location: class Governor
student jane = new student();
^
public class designers {
public static void main(String[] args) {
student jane = new student();
student smith = new student("jane", "36365355", "Nairobi", "Male");
System.out.println("Janes's Properties: "+jane.name() + " " + jane.nationalID() + " " + jane.county()+" "+jane.gender());
System.out.println("Smith's Properties: "+smith.name() + " " + smith.nationalID() + " " + smith.county()+" "+smith.gender());
}
}
其他代码如下
public class student {
//Private fields
private String name;
private String nationalID;
private String county;
private String gender;
//Constructor method
public student(String name, String nationalID, String county, String gender)
{
this.name = name;
this.nationalID = nationalID;
this.county = county;
this.gender = gender;
}
//Accessor for name
public String name()
{
return name;
}
//Accessor for nationalID
public String nationalID()
{
return nationalID;
}
//Accessor for county
public String county()
{
return county;
}
//Accessor for gender
public String gender()
{
return gender;
}
}
【问题讨论】:
-
请注意,Java 中的类名应以大写字母开头。
-
在问题中格式化代码时,使用代码示例按钮或将代码缩进 4 个空格。 sn-ps 按钮只能用于 js、html 和/或 css。
-
谢谢@JamesMontagne。确实很有帮助。信息量太大了。
-
非常感谢大家。我想我喜欢 Java。那里有很多知识,很有帮助,很中肯!
标签: java