hooyantsing's Blog

第33次课程

字数统计: 3.7k阅读时长: 19 min
2019/07/10

源辰74班

第33次课程

07.10.2019

**内容    **

*Java_API_java.util包[蒋斌]*

**02ce28ee4e1ba62bd8bf49b20f2ef224.png

**

0.<补丁包>

0.1Jar包的导入

对第32次课程(上一节课)的补丁包。

1.将“Jar包(log4j-1.2.15)”复制,在“Java项目(Java_API_lang)”右键选择粘贴。

cd71a503d00f0dfbeecd7ba9964ae2f2.png

2.将“配置文件(log4j)”复制,在“src”右键选择粘贴。

3daf7e28c2e6f5392c24d87dcfcfccb0.png

配置文件源码:

日志级别:****debug<INFO<WARN<ERROR<PATAL(致命错误)

1
2
3
4
5
6
7
8
9
10
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out                
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}  %m%n
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=yc.log
log4j.appender.file.Threshold=debug
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}  %l  %m%n
log4j.rootLogger=debug, stdout,file

例题分析:

功能:用于将错误信息存放入文本文件(.txt)中,生成错误和异常日志。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import org.apache.log4j.Logger;
public class Logger_Test {
    public static void main(String[] args) {
       //创建logger对象
       Logger logger =  Logger.getLogger(Logger_Test.class);
       //日志级别:
       //debug<INFO<WARN<ERROR<PATAL(致命错误)
       try {
           int result = 1/0;
       }catch(Exception e) {
           logger.debug(e.getMessage(),e);
       }
    }
}

例题结果:

1.生成目标文件:

86933ccfd0d3d0a879bacc8838517ae4.png

2.生成错误和错误日志(报告):

c415d5f79380a8fe1d5fa2af8cf8abf9.png

0.2[案例]打字游戏

对第31次课程作业的补丁包。

获取1997.01.01 00:00至现在时刻,已经流逝的时间,单位毫秒

1
long currentTime = System.currentTimeMillis();

老师标准代码:

Class Game

1
2
3
4
5
6
7
8
9
10
package com.yc.typingmaster;
/**
* 玩家玩游戏。
*/
public class Test {
    public static void main(String[] args) {
       Player player = new Player();
       player.play();
    }
}

Class Level

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.yc.typingmaster;
/**
* 玩家级别。
*/
public class Level {
    private int levelNo; // 级别号
    private int strLength; // 各级别一次输出字符串的长度
    private int strTime; // 各级别输出字符串的次数
    private int timeLimit; // 各级别闯关的时间限制
    private int perScore; // 各级别成功输入一次字符串后增加的分值
    public Level(int levelNo, int strLength,  int strTime, int timeLimit,
           int perScore) {
       this.levelNo = levelNo;
       this.strLength = strLength;
       this.strTime = strTime;
       this.timeLimit = timeLimit;
       this.perScore = perScore;
    }
    public int getLevelNo() {
       return levelNo;
    }
    public int getStrLength() {
       return strLength;
    }
    public int getStrTime() {
       return strTime;
    }
    public int getTimeLimit() {
       return timeLimit;
    }
    public int getPerScore() {
       return perScore;
    }
}

Class LevelParam

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.yc.typingmaster;
/**
* 级别参数类,配置各个级别参数。
*/
public class LevelParam {
    public final static Level levels[]=new  Level[6];//对应六个级别
    static {
       levels[0]=new Level(1, 2, 10, 30,1);
       levels[1]=new Level(2, 3, 9, 26,2);
       levels[2]=new Level(3, 4, 8, 22,5);
       levels[3]=new Level(4, 5, 7, 18,8);
       levels[4]=new Level(5, 6, 6, 15,10);
       levels[5]=new Level(6, 7, 5, 12,15);
    }
}

