JAVA进阶22
1、接口默认方法的使用
①接口的默认方法可以通过接口实现类对象直接调用。
②接口的默认方法也可以被接口实现类进行覆盖重写
1 package cn.intcast.demo17; 2 3 public interface MyInterfaceDefault { 4 //抽象方法 5 public abstract void methodAbs(); 6 // void AAA(); 7 public default void methodA(){ 8 System.out.println("this is a default method!"); 9 } 10 }
1 package cn.intcast.demo17; 2 3 public class MyInterfaceDefaultA implements MyInterfaceDefault { 4 @Override 5 public void methodAbs() { 6 System.out.println("实现了抽象 方法,AAA;"); 7 } 8 }
1 package cn.intcast.demo17; 2 3 public class Code01Interface { 4 public static void main(String[] args) { 5 //创建实现类对象 6 MyInterfaceDefaultA s = new MyInterfaceDefaultA(); 7 //调用抽象方法,如果实现类当中没有,会向上找接口 8 s.methodA(); 9 s.methodAbs(); 10 } 11 }
运行结果图
2、接口的静态方法使用
接口中的静态方法可以通过接口名称,调用其中的静态方法
格式:接口名称.静态方法名(参数)
1 package cn.intcast.demo17; 2 3 public interface StaticInterface { 4 public static void staticmethod(){ 5 System.out.println("this is a static method!"); 6 } 7 }
1 package cn.intcast.demo17; 2 3 public class CodeStatic { 4 public static void main(String[] args) { 5 StaticInterface.staticmethod(); 6 } 7 }
运行结果图
3、Collections工具类
1 package cn.demo01; 2 3 4 import java.util.ArrayList; 5 import java.util.Collection; 6 import java.util.Collections; 7 import java.util.List; 8 9 public class Test03 { 10 public static void main(String[] args){ 11 List<String> aList = new ArrayList<>(); 12 for (int i = 0; i < 5; i++) { 13 aList.add("a"+i); 14 } 15 System.out.println(aList); 16 //随机排查 17 Collections.shuffle(aList); 18 System.out.println(aList); 19 //逆序 20 Collections.reverse(aList); 21 System.out.println(aList); 22 //顺序 23 Collections.sort(aList); 24 System.out.println(aList); 25 System.out.println(Collections.binarySearch(aList,"a2")); 26 Collections.fill(aList,"hello"); 27 System.out.println(aList); 28 } 29 }
运行结果图
4、ORM思想的简单实验:map表示一行数据,多行数据是多个map;将多个map放到list中(存放表格数据)
1 package cn.demo01; 2 3 import java.util.*; 4 5 public class TestTableDate01 { 6 public static void main(String[] args) { 7 Map<String,Object> row1 = new HashMap<>(); 8 row1.put("id",101); 9 row1.put("姓名","张一"); 10 row1.put("age",18); 11 12 Map<String,Object> row2 = new HashMap<>(); 13 row2.put("id",102); 14 row2.put("姓名","张二"); 15 row2.put("age",22); 16 17 List<Map<String,Object>> table1 = new ArrayList<>(); 18 table1.add(row1); 19 table1.add(row2); 20 21 for (Map<String,Object> row:table1){ 22 Set<String> keyset = row.keySet(); 23 for (String key:keyset){ 24 System.out.print(key+":"+row.get(key)+"\t"); 25 } 26 System.out.println(); 27 } 28 } 29 }
运行结果图
5、表格数据的存储
1 package cn.demo01; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 public class TestTableDate02 { 7 public static void main(String[] args) { 8 User u1 = new User(101,"王大",20,88); 9 User u2 = new User(102,"王二",23,85); 10 User u3 = new User(103,"王三",20,84); 11 12 List<User> list = new ArrayList<>(); 13 list.add(u1); 14 list.add(u2); 15 list.add(u3); 16 17 for (User u:list){ 18 System.out.println(u); 19 } 20 } 21 } 22 23 class User{ 24 private int id; 25 private String name; 26 private int age; 27 private double grade; 28 29 public User() { 30 } 31 32 public User(int id, String name, int age, double grade) { 33 this.id = id; 34 this.name = name; 35 this.age = age; 36 this.grade = grade; 37 } 38 39 public int getId() { 40 return id; 41 } 42 43 public void setId(int id) { 44 this.id = id; 45 } 46 47 public String getName() { 48 return name; 49 } 50 51 public void setName(String name) { 52 this.name = name; 53 } 54 55 public int getAge() { 56 return age; 57 } 58 59 public void setAge(int age) { 60 this.age = age; 61 } 62 63 public double getGrade() { 64 return grade; 65 } 66 67 public void setGrade(double grade) { 68 this.grade = grade; 69 } 70 71 public String toString(){ 72 return "id:"+id+"\t"+"name:"+name+"\t"+"grade:"+grade+"\t"+"age:"+age+"\t"; 73 } 74 }
运行结果图