[英]“Cannot Find Symbol” in Sub Class Java
I am trying to make a sub class named " public class Once " and am getting the error " Cannot Find Symbol " on the lines " return date ;" 我正在尝试创建一个名为“ public class Once ”的子类,并且在“ 返回日期 ”行中收到错误“ 找不到符号 ”。 and " return descript ;". 和“ 返回描述 ;”。 I know its probably something really stupid I am missing, but any help would be great. 我知道我可能真的缺少它,但任何帮助都会很棒。
Here is my code! 这是我的代码!
import java.util.*;
public class Once
{
public Once(String dateIn, String descripIn)
{
String date = dateIn;
String descrip = descripIn;
}
public String getDate()
{
return date;
}
public String getDescrip()
{
return descrip;
}
}
You do not have those set as fields . 您没有将其设置为字段 。 A field defines a specific attribute about an object. 字段定义有关对象的特定属性。
What you'll want to do is set them up as such: 您需要做的是将它们设置如下:
public class Once {
private String date;
private String descrip;
//initialize in constructor
public Once(String dateIn, String descripIn) {
date = dateIn;
descrip = descripIn;
}
//Add getters and setters.
}
You have defined date and descrip local in constructor. 您已经在构造函数中定义了日期并描述了本地。
It should be 它应该是
public class Once{
String date;
String descrip;
public Once(String dateIn, String descripIn)
{
date = dateIn;
descrip = descripIn;
}
// other methods
}
Those variables are only in the scope of the Once() method. 这些变量仅在Once()方法的范围内。 You need to declare them inside the class scope: 您需要在类范围内声明它们:
public class Once
{
String date, descrip;
// ...
}
日期和说明需要在类级别定义,而不是作为局部变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.