码迷,mamicode.com
首页 > 其他好文 > 详细

接口和comparable、comparator。

时间:2017-06-10 21:39:04      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:test   gets   get   err   思路   tag   usb   打印机   构造   


*接口定义:使用interface关键字

* [修饰符] interface 接口名 [extends 父接口1,父接口2...]{
* //常量的声明
* //方法的声明
* }
*接口成员的特点:
*1.接口中所有的成员变量是常量,默认修饰符为public static final
*2.接口中所有的方法都是抽象方法,默认修饰符为:public abstract
*3.接口不能实例化,接口不能构造方法(抽象类不能实例化,但可以有构造方法)
*4.java类是单继承,但接口可以是多继承
*5.一个非抽象类实现实现接口,必须重写接口中的抽象方法,抽象类实现接口可以重写部分抽象方法。
*6.一个类只能继承一个父类,但可以实现多个接口
*
*如果一个类继承父类并实现了接口如何编写?
* [修饰符] class 类名 [extends 父类名 implements 接口名1,接口名2..]{
* }

 

public interface USB {
void work();
}

 

public class Printer implements USB {

@Override
public void work() {
System.out.println("我是打印机,我能打印资料....");
}

}

 

public class Keyborad implements USB{

@Override
public void work() {
System.out.println("我是机械键盘,我能打字....");
}


}

 

public class Mouse implements USB {

@Override
public void work() {
System.out.println("我是鼠标,我能玩游戏!");
}

}

public class Test {
public void useUSB(USB usb){
System.out.println("请向USB接口插入设备:");
usb.work();
}

public static void main(String[] args) {
Test test = new Test();
test.useUSB(new Mouse());
System.out.println("---------------");
test.useUSB(new Keyborad());
System.out.println("---------------");
test.useUSB(new Printer());
}
}

comparator:比较器。

public class Student {
private String name;
private int age;
private int score;
public Student(String name, int age, int score) {
super();
this.name = name;
this.age = age;
this.score = score;
}
public Student() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", score=" + score + "]";
}



}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

java.util.Comparator接口:比较器
* int compare(Object o1, Object o2):比较用来排序的两个参数。
* 根据第一个参数小于、等于或大于第二个参数分别返回负整数、零或正整数。
*/
public class ScoreComparator implements Comparator{

/**
* 定义比较规则:按照分数的降序进行排列
*/
@Override
public int compare(Object o1, Object o2) {
Student stu1=null;
Student stu2=null;
if(o1 instanceof Student){
stu1=(Student)o1;
}
if(o2 instanceof Student){
stu2=(Student)o2;
}
// if(stu1.getScore()>stu2.getScore()){
// return 1;
// }else if(stu1.getScore()==stu2.getScore()){
// return 0;
// }else{
// return -1;
// }
return -(stu1.getScore()-stu2.getScore());
}

}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

ava.util.Arrays类
* sort(Object[] objs,Comparator c):按照指定的比较器对指定数组进行排序。
*
*/
public class TestArrays {
public static void main(String[] args) {
Student[] stus = new Student[5];//保存学生信息的数组
stus[0]=new Student("aa",20,80);
stus[1]=new Student("bb",22,78);
stus[2]=new Student("cc",18,90);
stus[3]=new Student("dd",25,82);
stus[4]=new Student("ee",24,81);
System.out.println("排序前:");
for (Student student : stus) {
System.out.println(student);
}
Arrays.sort(stus, new ScoreComparator());//利用指定的比较器完成比较
System.out.println("排序后:");
for (Student student : stus) {
System.out.println(student);
}

}
}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

comparable:

*java.util.Arrays类
*sort(Object[] objs):根据元素的自然顺序对指定对象数组按升序进行排序。数组中的所有元素都必须实现 Comparable接口
*
*/
public class TestArrays {
public static void main(String[] args) {
Student[] stus = new Student[5];//保存学生信息的数组
stus[0]=new Student("aa",20,80);
stus[1]=new Student("bb",22,78);
stus[2]=new Student("cc",18,90);
stus[3]=new Student("dd",25,82);
stus[4]=new Student("ee",24,81);
System.out.println("排序前:");
for (Student student : stus) {
System.out.println(student);
}
Arrays.sort(stus);
System.out.println("排序后:");
for (Student student : stus) {
System.out.println(student);
}
}
}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

java.lang.Comparable接口:此接口强行对实现它的每个类的对象进行整体排序。
* 排序规则在compareTo()方法中定义。
* 当调用java.util.Arrays类中sort(Object[] objs)时,
* 程序会调用compareTo()方法对对象进行比较,
* 如果该方法返回正整数(1)时,代表当前对象大于待比较对象;
* 如果返回0,代表当前对象等于待比较对象
* 如果该方法返回负整数(-1)时,代表当前对象小于待比较对象;
*实现思路:
*1.实现Comparable接口,并重新其compareTo方法
*2.在compareTo方法中定义比较规则。返回值应该是正整数,零和负整数。
*3.在调用Arrays.sort(Object[] objs)方法的过程中,sort方法的内部对调用compareTo方法进行比较。
*
*/
public class Student implements Comparable{
private String name;
private int age;
private int score;
public Student(String name, int age, int score) {
super();
this.name = name;
this.age = age;
this.score = score;
}
public Student() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", score=" + score + "]";
}
/**
* 制定比较规则:
* compareTo(Object o):比较当前对象与指定对象o之间的关系。
* 如果当前对象大于指定对象o返回值是正整数
* 如果当前对象等于指定对象o返回值是零
* 如果当前对象小于指定对象o返回值是负整数
*/

@Override
public int compareTo(Object o) {
Student stu = (Student)o;
// if(age>stu.getAge()){
// return 1;
// }else if(age==stu.getAge()){
// return 0;
// }else{
// return -1;
// }
return (age-stu.getAge());
}


}

接口和comparable、comparator。

标签:test   gets   get   err   思路   tag   usb   打印机   构造   

原文地址:http://www.cnblogs.com/benpaozhimeng/p/6979798.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!