Java learning instance, implement empty object, and rewrite, Comparator class implements the comparison to the container!

Source: Internet
Author: User

Less nonsense, on the code: this is the code I study, if useful, I hope to help everyone! Striving for each example is involved in the new technology, new knowledge points!

Like to follow my blog: Http://blog.csdn.net/happylaoxu

Talk less nonsense, on the code:

Package Com.xuyaowen.student;import java.util.*;//uses an empty object to implement the Rtti form; interface Null{}class student{public static int allnum=0;private int id;private String name;private int age; Student () {System.out.println ("This is the form of an ordinary object");} 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 ("You are modifying the age!") "); 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 ();//This can be called directly student.null; That can be achieved,}//defines a comparison interface; 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 ("Start student admin mode! ");} Add a student; public void addstudent (Student std) {students.add (STD); SYSTEM.OUT.PRINTLN ("Successfully added student:" +std.getname ());} Increase the age of all; public void addage (int num) {if (num<0) {System.out.println ("Age should be positive!") ");} Else{for (Student a:students) {a.modifyage (num);} SYSTEM.OUT.PRINTLN ("Age increased" + num);}} Get the number of people older than 20; public int getNum20 () {int count = 0;for (Student a:students) {if (A.getage () >20) {count++;}} System.out.println ("The current student is older than 20 has" + count + "person"); return count;} Operation by ID to sort the students public void sortstudents (String tag) {if ("id" = = tag) {System.out.println ("You are sorted by id"); Collections.sort (students, New Studentidcomparator ());//Sort by ID,}else if ("age" = = tag) {Collections.sort (students, New STUDENTAGECOmparator ());//Use the age to sort System.out.println ("You are sort of as old");} else{System.out.println ("Warning! This class cannot be sorted by" + tag + ");}} Public String toString () {StringBuilder result = new StringBuilder (), for (Student a:students) {result.append (a.tostring ( + ""); Result.append (' \ n ');//The line break is really going to be parsed out! }return result.tostring ();}} public class School {public static admin admin = new Admin ();p rivate static Scanner scan = new Scanner (system.in);p ublic The static void Main (string[] args) {System.out.println ("This program only involves interface presentation and does not involve the interaction of the program!") "); Scan.nextline (); System.out.println ("This is a test of the Student Class"), Admin.addstudent (New Student (123, "Jack0"), Admin.addstudent (New Student (125, " Jack2, Admin.addstudent (New Student (121, "Jack3"), Admin.addstudent (New Student ("Jack4", 20)); Admin.addstudent (New Student (124, "Jack5"), Admin.addstudent (New Student (126, "Jack6", 21)); System.out.println (admin); Scan.nextline (); System.out.println ("Sort by Student ID:"); Admin.sortstudents ("id"); System.out.println (admin); Scan.nextline (); System.ouT.println ("Sort by student Age:"), Admin.sortstudents ("age"); System.out.println (admin); Scan.nextline (); System.out.println ("1 increase in age of all students"); Admin.addage (1); System.out.println (admin); Scan.nextline (); System.out.println ("Statistics of persons of all ages greater than 20 years"); Admin.getnum20 (); Scan.nextline (); SYSTEM.OUT.PRINTLN ("Program Interface show end! ");}}


Java learning instance, implement empty object, and rewrite, Comparator class implements the comparison to the container!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.