标签:
少说废话,上代码:这是我学习的代码,如果有用,希望能帮助到大家!争取每个例子都是涉及的到新的技术,新的知识点!
喜欢就关注我的博客:http://blog.csdn.net/happylaoxu
少说废话,上代码:
package com.xuyaowen.student; import java.util.*; //利用空对象的方式实现RTTI 的形式; interface Null{ } class Student{ public static int allNum=0; private int id; private String name; private int age; Student(){ System.out.println("这是普通的对象的形式"); } Student(int id, String name, int age){ allNum++; this.id = id; this.name = name; this.age = age; } public String toString(){ return "ID " + this.id + " Name "+ this.name + " age " + this.age; } public void modifyAge(int addNum){ System.out.println("你正在修改年龄!"); this.age = this.age + addNum; } public int getAge(){ return this.age; } public int getId(){ return this.id; } public String getName(){ return this.name; } public static class NullStudent extends Student implements Null { private NullStudent(){ super(0, "none", 0); } public String toString(){ return "NullPerson"; } } public static final Student NULL = new NullStudent(); //由此可见可以直接调用 Student.NULL ; 即可以实现其中的情况; } //定义一个比较的接口; class StudentIdComparator implements Comparator<Student>{ public int compare(Student std1, Student std2){ return (std1.getId() - std2.getId()); } } class StudentAgeComparator implements Comparator<Student>{ public int compare(Student std1, Student std2){ return (std1.getAge() - std2.getAge()); } } class Admin{ private LinkedList<Student> students = new LinkedList<Student>(); Admin(){ System.out.println("启动学生管理员模式!"); } //添加一个学生; public void addStudent(Student std){ students.add(std); System.out.println("成功添加学生:"+std.getName()); } //增加所有人的年龄; public void addAge(int num){ if(num<0){ System.out.println("年龄增长应为正数!"); }else{ for(Student a: students){ a.modifyAge(num); } System.out.println("年龄增长了 "+ num); } } //得到人群之中年龄大于20 的人数; public int getNum20(){ int count = 0; for(Student a: students){ if(a.getAge()>20){ count++; } } System.out.println("当前学生中年龄大于20的有 "+ count + " 人"); return count; } //通过id来给学生进行排序的操作 public void sortStudents(String tag){ if("id" == tag){ System.out.println("你是以id作为排序依据"); Collections.sort(students, new StudentIdComparator());//利用id进行排序; }else if("age" == tag){ Collections.sort(students, new StudentAgeComparator());//利用年龄进行排序 System.out.println("你是以age作为排序依据"); }else{ System.out.println("警告!这个类不能通过 以 "+ tag + " 为依据进行排序"); } } public String toString(){ StringBuilder result = new StringBuilder(); for(Student a: students){ result.append(a.toString()+" "); result.append('\n');//这之中换行符还真得可以被解析出来啊! } return result.toString(); } } public class School { public static Admin admin = new Admin(); private static Scanner scan = new Scanner(System.in); public static void main(String[] args){ System.out.println("本程序只是涉及接口展示,不涉及程序的交互!"); scan.nextLine(); System.out.println("这是学生类的测试"); admin.addStudent(new Student(123, "jack0", 12)); admin.addStudent(new Student(125, "jack2", 13)); admin.addStudent(new Student(121, "jack3", 16)); admin.addStudent(new Student(120, "jack4", 20)); admin.addStudent(new Student(124, "jack5", 22)); admin.addStudent(new Student(126, "jack6", 21)); System.out.println(admin); scan.nextLine(); System.out.println("按照学生id进行排序:"); admin.sortStudents("id"); System.out.println(admin); scan.nextLine(); System.out.println("按照学生age进行排序:"); admin.sortStudents("age"); System.out.println(admin); scan.nextLine(); System.out.println("所有学生年龄都增加1"); admin.addAge(1); System.out.println(admin); scan.nextLine(); System.out.println("统计所有年龄大于20岁的人数"); admin.getNum20(); scan.nextLine(); System.out.println("程序接口展示结束!"); } }
java 学习实例,实现空对象,和重写了,Comparator 的类实现了对于容器的比较!
标签:
原文地址:http://blog.csdn.net/happylaoxu/article/details/45642213