Java8特性总结(一)概述
Java8特性总结(二)Lambda表达式,函数式接口,方法引用
Java8特性总结(三)接口方法,注解,日期,Base64
这几个知识点相对内容较少,放到一篇文章里介绍。
接口方法如何写,大家都知道了,很简单。
谈到接口方法,那就难免不谈谈他的继承关系。
Java是可以实现多个接口的,那这个时候实现的多个接口有同名,同参的接口方法会发生什么情况呢。
接下来我们就通过代码示例,展开分析一下。
接口IA,
两个子接口IB1,IB2,
实现了IB1和IB2的类CC,
继承了IB1,IB2的接口IC, 大致就是3层关系。
代码:
public interface IA
{
default void print(String str)
{
System.out.println(this.getClass()+".print() IA "+str);
}
}
public interface IB1 extends IA
{
}
public interface IB2 extends IA
{
}
public interface IC extends IB1,IB2
{
}
public class C implements IB1, IB2
{
public static void main(String[] args)
{
new C().print("test");
}
}
1:IA的默认方法print(),IB1,IB2没有Override print().
这种情况一切正常,没有任何问题,最后执行C的输出是:
class my.test.java8.second.interfaceMethod1.C.print() IA test
2:IA定义默认方法print(),IB1 Override了print().
public interface IB1 extends IA
{
default void print(String str)
{
System.out.println(this.getClass()+".print() IB1 "+str);
}
}
这种情况也是正常,编译器也没有报任何错误,最后是C的输出
class my.test.java8.second.interfaceMethod1.C.print() IB1 test
发现输出调用的是IB1的默认方法。
可以得出结论,实现类只在乎离自己最近的那个接口的默认方法。
3:IA定义了默认方法print(),IB1和IB2分别Override。
public interface IB2 extends IA
{
default void print(String str)
{
System.out.println(this.getClass()+".print() IB2 "+str);
}
}
出事了,IC和C都报错了,因为编译器已经混乱了,不知道该调用IB1的方法还是IB2的方法。
Java给出的解决问题的办法就是每个子类Override这个方法,告诉编译器,我不调用别人的,我调用我自己的方法。
为C类添加函数,类似的也给IC添加。
@Override
public void print(String str)
{
IB1.super.print(str);
IB2.super.print(str);
}
编译过去了,看看输出吧
class my.test.java8.second.interfaceMethod1.C.print() IB1 test
class my.test.java8.second.interfaceMethod1.C.print() IB2 test
我们分别调用了两个父接口的默认方法。
当然我们也可以在方法中自己实现。
接下来就是继承类,实现接口,接口和类有同名,同参函数的情况了。
接口IA;
类CA;
抽象类ACA;
类CB1继承CA,实现IA;
类CB2继承ACA,实现IA。
public interface IA
{
default void print(String str)
{
System.out.println(this.getClass()+".print() IA "+str);
}
}
public class CA
{
public void print(String str)
{
System.out.println(this.getClass()+".print() CA "+str);
}
}
public abstract class ACA
{
public abstract void print(String str);
}
public class CB1 extends CA implements IA
{
public static void main(String[] args)
{
new CB1().print("hello");
}
}
public class CB2 extends ACA implements IA
{
}
CB2直接报错。必须要Override print()方法。
修改CB2
public class CB2 extends ACA implements IA
{
@Override
public void print(String str)
{
}
}
运行CB1.输出如下
class my.test.java8.second.interfaceMethod2.CB1.print() CA hello
调用的是父类的方法。
可以得出结论,父类的方法优先于接口。
注解的变化还是比较小,增加了可以反复添加注解的功能。
通过代码来看看如何实现。
在一个注解中使用另外一个注解数组,这个以前版本是支持的
@Retention(RetentionPolicy.RUNTIME)
public @interface Student
{
String value();
}
@Retention(RetentionPolicy.RUNTIME)
public @interface Classes
{
Student[] value();
}
实际使用情况
@Classes({
@Student("李壮"),@Student("王静")})
public class MyClasses
{
}
现在的版本可以写成这样。和上面的写法是等价的。
@Student("李壮")
@Student("王静")
public class MyClasses
{
}
编译器报错了!提示我们@Student不是一个repeatabel的注解。
@Repeatable这个注解的注释说的很清楚,value必须是当前注解的注解集合。
如下所示:
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(Classes.class)
public @interface Student
{
String value();
}
改成这样错误消失。
打印测试一下结果。
public static void main(String[] args)
{
Student student=MyClasses.class.getAnnotation(Student.class);
System.out.println(student);
Student[] students=MyClasses.class.getAnnotationsByType(Student.class);
Arrays.asList(students).stream().forEach(e->System.out.println(e.value()));
Classes classes=MyClasses.class.getDeclaredAnnotation(Classes.class);
Arrays.asList(classes.value()).stream().forEach(e->System.out.println(e.value()));
}
可以看出现在这种重复的注解方式,实际上是隐式的包装成Classes。
这个知识点使用较少,也就Mark一下,以备不时之需。
Base64编码引入了标准包里,使用更方便了。
以前版本使用Base64一般会用第三方的类。
比如我常用apache 的 commmon-codec.jar里Base64进行编码和解码。
String base64String = "I am base64";
byte[] result = Base64.encodeBase64(base64String.getBytes());
当然还有一些其他的。
现在直接使用Java自己的,而且据说也是效率最高的。
import java.util.Base64;
public class Base64Test
{
public static void main(String[] args) throws Exception
{
String base64String = "I am base64";
String encoded=Base64.getEncoder().encodeToString(base64String.getBytes("UTF-8"));
String end=new String(Base64.getDecoder().decode(encoded),"UTF-8");
System.out.println(encoded);
System.out.println(end);
}
}
新的时间变化包括有。
LocalDate,LocalTime,LocalDateTime,Clock,时区的概念,时间差的概念。
public class LocalDateTest
{
public static void main(String[] args)
{
LocalDate today = LocalDate.now();
LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS);
LocalDate yesterday = tomorrow.minusDays(2);
System.out.println(tomorrow);
System.out.println(yesterday);
LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4);
DayOfWeek dayOfWeek = independenceDay.getDayOfWeek();
System.out.println(dayOfWeek); // FRIDAY
}
}
输出结果
2017-01-20
2017-01-18
FRIDAY
直观的感觉运算比以前简单了不少。
public class LocalTimeTest
{
public static void main(String[] args)
{
LocalTime time=LocalTime.now();
System.out.println(time);
System.out.println(time.minusHours(1));
System.out.println(time.minusMinutes(10));
System.out.println(time.plusHours(10));
LocalTime late = LocalTime.of(23, 59, 59);
System.out.println(late); // 23:59:59
}
}
输出结果
14:22:14.033
13:22:14.033
14:12:14.033
00:22:14.033
23:59:59
感觉运算好方便。
public class LocalDateTimeTest
{
public static void main(String[] args)
{
LocalDateTime sylvester = LocalDateTime.of(2014, Month.DECEMBER, 31, 23, 59, 59);
DayOfWeek dayOfWeek = sylvester.getDayOfWeek();
System.out.println(dayOfWeek); // WEDNESDAY
Month month = sylvester.getMonth();
System.out.println(month); // DECEMBER
long minuteOfDay = sylvester.getLong(ChronoField.MINUTE_OF_DAY);
System.out.println(minuteOfDay); // 1439
LocalDateTime localdt=LocalDateTime.now();
System.out.println(localdt);
}
}
输出结果
WEDNESDAY
DECEMBER
1439
2017-01-19T14:23:34.938
public class TimeZoneTest
{
public static void main(String[] args)
{
//打印所有的时区
System.out.println(ZoneId.getAvailableZoneIds());
ZoneId zone1 = ZoneId.of("Europe/Berlin");
ZoneId zone2 = ZoneId.of("Brazil/East");
ZoneId zone3=ZoneId.of("Asia/Shanghai");
System.out.println(zone1.getRules());
System.out.println(zone2.getRules());
System.out.println(zone3.getRules());
LocalTime now2 = LocalTime.now(zone2);
LocalTime now1 = LocalTime.now(zone1);
System.out.println(now1);
System.out.println(now2);
System.out.println(now1.isBefore(now2)); // false
long hoursBetween = ChronoUnit.HOURS.between(now1, now2);
long minutesBetween = ChronoUnit.MINUTES.between(now1, now2);
System.out.println(hoursBetween); // -3
System.out.println(minutesBetween); // -239
}
}
打印结果
[Asia/Aden, America/Cuiaba, 。。。。。 Europe/Monaco]//太长了
ZoneRules[currentStandardOffset=+01:00]东一区
ZoneRules[currentStandardOffset=-03:00]西三区
ZoneRules[currentStandardOffset=+08:00]东八区
07:24:39.798
04:24:39.798
false
-3
-180
public class ClockTest
{
public static void main(String[] args)
{
Clock clock = Clock.systemDefaultZone();
long millis = clock.millis();
System.out.println(millis);
Instant instant = clock.instant();
Date legacyDate = Date.from(instant); // legacy java.util.Date
System.out.println(legacyDate);
}
}
输出
1484807586661
Thu Jan 19 14:33:06 CST 2017
和当前日期类型的转换关系,获取当前时区时间的毫秒。
public class DurationTest
{
public static void main(String[] args)
{
final LocalDateTime from = LocalDateTime.of( 2014, Month.MARCH, 16, 0, 0, 0 );
final LocalDateTime to = LocalDateTime.now();
final Duration duration = Duration.between( from, to );
System.out.println( "Duration in days: " + duration.toDays() );
System.out.println( "Duration in hours: " + duration.toHours() );
}
}
输出
Duration in days: 1040
Duration in hours: 24974
参考文章
http://www.cnblogs.com/guangshan/p/4889732.html
http://blog.csdn.net/calmspeed/article/details/44995105
http://www.importnew.com/11908.html
http://ifeve.com/java-8-tutorial-2/
http://www.importnew.com/15637.html
文章浏览阅读1.3k次,点赞40次,收藏19次。虽然你不能直接计算每个房间的人数,但通过马尔科夫链的蒙特卡洛方法,你可以从任意状态(房间)开始采样,并最终收敛到目标分布(人数分布)。然后,根据一个规则(假设转移概率是基于房间的人数,人数较多的房间具有较高的转移概率),你随机选择一个相邻的房间作为下一个状态。比如在巨大城堡,里面有很多房间,找到每个房间里的人数分布情况(每个房间被访问的次数),但是你不能一次进入所有的房间并计数。但是,当你重复这个过程很多次时,你会发现你更有可能停留在人数更多的房间,而在人数较少的房间停留的次数较少。_马尔科夫链期望怎么求
文章浏览阅读3.9k次。一、su命令su命令用于切换当前用户身份到其他用户身份,变更时须输入所要变更的用户帐号与密码。命令su的格式为:su [-] username1、后面可以跟 ‘-‘ 也可以不跟,普通用户su不加username时就是切换到root用户,当然root用户同样可以su到普通用户。 ‘-‘ 这个字符的作用是,加上后会初始化当前用户的各种环境变量。下面看下加‘-’和不加‘-’的区别:root用户切换到普通..._限制su root登陆
文章浏览阅读1.2k次。精通VC与Matlab联合编程(六)作者:邓科下载源代码浅析VC与MATLAB联合编程浅析VC与MATLAB联合编程浅析VC与MATLAB联合编程浅析VC与MATLAB联合编程浅析VC与MATLAB联合编程 Matlab C/C++函数库是Matlab扩展功能重要的组成部分,包含了大量的用C/C++语言重新编写的Matlab函数,主要包括初等数学函数、线形代数函数、矩阵操作函数、数值计算函数_精通vc和matlab联合编程 六
文章浏览阅读128次。在MVC2中默认并没有实现DescriptionAttribute(虽然可以找到这个属性,通过阅读MVC源码,发现并没有实现方法),这很不方便,特别是我们使用EditorForModel的时候,我们需要对字段进行简要的介绍,下面来扩展这个属性。新建类 DescriptionMetadataProvider然后重写DataAnnotationsModelMetadataPro..._asp.net mvc 模型description
文章浏览阅读1.3k次。一.概述 本篇继续探讨web应用架构,讲基于DDD风格下最初的领域模型架构,不同于DDD风格下CQRS架构,二者架构主要区别是领域层的变化。 架构的演变是从领域模型到C..._eshoponweb
文章浏览阅读2.6w次,点赞23次,收藏85次。首先说明,本人之前没用过zookeeper、kafka等,尚硅谷十几个小时的教程实在没有耐心看,现在我也不知道分区、副本之类的概念。用kafka只是听说他比RabbitMQ快,我也是昨天晚上刚使用,下文中若有讲错的地方或者我的理解与它的本质有偏差的地方请包涵。此文背景的环境是windows,linux流程也差不多。 官网下载kafka,选择Binary downloads Apache Kafka 解压在D盘下或者什么地方,注意不要放在桌面等绝对路径太长的地方 打开conf_springboot kafka
文章浏览阅读1k次。编好水晶报表代码,用的是ActiveX模式,在本机运行,第一次运行提示安装ActiveX控件,安装后,一切正常,能正常打印,但发布到网站那边运行,可能是一闪而过,连提示安装ActiveX控件也没有,甚至相关的功能图标都不能正常显示,再点"打印图标"也是没反应解决方法是: 1.先下载"PrintControl.cab" http://support.businessobjects.c_水晶报表 不能打印
文章浏览阅读1.3k次。绝大部分UC/OS-II的源码是用移植性很强的ANSI C写的。也就是说某产品可以只使用很少几个UC/OS-II调用,而另一个产品则使用了几乎所有UC/OS-II的功能,这样可以减少产品中的UC/OS-II所需的存储器空间(RAM和ROM)。UC/OS-II是为嵌入式应用而设计的,这就意味着,只要用户有固化手段(C编译、连接、下载和固化), UC/OS-II可以嵌入到用户的产品中成为产品的一部分。1998年uC/OS-II,目前的版本uC/OS -II V2.61,2.72。1.UC/OS-Ⅱ简介。_ucos
文章浏览阅读614次,点赞22次,收藏11次。大家好,本文将围绕python自动化运维需要掌握的技能展开说明,python自动化运维从入门到精通是一个很多人都想弄明白的事情,想搞清楚python自动化运维快速入门 pdf需要先了解以下几个事情。这篇文章主要介绍了一个有趣的事情,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获,下面让小编带着大家一起了解一下。_运维学python该学些什么
文章浏览阅读524次。2019独角兽企业重金招聘Python工程师标准>>> ..._hotfix for msxml 4.0 service pack 2 - kb832414
文章浏览阅读546次。python和易语言的脚本哪门更实用?_易语言还是python适合辅助
文章浏览阅读134次。详解redis中的锁以及使用场景,指令,事务,分布式,命令,时间详解redis中的锁以及使用场景易采站长站,站长之家为您整理了详解redis中的锁以及使用场景的相关内容。分布式锁什么是分布式锁?分布式锁是控制分布式系统之间同步访问共享资源的一种方式。为什么要使用分布式锁? 为了保证共享资源的数据一致性。什么场景下使用分布式锁? 数据重要且要保证一致性如何实现分布式锁?主要介绍使用redis来实..._redis setnx watch