JAVA进阶22

时间:2024-12-27 21:03:32

1、接口默认方法的使用

①接口的默认方法可以通过接口实现类对象直接调用。

②接口的默认方法也可以被接口实现类进行覆盖重写

 package cn.intcast.demo17;

 public interface MyInterfaceDefault {
//抽象方法
public abstract void methodAbs();
// void AAA();
public default void methodA(){
System.out.println("this is a default method!");
}
}

接口类1

 package cn.intcast.demo17;

 public class MyInterfaceDefaultA implements MyInterfaceDefault {
@Override
public void methodAbs() {
System.out.println("实现了抽象 方法,AAA;");
}
}

接口实现类

 package cn.intcast.demo17;

 public class Code01Interface {
public static void main(String[] args) {
//创建实现类对象
MyInterfaceDefaultA s = new MyInterfaceDefaultA();
//调用抽象方法,如果实现类当中没有,会向上找接口
s.methodA();
s.methodAbs();
}
}

运行结果图

JAVA进阶22

2、接口的静态方法使用

接口中的静态方法可以通过接口名称,调用其中的静态方法

格式:接口名称.静态方法名(参数)

 package cn.intcast.demo17;

 public interface StaticInterface {
public static void staticmethod(){
System.out.println("this is a static method!");
}
}

静态方法

 package cn.intcast.demo17;

 public class CodeStatic {
public static void main(String[] args) {
StaticInterface.staticmethod();
}
}

运行结果图

JAVA进阶22

3、Collections工具类

 package cn.demo01;

 import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List; public class Test03 {
public static void main(String[] args){
List<String> aList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
aList.add("a"+i);
}
System.out.println(aList);
//随机排查
Collections.shuffle(aList);
System.out.println(aList);
//逆序
Collections.reverse(aList);
System.out.println(aList);
//顺序
Collections.sort(aList);
System.out.println(aList);
System.out.println(Collections.binarySearch(aList,"a2"));
Collections.fill(aList,"hello");
System.out.println(aList);
}
}

运行结果图

JAVA进阶22

4、ORM思想的简单实验:map表示一行数据,多行数据是多个map;将多个map放到list中(存放表格数据)

 package cn.demo01;

 import java.util.*;

 public class TestTableDate01 {
public static void main(String[] args) {
Map<String,Object> row1 = new HashMap<>();
row1.put("id",101);
row1.put("姓名","张一");
row1.put("age",18); Map<String,Object> row2 = new HashMap<>();
row2.put("id",102);
row2.put("姓名","张二");
row2.put("age",22); List<Map<String,Object>> table1 = new ArrayList<>();
table1.add(row1);
table1.add(row2); for (Map<String,Object> row:table1){
Set<String> keyset = row.keySet();
for (String key:keyset){
System.out.print(key+":"+row.get(key)+"\t");
}
System.out.println();
}
}
}

运行结果图

JAVA进阶22

5、表格数据的存储

 package cn.demo01;

 import java.util.ArrayList;
import java.util.List; public class TestTableDate02 {
public static void main(String[] args) {
User u1 = new User(101,"王大",20,88);
User u2 = new User(102,"王二",23,85);
User u3 = new User(103,"王三",20,84); List<User> list = new ArrayList<>();
list.add(u1);
list.add(u2);
list.add(u3); for (User u:list){
System.out.println(u);
}
}
} class User{
private int id;
private String name;
private int age;
private double grade; public User() {
} public User(int id, String name, int age, double grade) {
this.id = id;
this.name = name;
this.age = age;
this.grade = grade;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public double getGrade() {
return grade;
} public void setGrade(double grade) {
this.grade = grade;
} public String toString(){
return "id:"+id+"\t"+"name:"+name+"\t"+"grade:"+grade+"\t"+"age:"+age+"\t";
}
}

运行结果图

JAVA进阶22