Class Player

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.yc.typingmaster;
import java.util.Scanner;
/**
* 玩家类。
*/
public class Player {
    private int levelNo; // 级别号
    private int curScore; // 当前积分
    private long startTime = 0; // 各级别开始时间
    private int elapsedTime; // 各级别已用时间
    public long getStartTime() {
       return startTime;
    }
    public void setStartTime(long startTime)  {
       this.startTime = startTime;
    }
    public int getLevelNo() {
       return levelNo;
    }
    public void setLevelNo(int levelNo) {
       this.levelNo = levelNo;
    }
    public int getCurScore() {
       return curScore;
    }
    public void setCurScore(int curScore) {
       this.curScore = curScore;
    }
    public int getElapsedTime() {
       return elapsedTime;
    }
    public void setElapsedTime(int  elapseTime) {
       this.elapsedTime = elapseTime;
    }
    /**
     * 玩家玩游戏。
     */
    public void play() {
       Game game = new Game(this);
       Scanner input = new  Scanner(System.in);
       // 外层循环,循环一次级别晋一级
       for (int i = 0; i <  LevelParam.levels.length; i++) {
           // 1、 晋级[从一级开始玩....]
           this.levelNo += 1;
           // 2、 晋级后计时清零,积分清零
           this.startTime =  System.currentTimeMillis();
           this.curScore = 0;
           // 3、 内层循环,循环一次完成一次字符串的输出、输入、比较  [每个级别需要玩的次数(总的时间是否少于通关时间)....]
           for (int j = 0; j <  LevelParam.levels[levelNo-1].getStrTime();  j++) {
               // 3.1、游戏输出字符串
               String outStr =  game.printStr();
               // 3.2、接收用户输入
               String inStr = input.next();
               // 3.3、游戏判断玩家输入是否正确,并输出相应结果信息
               game.printResult(outStr,  inStr);
           }
       }
    }
}

Main Test

1
2
3
4
5
6
7
8
9
10
package com.yc.typingmaster;
/**
* 玩家玩游戏。
*/
public class Test {
    public static void main(String[] args) {
       Player player = new Player();
       player.play();
    }
}

自己代码:

Class Game

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.hooy.typingmaster;

import java.util.Random;
import java.util.Scanner;

public class Game {
    private Player player;
    private String[] str = {"a","b","c","d","e","f","g","h","i","j","k","l","m",
                            "n","o","p","q","r","s","t","u","v","w","x","y","z"};
    private Random r = new Random();    
    private Scanner sc = new Scanner(System.in);
    public Game(Player player) {
        super();
        this.player = player;
    }
    
    public String printStr() {
        String out = "";
        int strNumber;
        for(int i=0;i<LevelParam.level[player.getLevelNo()-1].getStrLength();i++) {
            strNumber = r.nextInt(str.length);
            out += str[strNumber];
        }
        //输出字符串,返回字符串用于和玩家输入比较
        System.out.println(out);
        return out;
    }
    
    public String inStr() {
        System.out.println("现在开始打字:");
        return sc.nextLine();
    }
    
    public void printResult(String out,String in) {
        if(out.equals(in)) {
            //输出和输入相同
            long currentTime = System.currentTimeMillis();
            //检查是否超时
            if((currentTime-player.getStartTime())/1000>
                LevelParam.level[player.getLevelNo()-1].getTimeLimit()) {
                System.out.println("用时:"+((currentTime-player.getStartTime())/1000)+",已超时");
                System.exit(1);
            }else {
                //未超时
                //计算积分
                player.setCurrScore(player.getCurrScore()+
                        LevelParam.level[player.getLevelNo()-1].getPerScore());
                //计算时间
                player.setElapsedTime((int)(currentTime-player.getStartTime())/1000);
                //汇总输出
                System.out.println("输入正确,您的积分为:"+player.getCurrScore()+",等级为:"+
                player.getLevelNo()+",已用时间:"+player.getElapsedTime()+"秒");
            }
        }else {
            //输出和输入不相同
            System.out.println("测试:输入错误");
            System.exit(1);
        }
    }
}

Class Level

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.hooy.typingmaster;
public class Level {
    private int levelNo;  //各级别号
    private int strLength;//各级别一次输出字符串的长度
    private int strTime;  //各级别输出字符串的次数
    private int timeLimit;//各级别闯关的时间限制
    private int perScore; //各级别正确输入一次的得分
    public Level() {
       super();
    }
    public Level(int levelNo, int strLength,  int strTime, int timeLimit, int perScore) {
       super();
       this.levelNo = levelNo;
       this.strLength = strLength;
       this.strTime = strTime;
       this.timeLimit = timeLimit;
       this.perScore = perScore;
    }
    public int getLevelNo() {
       return levelNo;
    }
    public void setLevelNo(int levelNo) {
       this.levelNo = levelNo;
    }
    public int getStrLength() {
       return strLength;
    }
    public void setStrLength(int strLength)  {
       this.strLength = strLength;
    }
    public int getStrTime() {
       return strTime;
    }
    public void setStrTime(int strTime) {
       this.strTime = strTime;
    }
    public int getTimeLimit() {
       return timeLimit;
    }
    public void setTimeLimit(int timeLimit)  {
       this.timeLimit = timeLimit;
    }
    public int getPerScore() {
       return perScore;
    }
    public void setPerScore(int perScore) {
       this.perScore = perScore;
    }
}

