No2_4.接口继承多态_Java学习笔记_经典案例

  1 import java.lang.reflect.Field;
  2 import java.util.ArrayList;
  3 import java.util.Collections;
  4 import java.util.List;
  5 
  6 /**
  7  * @title 接口继承多态的经典范例
  8  * @author 作者:sunshine
  9  * @date 创建时间:2016年7月6日 下午5:27:39
 10  */
 11 
 12 //使用Comparable接口自定义排序 
 13 class Employee implements Comparable<Employee>{
 14     private int id;
 15     private String name;
 16     private int age;
 17     public Employee(int id,String name,int age){
 18         this.id=id;
 19         this.age=age;
 20         this.name=name;        
 21     }
 22     @Override
 23     public int compareTo(Employee o){
 24         if(age>o.age){
 25             return 1;
 26         }else if(age<o.age){
 27         return -1;
 28         }
 29         return 0;
 30     }
 31     @Override 
 32     public String toString(){
 33         StringBuilder sb=new StringBuilder();
 34         sb.append("员工的编号:"+id+",");
 35         sb.append("员工的姓名"+ name+",");
 36         sb.append("员工的年龄:"+age);
 37         return sb.toString();
 38     }
 39     
 40 }
 41 
 42 class Students{
 43     private int id;
 44     private String name;
 45     private boolean male;
 46     private double account;        //    学生的帐户余额
 47     //Get() Set()
 48     public Students(){
 49     }
 50     public void setId(int id){
 51         this.id=id;        
 52     }
 53     public void setName(String name){
 54         this.name=name;
 55     }
 56     public void setMale(boolean male){
 57         this.male=male;
 58     }
 59     public void setAccount(double account){
 60         this.account=account;
 61     }
 62     public int getID(){
 63         return id;
 64     }
 65     public String getName(){
 66         return name;
 67     }
 68     public boolean getMale(){
 69         return male;
 70     }
 71     public double getAccount(){
 72         return account;
 73     }
 74     
 75 }
 76 
 77 
 78 
 79 public class Hello4IEPExample {
 80 
 81     public static void main(String[] args) {
 82         // TODO Auto-generated method stub
 83         /**************使用Comparable接口自定义比较**************/
 84         System.out.println("使用Comparable接口自定义比较,输出年龄较大的类:");
 85         Employee employee=new Employee(1,"sunshine",68);
 86         Employee employee1=new Employee(2,"habit",26);
 87         Employee employee2=new Employee(3,"Liuliu",30);
 88         String mm;
 89         if (employee.compareTo(employee2) ==1){
 90              mm=employee.toString();
 91         }
 92         else{
 93              mm=employee2.toString();    
 94         }
 95         
 96         System.out.println(mm);
 97         
 98         /**************使用Comparable接口自定义排序**************/
 99         System.out.println("
使用Comparable接口自定义排序:");
100         List<Employee> list=new ArrayList<Employee>();    //之后了解相关功能
101         list.add(employee);
102         list.add(employee1);
103         list.add(employee2);
104         System.out.println("排序前:");
105         for(Employee e : list){
106             System.out.println(e);            
107         }
108         
109         System.out.println("按年龄排序后:");
110         Collections.sort(list);
111         for(Employee e : list){
112             System.out.println(e);            
113         }
114         
115         /**************动态设置类的私有域**************/
116         System.out.println("
动态设置类的私有域,不太明白,以后再看。。。。。。。。。。。。。。。。。");
117         Students students=new Students();
118         Class<?> clazz=students.getClass();
119         System.out.println("类的标准名称是:"+clazz.getCanonicalName());
120         
121         try {
122             // Field id=clazz.getDeclaredField("id");
123             System.out.println("设置前的id是" + students.getID());
124 
125         } catch (SecurityException e) {
126 
127         }
128         
129         }    
130 }

 输出结果:

 1 使用Comparable接口自定义比较,输出年龄较大的类:
 2 员工的编号:1,员工的姓名sunshine,员工的年龄:68
 3 
 4 使用Comparable接口自定义排序:
 5 排序前:
 6 员工的编号:1,员工的姓名sunshine,员工的年龄:68
 7 员工的编号:2,员工的姓名habit,员工的年龄:26
 8 员工的编号:3,员工的姓名Liuliu,员工的年龄:30
 9 按年龄排序后:
10 员工的编号:2,员工的姓名habit,员工的年龄:26
11 员工的编号:3,员工的姓名Liuliu,员工的年龄:30
12 员工的编号:1,员工的姓名sunshine,员工的年龄:68
13 
14 动态设置类的私有域,不太明白,以后再看,Pages222。。。。。。。。。。。。。。。。。
15 类的标准名称是:Students
16 设置前的id是0
原文地址:https://www.cnblogs.com/sunshine-habit/p/5647815.html