昏黄烛火轻摇晃 2017-07-17 10:44 采纳率: 61.5%
浏览 759
已结题

JAVA this问题 求详细解答

compareTo()方法中的 if(this.getAge()>stu.getAge())  的this.getAge()指的是?实例化对象传入的参数不都是stu.getAge() 我想了好久都不知道这个this表示什么

import java.util.Arrays;

import java.util.Comparator;

public class Student implements Comparable{

//就简单点按年龄大小排序

private int age;

Student(){

    this.age = 0;

}

Student(int age){

    this.age = age;

}

public int getAge() {

    return age;

}



public void setAge(int age) {

    this.age = age;

}



@Override

public int compareTo(Student stu) {

    if(this.getAge()>stu.getAge()){

        return 1;

    }else if(this.getAge()<stu.getAge()){

        return -1;

    }else{

        return 0;   

    }

}

public static void main(String[] args) {

    Student stu[] = new Student[3];

    stu[0] = new Student(2);

    stu[1] = new Student(1);

    stu[2] = new Student(3);



    Arrays.sort(stu);
  • 写回答

3条回答 默认 最新

  • SonOfWind0311 2017-07-18 00:50
    关注

    这个很好理解吧, compareTo的用法一般就是这样:
    Student stu1 = new Student(3);
    Student stu2 = new Student(5);

    stu1.compareTo(stu2); // 对应代码: this就是对象stu1, 参数stu就是对象stu2

    评论

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功