Class LevelParam

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.hooy.typingmaster;
public class LevelParam {
    public final static Level[] level = new  Level[6];
    
    static {
       level[0] = new Level(1,2,10,30,1);
       level[1] = new Level(2,3,9,26,2);
       level[2] = new Level(3,4,8,22,5);
       level[3] = new Level(4,5,7,18,8);
       level[4] = new Level(5,6,6,15,10);
       level[5] = new Level(6,7,5,12,15);
    }
}

Class Player

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.hooy.typingmaster;
public class Player {
    private int levelNo;      //玩家当前级别号
    private int currScore;    //玩家当前级别积分
    private long startTime;       //当前级别开始时间
    private int elapsedTime;  //当前级别已用时间
    public Player() {
       super();
    }
    public Player(int levelNo, int  currScore, int startTime, int elapsedTime) {
       super();
       this.levelNo = levelNo;
       this.currScore = currScore;
       this.startTime = startTime;
       this.elapsedTime = elapsedTime;
    }
    public int getLevelNo() {
       return levelNo;
    }
    public int getCurrScore() {
       return currScore;
    }
    public long getStartTime() {
       return startTime;
    }
    public int getElapsedTime() {
       return elapsedTime;
    }
    public void setLevelNo(int levelNo) {
       this.levelNo = levelNo;
    }
    public void setCurrScore(int currScore)  {
       this.currScore = currScore;
    }
    public void setStartTime(int startTime)  {
       this.startTime = startTime;
    }
    public void setElapsedTime(int  elapsedTime) {
       this.elapsedTime = elapsedTime;
    }
    
    public void play() {
       Game game = new Game(this);
       for(int  i=0;i<LevelParam.level.length;i++) {
           this.levelNo = i+1;
           //分数清零
           this.currScore = 0;
           //时间清零
           this.startTime =  System.currentTimeMillis();
           //出题
           for(int  j=0;j<LevelParam.level[this.levelNo].getStrTime();j++) {   
               game.printResult(game.printStr(),game.inStr());
           }
       }
       System.out.println("恭喜你通关游戏!");
    }
}

Main Test

1
2
3
4
5
6
7
package com.hooy.typingmaster;
public class Test {
    public static void main(String[] args) {
       Player player = new Player();
       player.play();
    }
}

1.Class类

导入Class文件:Class.forName(“hooy.java.lang.Person”)

获取Class中属性相关内容:Class.getDeclaredFields()

获取Class中方法相关内容:Class.getDeclaredMethods()

获取属性/方法类型:Field.getType()   Method.getType()

获取属性/方法名称:Field.getName()   Method.getName()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Class_FieldAndMethod {
    public static void main(String[] args)  throws ClassNotFoundException {
       //找到目标类
       Class clzz =  Class.forName("hooy.java.lang.Person");
       //将获取的类属性信息放入Field类型数组中
       Field[] fields =  clzz.getDeclaredFields();
       for (Field field : fields) {
           //获取属性类型和属性名
           System.out.println(field.getType()+"=="+field.getName());
       }
       //将获取的类方法信息放入Method类型数组中
       Method[] methods =  clzz.getDeclaredMethods();
       for (Method method : methods) {
           System.out.println(method.getName());
       }
    }
}

2.equals和hashCode方法重写

885dbacd7b3c6c60583f50d685394dbd.png

equals方法:指示其他某个对象是否与此对象“相等”。

hashCode方法:返回该对象的哈希码值。

例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//重写equals方法
    @Override
    public boolean equals(Object obj) {
       if(obj==null) {
           return false;
       }
       if(!(obj instanceof Person)) {
           return false;
       }
       Person p = (Person)obj;
       //比较hashCode值
       if(this.hashCode()==p.hashCode()) {
           return true;
       }
       return false;
    }
//重写hashCode方法,其中算法仅是体现过程
    @Override
    public int hashCode() {
       int result = 0;
       int prime = 31;
       result = prime *  (name==null?0:name.hashCode());
       result = prime * result +  (idcode==null?0:idcode.hashCode());
       return result;
    }

