多态
声明方式 向上转型
父类类型 变量名 = new 子类类型;
接口名称 变量名 = new 实现类名称;
向下转型 (在类型转换前 先通过 变量名 instanceof 子类类型)
子类类型 变量名 = (子类类型) 父类对象
实现类名称 变量名 = (实现类名称) 接口名称
向上转型的意义:当接口中的方法已经能够满足需求时,这样更加安全方便
向下转型的意义:能够访问接口类中的独有方法,这些方法接口中不存在
访问成员方法:new谁就优先使用谁 没有向上找
访问成员变量
1.直接通过对象名称访问:看等号左边是谁 优先使用 没有则向上找
2.间接通过成员方法访问:看方法属于谁 优先使用 没有则向上找
总结一句话就是:
成员变量)编译看左边,运行看左边
成员方法)编译看左边,运行看右边
类型转换异常:ClassCastException
日期格式化
DateFormat,SimpleDateFormat
public class App {
public static void main(String[] args) throws ParseException {
String time = "2012-12-12 12:10:1";
DateFormat dateFormat = DateFormat.getDateTimeInstance();
Date date1=dateFormat.parse(time);
System.out.println(dateFormat.format(date1));
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date2 = simpleDateFormat.parse(time);
System.out.println(simpleDateFormat.format(date2));
}
}
LocalDate、LocalTime、LocalDateTime
public class Outer {
public static void main(String[] args){
//now获取当前的年、月、日、时、分、秒
//获取当前年月日时分秒
LocalDateTime time1 = LocalDateTime.now();
//获取当前年月日
LocalDate time2 = LocalDate.now();
//获取当前时分秒
LocalTime time3 = LocalTime.now();
//of设置指定的年、月、日、时、分、秒
//设置指定年月日时分秒
LocalDateTime time1 = LocalDateTime.of(年,月.日,时,分,秒);
//设置指定年月日
LocalDate time2 = LocalDate.of(年,月,日);
//设置指定时分秒
LocalTime time3 = LocalTime.of(时,分,秒);
}
}
Duration 计算时间间隔
public class Outer {
public static void main(String[] args){
LocalTime time1 = LocalTime.of(10,15);
LocalTime time2 = LocalTime.of(20,30,30);
Duration duration = Duration.between(time1,time2);
//输出 相隔:10时15分30秒与目标
System.out.printf("相隔:%d时 %d分 %d秒",duration.toHours(),duration.toMinutes()%60,duration.toMillis()%60000/1000);
}
}
Period计算日期间隔
public class Outer {
public static void main(String[] args){
LocalDate time1 = LocalDate.of(2022,10,10);
LocalDate time2 = LocalDate.of(2020,10,10);
Period period = Period.between(time2,time1);
//输出 相隔:2年0月0日与目标
System.out.printf("相隔:%d年%d月%d日",period.getYears(),period.getMonths(),period.getDays());
}
}
正则表达式
规则
使用
public class test {
public static void main(String[] args) {
String check = "1[357]\\d{9}";
String phoneNumber = "13206335101";
System.out.println(phoneNumber.matches(check));
Pattern pattern= Pattern.compile("1[357]\\d{9}");
//phoneNumber被检测的文本
Matcher matcher = pattern.matcher(phoneNumber);
//匹配上的时候返回true,匹配不通过返回false
System.out.println(matcher.matches());
}
}
泛型
1、定义:泛型的本质是参数化类型,就是将类型由原来的具体的类型参数化,这种参数类型可以用在类、接口、方法中,分别称为泛型类、泛型接口、泛型方法;
泛型类
public class MyArrayList<E> {
public ArrayList arrs = new ArrayList();
public boolean add(E val){
arrs.add(val);
return true;
}
public boolean remove(E val){
arrs.remove(val);
return true;
}
@Override
public String toString() {
return arrs.toString();
}
}
//使用时 E 会自动转换为 <>中的类型
public class test {
public static void main(String[] args) {
MyArrayList<String> arrs = new MyArrayList();
arrs.add("123");
System.out.println(arrs);//[123]
arrs.remove("123");
System.out.println(arrs);//[]
}
}
泛型方法
public class test {
public static void main(String[] args) {
String[] arr1 = {"小李","小帅","小王","小张"};
toStr(arr1);
Integer[] arr2 = {1,2,3,4};
toStr(arr2);
toStr(null);
}
//在调用方法时 会自动赋类型
public static <T> T[] toStr(T[] arrs){
StringBuilder str = new StringBuilder("[") ;
if (arrs == null) return null;
for (int i = 0; i < arrs.length; i++) {
str.append(arrs[i]).append(arrs.length-1==i? "]":",");
}
System.out.println(str);
return arrs;
}
}
泛型接口
//接口
// T 自动转为 Data
public interface MyInterface<T> {
void add(T data);
}
//类
public class Data implements MyInterface<Data> {
@Override
public void add(Data data) {
}
}
通配符
泛型上下边界
public class test {
public static void main(String[] args) {
ArrayList<BMW> car1 = new ArrayList<>();
ArrayList<Benz> car2 = new ArrayList<>();
car1.add(new BMW());
car2.add(new Benz());
game(car1);
game(car2);
}
public static void game(ArrayList<? extends Car> car){
car.forEach(item -> System.out.println(item.name+"参加了比赛"));
}
public static class Car{
String name;
}
public static class BMW extends Car{
public BMW() {
name = "宝马";
}
}
public static class Benz extends Car {
public Benz() {
name = "奔驰";
}
}
}