这个显示时间的java程序错在哪,编辑有三个错

来源:百度知道 编辑:UC知道 时间:2024/06/05 11:00:46
import java.util.*;
class now
{
public now()
{
Date now=new Date();
}
String getD()
{
return now.getDate().toString();
}
public static void main(String[] args)
{
System.outprintln("time is:"+getD());
}
}

E:\java>javac now.java
now.java:10: cannot resolve symbol
symbol : method getDate ()
location: class now
return now.getDate().toString();
^
now.java:14: non-static method getD() cannot be referenced from a static context

System.outprintln("time is:"+getD());
^
now.java:14: cannot resolve symbol
symbol : method outprintln (java.lang.String)
location: class java.lang.System
System.outprintln("time is:"+getD());
^
3 errors

谢谢<

1、now这个对象,你是在now()这个构造方法里声明的,也就是说它的作用范围只在这个方法中,而你是在getD() 中又使用了这个变量now.getDate().toString();这个时候编译就提示你第10行出错
2、java中,用static声明的方法是静态方法,只能调用和它相同的静态方法,而你的 getD() 是一个非静态的方法,用main()是不能调用它的,所以第14行出错
3、这个错误是你打错了System.outprintln("time is:"+getD());
你少打了一个”.”,应该是System.out.println("time is:"+getD());

import java.util.*;
class Now
{
private static Date now ;
public now()
{
now=new Date();
}
public static String getD()
{
return now.getDate+"" ;
}
}
class Test
{
public static void main(String[] args)
{
System.out.println("time is:"+getD());
}
}

不知道这样写,和不和你的要求

补充问题回答
1。你无法从方法之外访问方法内部的局部变量
2。可以
3。如下

import java.util.*;

class Now {
private Date now;

public Now() {
now = new Dat