【发布时间】:2012-08-30 03:40:59
【问题描述】:
显然我的 Java 暂停时间太长了……
我有以下课程:
public class TimeLine {
public static final String TIME_LINE_DATE_FORMAT = "dd.MM.yyyy";
public TimeLine(Context context, LinearLayout layout)
{
this.context = context;
this.layout = layout;
}
// some methods and stuff
public static Date getDateFromString(String dateString)
{
SimpleDateFormat s = new SimpleDateFormat(TIME_LINE_DATE_FORMAT);
try {
return s.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
}
我经常使用将字符串解析为日期,这就是为什么我希望这个函数只使用 1 次并且是静态的。
我尝试这样访问它:
public class TrackedValue {
private double value;
private String unit;
private Date date;
public TrackedValue()
{
}
public TrackedValue(Date date, String unit, double value)
{
this.date = date;
this.unit = unit;
this.value = value;
}
public TrackedValue(String dateString, String unit, double value)
{
this.date = TimeLine.getDateFromString(dateString); //Here's the error
this.unit = unit;
this.value = value;
}
// some getters and setters here
}
这给我带来了错误: 方法 getDateFromString(String) 没有为类型时间线定义
呃……为什么?
【问题讨论】:
-
我的猜测:你指的不是你认为的
TimeLine。 -
或者可能是
TimeLine没有编译 -
TimeLine 的方式不是编译。除此之外,代码没有其他问题。调用静态方法就好了。
-
TrackedValue.java 文件中的导入语句对于 Timeline 包是什么样的?
标签: java methods static constructor undefined