多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 概述 接口就是对已有类的功能进行扩展. ``` public class Employee implements Comparable<Employee> { private String name; private double salary; public Employee(String name, double salary) { this.name = name; this.salary = salary; } public void raiseSalary(double byPrecent) { double raise = this.salary * byPrecent / 100; this.salary += raise; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } @Override public int compareTo(Employee o) { return Double.compare(this.salary, o.salary); } } ``` 测试 ~~~ import java.util.Arrays; public class EmployeeSortTest { public static void main(String[] args) { Employee[] staff = new Employee[3]; staff[0] = new Employee("jack", 20); staff[1] = new Employee("milan", 40); staff[2] = new Employee("mike", 30); Arrays.sort(staff); for (Employee e : staff) { System.out.println("name = " + e.getName() + ", salary = " + e.getSalary()); } } } ~~~