3.Object.clone方法

例题分析:

超类protected用于修饰方法,表示子类需要重写该方法,重写方法中也可以通过super.继承

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class Object_Test {
    public static void main(String[] args)  throws CloneNotSupportedException {
       Student s = new Student("张三");
       Student clone = (Student)s.clone();
       System.out.println(s==clone);
       System.out.println(clone.getName());
    }
}
class Student extends Object{
    private String name;
    public Student(String name) {
       super();
       this.name = name;
    }
    public String getName() {
       return name;
    }
    public void setName(String name) {
       this.name = name;
    }
    //protected用于修饰方法,表示子类需要重写该方法,也可以通过super.继承
    //返回与同一个对象(不同变量引用同一个对象)
    @Override
    protected Object clone() throws  CloneNotSupportedException{
       return this;
    }
    /*
    //返回克隆的一个新对象(不同变量引用不同对象)
    @Override  //<-安全机制
    protected Object clone() throws  CloneNotSupportedException{
       return new Student(this.name);
    }
    */
    /*
    //父类clone方法不支持
    @Override
    protected Object clone() throws  CloneNotSupportedException{
       return super.clone();
    }
    */
}

4.Object.toString方法

41a212a3f1940098c502a1362a3f880c.png

例题分析:

1
2
3
4
@Override
    public String toString() {
       return "PersonTest [name=" + name +  ", idcode=" + idcode + "]";
    }

5.Date类和Calendar类

已过时的日期类(Date),通常被日历类(Calendar)取代。

例题分析:

Date类获取当前时间:Date date = new Date()

Date类定义日期格式:DateFormat df = new  SimpleDateFormat(“yyyy年MM月dd日 HH:mm:ss”)

Calendar类获取当前时间:Calendar c = Calendar.getInstance()

Calendar类使用关键字获取值:Calendar.get(Calendar.VALUE)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class DateAndCalendar_Test {
    public static void main(String[] args) {
       //Date
       //获取当前时间
       Date date = new Date();
       //自定义日期格式
       DateFormat df = new  SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
       String format = df.format(date);
       System.out.println(format);
       
       //Calendar(日历)
       Calendar c = Calendar.getInstance();
       System.out.println(c);
       //字段选取
       System.out.println("年:"+c.get(Calendar.YEAR));
       System.out.println("月:"+c.get(Calendar.MONTH));
       System.out.println("日:"+c.get(Calendar.DATE));
       System.out.println("周:"+c.get(Calendar.DAY_OF_WEEK));
    }
}

6.Random类

常用于产生随机数。

例题分析:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Random_Test {
    public static void main(String[] args) {
       Random r = new Random();
       int count = 0;
       int flag = 0;
       StringBuffer sb = new  StringBuffer();
       while(count<5) {
           flag = r.nextInt(2);
           if(flag==0) {
               sb.append(r.nextInt(10));
           }
           if(flag==1) {
               sb.append((char)(r.nextInt(26)+97));
           }
           count++;
       }
       System.out.println(sb.toString());
    }
}

7.ArrayList类

数据结构:顺序存储结构

通过数组实现

例题分析:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class MyList<T> {
    private Object[] data;
    private int size;
    public MyList() {
       this(10);
    }
    public MyList(int len) {
       if(len<=0) {
           throw new  IllegalArgumentException("参数异常");
       }
       //申请所需空间
       data = new Object[len];
    }
    
    //扩容
    public void capactiy(int newSize) {
       if(size<newSize) {
           Object[] new_obj = new  Object[newSize*2];
           //原数组 从下标开始位置 新数组 从下标开始位置 复制多少
           System.arraycopy(data, 0,  new_obj, 0, size);
           data = new_obj;
       }
    }
    
    public boolean add(T obj) {
       //判断是否扩容
       capactiy(size+1);
       data[size++] = obj;
       return true;
    }
    
    public int getSize() {
       return size;
    }
    
    public void indexCheck(int i) {
       if(i<0 || i>=size) {
           throw new RuntimeException("索引值不合法");
       }
    }
    
    public T get(int i) {
       indexCheck(i);
       return (T)data[i];
    }
    
    public static void main(String[] args) {
       MyList<String> list = new  MyList<String>();
       list.add("张三");
       list.add("李四");
       list.add("王五");
       for(int i=0;i<list.getSize();i++) {
           System.out.println(list.get(i));
       }
    }
}

8.LinkedList类

数据结构:链式存储结构

