public interface Type{ static int string = 1; static int date = 2; }public Date getCurrentTime(Object arg1, int type) public String getCurrentTime(Object arg1, int type)
若楼主的函数原型确实是这样: class MyClass { public Date getCurrentTime(); public String getCurrentTime(); } 根据Sun的Java规范,这是100%错误的, 两个成员函数的参数的类型必须不同才可以。 楼主的问题涉及到OOP中最根本的概念问题,建议楼主多看一看<Thinking in Java>这本书.
public Date getCurrentTime() public String getCurrentTime() 这样写是错误的. overload是指在同一类中,参数可以不一样,但返回类型必须一样,你一个返回Date 而另一个返回String,是不行的.试想,java当中允许invoke 某一个方法而不用任何变量去接受,比如调用的时候就直接 getCurrentTime(); 这个时候你让编译器去调用哪一方法呢?
public Date getCurrentTime() public String getCurrentTime(int i) 这样就OK了。
这样试试吧,不知道行不行? import java.util.*; public class test { public static Object getMyTime(int type) { if(type==1) return new String("abcdefg"); if(type==2) return new Date(); return null; } public static void main(String args[]) { System.out.println(getMyTime(1)); System.out.println(getMyTime(2)); } }
我觉得楼主的想法在java中是无法实现的
这是JAVA的规则,当然是不能实现,你要用它,就得服从它,否则自己搞一个
若楼主想要完成与此相似的方法如下应该可以: public void getCurrentTime(Date ) public void getCurrentTime(String)
static int string = 1;
static int date = 2;
}public Date getCurrentTime(Object arg1, int type)
public String getCurrentTime(Object arg1, int type)
class MyClass {
public Date getCurrentTime();
public String getCurrentTime();
}
根据Sun的Java规范,这是100%错误的, 两个成员函数的参数的类型必须不同才可以。
楼主的问题涉及到OOP中最根本的概念问题,建议楼主多看一看<Thinking in Java>这本书.
public String getCurrentTime() 这样写是错误的.
overload是指在同一类中,参数可以不一样,但返回类型必须一样,你一个返回Date
而另一个返回String,是不行的.试想,java当中允许invoke
某一个方法而不用任何变量去接受,比如调用的时候就直接 getCurrentTime();
这个时候你让编译器去调用哪一方法呢?
public String getCurrentTime(int i)
这样就OK了。
import java.util.*;
public class test
{
public static Object getMyTime(int type)
{
if(type==1)
return new String("abcdefg");
if(type==2)
return new Date();
return null;
}
public static void main(String args[])
{
System.out.println(getMyTime(1));
System.out.println(getMyTime(2));
}
}
public void getCurrentTime(Date )
public void getCurrentTime(String)