Hi All!我有一个问题,我要按照一个class的API写这个class,这个class
Implemented Interfaces:
java.lang.Comparable 其中有一个method是这样的:public int compareTo(java.lang.Object other)Compares this object with the specified object for order. Returns a negative, zero, or positive integer based on whether this object is less than, equal to, or greater than the specified object. Specified by:
compareTo in interface java.lang.Comparable
Parameters:
other - the Object to compare to.
Returns:
the same return as that of compareTo(String) in String when invoked on the title of this book.这个method应该怎么写的?我的问题在于不太明白如何在这个class里使用这个interface。我感觉好像是在这个method里使用Interface java.lang.Comparable 来compare两个string
请详细指点,谢谢。 简单说,就是我还不太明白interface是怎么回事,还有如何使用。
我的QQ 314386
MSN [email protected]
Implemented Interfaces:
java.lang.Comparable 其中有一个method是这样的:public int compareTo(java.lang.Object other)Compares this object with the specified object for order. Returns a negative, zero, or positive integer based on whether this object is less than, equal to, or greater than the specified object. Specified by:
compareTo in interface java.lang.Comparable
Parameters:
other - the Object to compare to.
Returns:
the same return as that of compareTo(String) in String when invoked on the title of this book.这个method应该怎么写的?我的问题在于不太明白如何在这个class里使用这个interface。我感觉好像是在这个method里使用Interface java.lang.Comparable 来compare两个string
请详细指点,谢谢。 简单说,就是我还不太明白interface是怎么回事,还有如何使用。
我的QQ 314386
MSN [email protected]
public Employee(String n, double s, int year, int month, int day) {
name = n;
salary = s;
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
hireday = calendar.getTime();
}
public int compareTo(Object otherObject) {
Employee other = (Employee) otherObject;
if (salary < other.salary) {
return -1;
}
if (salary > other.salary) {
return 1;
}
return 0;
} private static String name;
private double salary = 100;
private static Date hireday;
} 这是我练习用的小例子,希望对你有帮助,我也是初学者在这个method里使用Interface java.lang.Comparable 来compare两个string,不是这样的是在你写得类中实现compareTo这个方法,借口中只有方法的定义,并没有方法的实现,
在继承他的类中要实现借口中的所有方法,否则好像要抛什么异常,学得不好,我也记不住了
如果此时我要让类B即具有类A的功能,又想类B具有类C的功能,我要怎么办呢,最好的方法就是接口。我可以把类C换成为接口。在类B中使用extends A implement C。这样的话类B就同时具有了类A、C的功能。如果你要用继承机制的话,是无法实现的。所以用接口实现的对象可以不受个数的限制而实现多个类的功能
interface的话,不需要产生对象就可以调用,而且可以多重“继承”,前提是你必须覆写base的所有方法
还有一个问题,哪里可以找到详细的javadoc的说明
Stuendt st1=new Stuendt();
Stuendt st2=new Stuendt();
al.add(st1);
al.add(st2);
Collections.sort(al);
class Student implements Compareable
{
.............
public int compareTo(Object o)
{
Student s=(Student)o;
return num > s.num ? 1 : (num==s.num ? 0 : -1);
}
}
什么时候可以排序al呢?资料上说它里面的元素需实现了compareable接口。我再这里形象的说一下:)
当Collections排序时,它把al里面的每个对象插上compare接口,相当于一个电线板(al)上的插孔(compareable)插了许多用电器(object)的插座(compareable)。然后这个电线板聪明的通过一块智能芯片(Collections.sort())把这些用电器按照功率(自定义比较大小的方法)进行了一个排序,然后它就会按从小到大顺序自动把每个插座重新插一遍,就这么简单。
所以按照这个例子来讲,compareable接口一头是collections,一头是objects。
在collections里,sun公司在compareable内部编写了一个compareable方法,希望程序员能用。在这个方法里,都是相互比较1,0,-1这些值来比较大小的。为此,sun公司又写了一个compareable接口。这样,程序员只要自己设计的类插上(实现)这个接口,就能按照自己的意愿编写compareTo这个方法(通过返回-1,0,1这些值)来达到比较的目的。
ps:虽然接口里的compareto具体方法为空,但各自的比较方案都在里面写了。student里的结果纯粹是1,-1,0,然后collections调用sort依据它内部的compareto方法(当然,比如里面肯定有什么1就是大,0就是相等之类的解释)来比较大小进行排序