1.接口默认方法
1.1 接口默认方法的定义
- 在java8之前,接口(interface)没有提供任何具体的实现,接口定义的方法全部都是抽象方法
- java8之后对接口做了进一步增强:
- 允许接口中可以添加使用default关键字,即默认方法,或者也可以叫做可扩展方法
- 接口里可以声明静态方法,并且可以做具体实现
- 默认方法与抽象方法的区别
- 抽象方法必须要实现,默认方法不是,因为默认方法做了具体实现;这个接口中的默认方法可以通过子类继承到这个默认方法,当然子类也可以重写该默认方法。
- 为了提高代码的可重用性,接口的默认方法有助于在扩展系统功能的同时,不对现有的继承关系及类库产生很大的影响,比如在java8中,java集合框架的collection接口增加stream()等默认方法,这些默认方法即增强了集合的功能,同时又保证对低版本jdk的兼容。
1.2 案例1
public interface Animal{
void fly();//飞
void swim();//游泳
}
- 假设定义了Animal接口,里面定义了两个方法,一个fly()飞方法,一个swim()游泳方法。
- 当子类Brid鸟类实现Animal接口的时候就必须重写这两个方法,但是Brid鸟类只需要fly()方法不需要swim()方法,所以这样方法重写两个方法势必造成代码冗余,那么在接口中定义默认方法就体现出优势了!
1.3 案例2
- Animal类
public interface Animal {
default void fly() {
System.out.println("鸟飞飞`````");
}
default void swim() {
System.out.println("鱼游泳、、、、");
}
}
- Brid类
public class Brid implements Animal{
// public void showBrid() {
// this.fly();
// this.swim();
// }
@Override
public void fly() {
System.out.println("鸟在天上飞!");
}
}
- Fish类
public class Fish implements Animal{
@Override
public void swim() {
System.out.println("鱼在水里游!");
}
}
- 测试
public class Test {
public static void main(String[] args) {
Animal brid = new Brid();
brid.fly();
Animal fish = new Fish();
fish.swim();
}
}
1.4接口中静态方法
- java8 之后可以在接口中定义静态方法,因为接口不能被实例化,但是可以通过接口.方法名直接调用。
- 案例1
public interface Animal {
default void fly() {
System.out.println("鸟飞飞`````");
}
default void swim() {
System.out.println("鱼游泳、、、、");
}
public static void showAnimal() {
System.out.println("我是一个动物");
}
}
public class Test {
public static void main(String[] args) {
Animal brid = new Brid();
brid.fly();
Animal fish = new Fish();
fish.swim();
Animal.showAnimal();
}
}
- 案例2
import java.util.function.Supplier;
public interface AnimalFactory {
public static Animal create(Supplier<Animal> supplier) {
return supplier.get();
}
}
public class Test {
public static void main(String[] args) {
//通过接口调用接口中的静态方法
Animal brid = AnimalFactory.create(()->new Brid());
brid.fly();
Animal fish = AnimalFactory.create(Fish::new);
fish.swim();
}
}
2.日期API
- java8新增很多对于日期的处理,如下:
2.1 LocalDate
- 案例1:
import java.time.LocalDate;
public class Test1 {
public static void main(String[] args) {
LocalDate date1 = LocalDate.of(2020, 10, 9);
LocalDate date2 = LocalDate.parse("2020-10-09");
LocalDate date3 = LocalDate.now();
System.out.println(date3);
//获取年月日
System.out.println(date3.getYear());
System.out.println(date3.getMonth());
System.out.println(date3.getMonthValue());
System.out.println(date3.getDayOfYear());
System.out.println(date3.getDayOfMonth());
System.out.println(date3.getDayOfWeek());
//查看该月有多少天
System.out.println(date3.lengthOfMonth());
//检查是否是闰年
System.out.println(date3.isLeapYear());
}
}
- 案例2:
还可以通过get方法获取年月日
import java.time.LocalDate;
import java.time.temporal.ChronoField;
public class Test2 {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
int year = date.get(ChronoField.YEAR);
int month = date.get(ChronoField.MONTH_OF_YEAR);
int day = date.get(ChronoField.DAY_OF_MONTH);
System.out.println(year+"-"+month+"-"+day);
System.out.println("本月第几周:"+date.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH));
}
}
- 案例3
修改日期
import java.time.LocalDate;
import java.time.temporal.ChronoField;
public class Test3 {
public static void main(String[] args) {
LocalDate date1 = LocalDate.of(2020, 10, 9);
date1 = date1.withDayOfMonth(30);//修改天数
System.out.println(date1);
date1 = date1.with(ChronoField.DAY_OF_MONTH,25);//修改天数
System.out.println(date1);
date1 = date1.plusYears(5);//增加
System.out.println(date1);
}
}
- 案例4:
将日期格式化转换成字符串
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class Test {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
String str1 = date.format(DateTimeFormatter.BASIC_ISO_DATE);
System.out.println(str1);
String str2 = date.format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(str2);
System.out.println(date.toString());
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String str3 = date.format(dtf);
System.out.println(str3);
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date time = new Date();
//格式化时间,获取到年月日时分秒
String str4 = df.format(time);
System.out.println("java8之前的日期处理:"+str4);
//将
LocalDate date2 = LocalDate.parse(str3,dtf);
System.out.println(date2);
}
}
2.2 LocalTime
LocalTime是时分秒格式的时间
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class Test1 {
public static void main(String[] args) {
LocalTime time = LocalTime.of(14, 29,35);
time=LocalTime.parse("14:44:56");
System.out.println(time);
System.out.println(time.getHour());
System.out.println(time.getMinute());
System.out.println(time.getSecond());
//将time转换自定义格式
LocalTime time2 = LocalTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH时mm分ss秒");
String time3 = time2.format(dtf);
System.out.println(time3);
}
}
2.3 LocalDateTime
LocalDateTime是年月日 时分秒格式的时间
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Test2 {
public static void main(String[] args) {
LocalDateTime dateTime1 = LocalDateTime.of(2020, 10,9,14,45,55);
LocalDateTime dateTime2 = LocalDateTime.now();
LocalDateTime dateTime = LocalDateTime.parse("2020-11-12T14:45:55");
System.out.println(dateTime);
String time = dateTime2.format(DateTimeFormatter.ofPattern("yyyy年/MM月/dd日 HH时mm分ss秒"));
System.out.println(time);
}
}
2.4Duration\Period求时间差
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
public class Test {
public static void main(String[] args) {
LocalDateTime time1 = LocalDateTime.of(1992,1,11,5,1);
LocalDateTime time2 = LocalDateTime.now();
//求时间差
Duration duration = Duration.between(time1, time2);
//换算为天
long days = duration.toDays();
System.out.println(days);
LocalDate time3 = LocalDate.of(1992,1,11);
LocalDate time4 = LocalDate.now();
//period类是用于localDate类型的时间差
Period period = Period.between(time3, time4);
long days2 = period.getDays();
long months2 = period.getMonths();
System.out.println("相差天数:"+days2);
System.out.println("相差月份:"+months2);
}
}
2.5 isAfter\isBefore判断
import java.time.LocalDate;
public class Test {
public static void main(String[] args) {
LocalDate time1 = LocalDate.of(1997, 3, 15);
LocalDate time2 = LocalDate.now();
System.out.println("两个时间相等吗?"+time1.equals(time2));
System.out.println("time1在time2之后吗?"+time1.isAfter(time2));
System.out.println("time1在time2之前吗?"+time1.isBefore(time2));
}
}
2.6MonthDay
import java.time.LocalDate;
import java.time.MonthDay;
public class Test {
public static void main(String[] args) {
// MonthDay monthDay = MonthDay.of(1, 11);
LocalDate date = LocalDate.of(1997,3,15);
MonthDay monthDay = MonthDay.of(date.getMonth(), date.getDayOfMonth());
//monthDay用于存月-日时间
MonthDay currentMonthDay = MonthDay.from(LocalDate.now());
if(currentMonthDay.equals(monthDay)) {
System.out.println("生日快乐!");
}
}
}