通过链(地址)实现

例题分析:

Class MyNode

相当于C/C++中通过结构体创建节点类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class MyNode<E> {
    //节点成员
    E item;
    MyNode<E> next;
    
    public MyNode() {
       super();
    }
    public MyNode(E item, MyNode<E> next) {
       super();
       this.item = item;
       this.next = next;
    }
}

**Class **MyLinkedList

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public class MyLinkedList<E> {
    MyNode<E> first;
    MyNode<E> last;
    int size;
    public void add(E item) {
       MyNode<E> l = last;//表示最后节点
       MyNode<E> node = new MyNode<E>();
       node.item = item;
       node.next = null;
       if(l!=null) {//表示已经有节点
           l.next = node;
       }
       if(first==null) {//里面没有
           first=node;//第一次添加
       }
       last = node;//始终是最后一个
       size++;
       
    }
    
    public E get(int index) {
       if(index<0 || index>=size) {
           throw new  IndexOutOfBoundsException("index"+index+"size");
       }
       MyNode<E> result = first;
       for(int i=0;i<index;i++) {
           result = result.next;
       }
       return result.item;
    }
    
    public int getSize() {
       return size;
    }
    
    public void remove(int index) {
       if(index<0 || index>=size) {
           throw new  IndexOutOfBoundsException("index"+index+"size");
       }
       MyNode<E> pre = first;
       if(index==0) {
           first = first.next;
           size--;
           return;
       }
       for(int i=0;i<index-1;i++) {
           pre = pre.next;
       }
       MyNode<E> self = pre.next;
       MyNode<E> next = self.next;
       if(next==null) {
           pre.next = null;
           self = null;
           last = pre;
       }else {
           pre.next = next;
           self = null;
       }
       size--;
    }
}

**9.**Vector和Stack类

Stack:栈。

10.Set类

包含两个子类:HashSetTreeSet

**HashSet**:返回元素的顺序并不是特定的。基于HashMap。

**TreeSet**:返回元素的顺序是根据比较器(Comparator)实现,因此按照每个字符值排序。基于二叉树(TreeMap)。

例题分析:

迭代器:Iterator iterator1 = Set.iterator()

注解:   接口名<泛型>  参数名 = 类名(继承接口并实现)<泛型>.方法名(继承接口具体实现的方法)()   

**Iterator.hasNext()**:如果仍有元素可以迭代,则返回 true。(换句话说,如果 next 返回了元素而不是抛出异常,则返回 true)。

Iterator**.next()**:返回迭代的下一个元素。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
public class Set_Test {
    public static void main(String[] args) {
       //HashSet
       Set<Student> set1 = new  HashSet<Student>();
       for(int i=0;i<20;i++) {
           set1.add(new Student(("张")+i,(18+i)));
       }
       Iterator<Student> iterator1 =  set1.iterator();
       while(iterator1.hasNext()) {
           System.out.println(iterator1.next());
       }
       
       System.out.println("=========================");
       
       //TreeSet
       Set<Student> set2 = new  TreeSet<Student>();
       for(int i=0;i<20;i++) {
           set2.add(new Student(("张")+i,(18+i)));
       }
       Iterator<Student> iterator2 =  set2.iterator();
       while(iterator2.hasNext()) {
           System.out.println(iterator2.next());
       }
    }
    
    
}
class Student implements Comparable {
       private String name;
       private int age;
       public Student() {
           super();
       }
       public Student(String name, int age)  {
           super();
           this.name = name;
           this.age = age;
       }
       @Override
       public String toString() {
           return "Student [name=" + name +  ", age=" + age + "]";
       }
       @Override
       public int hashCode() {
           final int prime = 31;
           int result = 1;
           result = prime * result + age;
           result = prime * result + ((name  == null) ? 0 : name.hashCode());
           return result;
       }
       @Override
       public boolean equals(Object obj) {
           if (this == obj)
               return true;
           if (obj == null)
               return false;
           if (getClass() != obj.getClass())
               return false;
           Student other = (Student) obj;
           if (age != other.age)
               return false;
           if (name == null) {
               if (other.name != null)
                  return false;
           } else if  (!name.equals(other.name))
               return false;
           return true;
       }
       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;
       }
       @Override
       public int compareTo(Object o) {
           if(!(o instanceof Student)) {
               throw new  RuntimeException("错误");
           }
           return  this.hashCode()-((Student)o).hashCode();
       }
    }
CATALOG