5建网站,求个网站或者软件,昆明电商网站建设,珠宝网站建设的主要方式1.接口私有方法#xff08;Java9#xff09;
在Java9之前#xff0c;interface接口只能定义abstract抽象方法和default默认方法。如果有多个默认方法使用了相同的处理逻辑#xff0c;那只能写重复代码#xff0c;或者再单独建个类进行调用。Java9解决了此类问题#xff…1.接口私有方法Java9
在Java9之前interface接口只能定义abstract抽象方法和default默认方法。如果有多个默认方法使用了相同的处理逻辑那只能写重复代码或者再单独建个类进行调用。Java9解决了此类问题其允许在接口中定义private私有方法减少重用代码和多余的类。比如下面这个例子
public interface MyInterface { default void method1() { System.out.println(Default method1); commonMethod(); }default void method2() { System.out.println(Default method2); commonMethod(); }/** * 这是一段通用的处理逻辑 */ private void commonMethod() { System.out.println(Common method in the interface); }}在这个示例中MyInterface接口有两个默认方法method1()和method2()它们都调用了私有方法commonMethod()避免了代码重复。并且当实现MyInterface时只需要调用method1()或者method2()无需关心其共同调用的commonMethod()的具体实现。
2.Optional增强Java9
stream()
在Java 9之前如果想对Optional对象中的值进行操作还得使用ifPresent()方法或者orElse()方法。例如以下是一个Java 8的例子
OptionalString optional ...;
optional.ifPresent(value - System.out.println(value.length()));在Java 9中可以直接使用stream()方法和Stream的map()方法来达到相同的效果代码如下
OptionalString optional ...;
optional.stream().map(String::length).forEach(System.out::println);还是能看出来有了stream()方法后对于对象的操作也变得方便了许多。
ifPresentOrElse()
这个方法允许提供两个Runnable第一个在Optional对象包含值时执行第二个在Optional对象为空时执行。例如下面这两段代码对比了Java8和Java9中不同的处理
OptionalString optionalValue Optional.of(Hello);
// Java 8if (optionalValue.isPresent()) { System.out.println(Value is present: optionalValue.get());} else { System.out.println(Value is absent);}
// Java 9
optionalValue.ifPresentOrElse( value - System.out.println(Value is present: value), () - System.out.println(Value is absent));通过ifPresentOrElse方法可以更加简洁地处理类似情况而不再需要频繁使用条件语句。
or()
这个方法允许你在Optional对象为空时提供一个备选的Optional对象。例如
OptionalString optional Optional.empty();
OptionalString backup Optional.of(Backup value);
OptionalString result optional.or(() - backup);
System.out.println(result.get()); // Prints Backup valueisEmpty()
用于检查Optional对象是否为空。例如
OptionalString optional Optional.empty();
if (optional.isEmpty()) { System.out.println(Optional is empty);} // Prints Optional is empty其实这个方法等价于!optionalValue.isPresent()只是不再需要取反
Stream API增强Java9
takeWhile()
这个方法接收一个指定条件它可以从一个有序的Stream中取出满足条件的所有元素一旦遇到不满足条件的元素就会停止处理后续元素。例如
Stream.of(a, b, c, de, f).takeWhile(s - s.length() 1).forEach(System.out::print); // Prints abc在这个例子中我们使用takeWhile()方法从一个Stream中取出所有长度为1的字符串直到遇到一个长度不为1的字符串。
dropWhile()
该方法和takeWhile逻辑正好相反通过指定条件来丢弃Stream流中满足条件的元素一旦遇到不满足条件的元素才会开始处理后续元素。
Stream.of(a, b, c, de, f) .dropWhile(s - s.length() 1) .forEach(System.out::print); // Prints def在这个例子中使用dropWhile()方法丢弃所有长度为1的字符串直到遇到一个长度不为1的字符串才开始处理后续的逻辑。
ofNullable()
该方法允许我们使用Optional对象来创建流。如果提供的元素为非空则生成一个包含该元素的流如果提供的元素为空则生成一个空流。
Stream.ofNullable(null).forEach(System.out::print); // Prints nothingiterate()
该方法提供了一个新的重载形式允许我们指定一个条件来定义流的终止条件这样可以更灵活地控制Stream流的生成。
StreamInteger stream Stream.iterate(1, n - n 10, n - n * 2);stream.forEach(System.out::print);// Prints 1248Switch表达式增强Java12
在之前的版本中switch只能作为语句来使用看下面的示例
int day 2;String dayOfWeek;
switch (day) { case 1: dayOfWeek Monday; break; case 2: dayOfWeek Tuesday; break; case 3: dayOfWeek Wednesday; break; case 4: dayOfWeek Thursday; break; case 5: dayOfWeek Friday; break; case 6: dayOfWeek Saturday; break; case 7: dayOfWeek Sunday; break; default: throw new IllegalArgumentException(Invalid day of week: day);}System.out.println(dayOfWeek); // Prints Tuesday在Java12以后switch可以直接作为表达式使用直接看示例
int day 2;
String dayOfWeek switch (day) { case 1 - Monday; case 2 - Tuesday; case 3 - Wednesday; case 4 - Thursday; case 5 - Friday; case 6 - Saturday; case 7 - Sunday; default - throw new IllegalArgumentException(Invalid day of week: day);
};
System.out.println(dayOfWeek); // Prints Tuesday无论是代码量还是可读性上都有了改进。
instanceof增强Java16
在Java 16以前当我们使用instanceof来检查一个对象是否是某个类型的实例时如果检查成功还得显式地将这个对象转型为对应类型然后才能调用这个类型的方法或访问它的字段。例如
Object obj ...;
if (obj instanceof String) { String str (String) obj; System.out.println(str.length());
}在这个例子中我们首先检查obj对象是否是String类型的实例然后将obj对象转型为String类型并将结果赋值给str变量最后调用str变量的length()方法。 但是在Java 16中可以在instanceof操作符后面直接定义一个变量这个变量会自动被初始化为转型后的对象可以直接使用这个变量再也不用显式转型。例如
Object obj ...;
if (obj instanceof String str) { System.out.println(str.length());
}我们在instanceof操作符后面定义了一个str变量这个变量自动被初始化为obj对象转型为String类型后的结果然后我们直接调用str变量的length()方法无需显式转型。这又是一个利于开发的特性让代码更加简洁和直观。 文章转载自: http://www.morning.ctsjq.cn.gov.cn.ctsjq.cn http://www.morning.srrzb.cn.gov.cn.srrzb.cn http://www.morning.kwqwp.cn.gov.cn.kwqwp.cn http://www.morning.jcfqg.cn.gov.cn.jcfqg.cn http://www.morning.hxxzp.cn.gov.cn.hxxzp.cn http://www.morning.klyyd.cn.gov.cn.klyyd.cn http://www.morning.mltsc.cn.gov.cn.mltsc.cn http://www.morning.stwxr.cn.gov.cn.stwxr.cn http://www.morning.mlffg.cn.gov.cn.mlffg.cn http://www.morning.lqqqh.cn.gov.cn.lqqqh.cn http://www.morning.hxljc.cn.gov.cn.hxljc.cn http://www.morning.dpbdq.cn.gov.cn.dpbdq.cn http://www.morning.tsgxz.cn.gov.cn.tsgxz.cn http://www.morning.wbqk.cn.gov.cn.wbqk.cn http://www.morning.wrlxt.cn.gov.cn.wrlxt.cn http://www.morning.ryyjw.cn.gov.cn.ryyjw.cn http://www.morning.hwzzq.cn.gov.cn.hwzzq.cn http://www.morning.lkcqz.cn.gov.cn.lkcqz.cn http://www.morning.fllfc.cn.gov.cn.fllfc.cn http://www.morning.leboju.com.gov.cn.leboju.com http://www.morning.bqwsz.cn.gov.cn.bqwsz.cn http://www.morning.fcqlt.cn.gov.cn.fcqlt.cn http://www.morning.lbggk.cn.gov.cn.lbggk.cn http://www.morning.lbqt.cn.gov.cn.lbqt.cn http://www.morning.fwgnq.cn.gov.cn.fwgnq.cn http://www.morning.bmtkp.cn.gov.cn.bmtkp.cn http://www.morning.nkcfh.cn.gov.cn.nkcfh.cn http://www.morning.wbhzr.cn.gov.cn.wbhzr.cn http://www.morning.lpppg.cn.gov.cn.lpppg.cn http://www.morning.tthmg.cn.gov.cn.tthmg.cn http://www.morning.ngmjn.cn.gov.cn.ngmjn.cn http://www.morning.sdkaiyu.com.gov.cn.sdkaiyu.com http://www.morning.fpxyy.cn.gov.cn.fpxyy.cn http://www.morning.gynlc.cn.gov.cn.gynlc.cn http://www.morning.ppqzb.cn.gov.cn.ppqzb.cn http://www.morning.mstrb.cn.gov.cn.mstrb.cn http://www.morning.dytqf.cn.gov.cn.dytqf.cn http://www.morning.bloao.com.gov.cn.bloao.com http://www.morning.jpbky.cn.gov.cn.jpbky.cn http://www.morning.knqzd.cn.gov.cn.knqzd.cn http://www.morning.xnpj.cn.gov.cn.xnpj.cn http://www.morning.fqzz3.cn.gov.cn.fqzz3.cn http://www.morning.lgznc.cn.gov.cn.lgznc.cn http://www.morning.mtqqx.cn.gov.cn.mtqqx.cn http://www.morning.jrqw.cn.gov.cn.jrqw.cn http://www.morning.frtt.cn.gov.cn.frtt.cn http://www.morning.xhhqd.cn.gov.cn.xhhqd.cn http://www.morning.wnhml.cn.gov.cn.wnhml.cn http://www.morning.qhkx.cn.gov.cn.qhkx.cn http://www.morning.qhrlb.cn.gov.cn.qhrlb.cn http://www.morning.pqypt.cn.gov.cn.pqypt.cn http://www.morning.bqts.cn.gov.cn.bqts.cn http://www.morning.rcmwl.cn.gov.cn.rcmwl.cn http://www.morning.gagapp.cn.gov.cn.gagapp.cn http://www.morning.duqianw.com.gov.cn.duqianw.com http://www.morning.bkwd.cn.gov.cn.bkwd.cn http://www.morning.cpgdy.cn.gov.cn.cpgdy.cn http://www.morning.hyxwh.cn.gov.cn.hyxwh.cn http://www.morning.crsnb.cn.gov.cn.crsnb.cn http://www.morning.jqmqf.cn.gov.cn.jqmqf.cn http://www.morning.kyjpg.cn.gov.cn.kyjpg.cn http://www.morning.bklhx.cn.gov.cn.bklhx.cn http://www.morning.yjmlg.cn.gov.cn.yjmlg.cn http://www.morning.ncwgt.cn.gov.cn.ncwgt.cn http://www.morning.bxrqf.cn.gov.cn.bxrqf.cn http://www.morning.gdgylp.com.gov.cn.gdgylp.com http://www.morning.wfmqc.cn.gov.cn.wfmqc.cn http://www.morning.haibuli.com.gov.cn.haibuli.com http://www.morning.xgmf.cn.gov.cn.xgmf.cn http://www.morning.qxwwg.cn.gov.cn.qxwwg.cn http://www.morning.dqzcf.cn.gov.cn.dqzcf.cn http://www.morning.wnjbn.cn.gov.cn.wnjbn.cn http://www.morning.xqcst.cn.gov.cn.xqcst.cn http://www.morning.ylph.cn.gov.cn.ylph.cn http://www.morning.yhgbd.cn.gov.cn.yhgbd.cn http://www.morning.qfrsm.cn.gov.cn.qfrsm.cn http://www.morning.ghxsn.cn.gov.cn.ghxsn.cn http://www.morning.yrwqz.cn.gov.cn.yrwqz.cn http://www.morning.gyfhk.cn.gov.cn.gyfhk.cn http://www.morning.qggxt.cn.gov.cn.qggxt.cn