当前位置: 首页 > news >正文

获取iis中网站日志文件保存路径整合营销案例举例说明

获取iis中网站日志文件保存路径,整合营销案例举例说明,营销软件网站建设,公司网站友情链接怎么做副链目录 一、Java21新特性 1、字符串模版 2、scoped values 3、record pattern 4、switch格式匹配 5、可以在switch中使用when 6、Unnamed Classes and Instance Main Methods 7、Structured Concurrency 一、Java21新特性 1、字符串模版 字符串模版可以让开发者更简洁的…

目录

一、Java21新特性

1、字符串模版

2、scoped values

3、record pattern

4、switch格式匹配

5、可以在switch中使用when

6、Unnamed Classes and Instance Main Methods

7、Structured Concurrency


一、Java21新特性

1、字符串模版

        字符串模版可以让开发者更简洁的进行字符串拼接(例如拼接sql,xml,json等)。该特性并不是为字符串拼接运算符+提供的语法,也并非为了替换StringBuffer和StringBuilder。

利用STR模版进行字符串与变量的拼接

public class StringTest {public static void main(String[] args) {String sport="basketball";String msg=STR."I like \{sport}";System.out.println(msg);}
}
预览功能(java文件不要加package包信息)
$>javac --enable-preview -source 21 StringTest.java
注: StringTest.java 使用 Java SE 21 的预览功能。
注: 有关详细信息,请使用 -Xlint:preview 重新编译。
$>java --enable-preview  StringTest
I like basketball

上面使用的STR是java中定义的模版处理器,它可以将变量的值取出,完成字符串的拼接。在每个java源文件中都引入了一个public static final修饰的STR属性,STR通过打印STR可以知道它是java.lang.StringTemplate,是一个接口。

Processor<String, RuntimeException> STR = StringTemplate::interpolate;default String interpolate() {return StringTemplate.interpolate(fragments(), values());}static String interpolate(List<String> fragments, List<?> values) {Objects.requireNonNull(fragments, "fragments must not be null");Objects.requireNonNull(values, "values must not be null");int fragmentsSize = fragments.size();int valuesSize = values.size();if (fragmentsSize != valuesSize + 1) {throw new IllegalArgumentException("fragments must have one more element than values");}JavaTemplateAccess JTA = SharedSecrets.getJavaTemplateAccess();return JTA.interpolate(fragments, values);}public static JavaTemplateAccess getJavaTemplateAccess() {var access = javaTemplateAccess;if (access == null) {try {Class.forName("java.lang.runtime.TemplateSupport", true, null);access = javaTemplateAccess;} catch (ClassNotFoundException e) {}}return access;}final class TemplateSupport implements JavaTemplateAccess {
...@Overridepublic String interpolate(List<String> fragments, List<?> values) {int fragmentsSize = fragments.size();int valuesSize = values.size();if (fragmentsSize == 1) {return fragments.get(0);}int size = fragmentsSize + valuesSize;String[] strings = new String[size];int i = 0, j = 0;for (; j < valuesSize; j++) {strings[i++] = fragments.get(j);strings[i++] = String.valueOf(values.get(j));}strings[i] = fragments.get(j);return JLA.join("", "", "", strings, size);}

其他使用示例,在STR中可以进行基本的运算(支持三元运算)

int x=10,y=20;
String result=STR."\{x} + \{y} = \{x+y}";
System.out.println(result);//10 + 20 = 30

调用方法

String res=STR."获取一个随机数:\{Math.random()}";
System.out.println(res);

获取属性

String res1=STR."int的最大值是:\{Integer.MAX_VALUE}";
System.out.println(res1);

查看时间

String res2=STR."现在时间:\{new SimpleDateFormat("yyyy-MM-dd").format(new Date()) }";
System.out.println(res2);

计数操作

int index=0;
String result=STR."\{index++},\{index++},\{index++}";
System.out.println(result);

获取数组数据

String[] cars ={"bmw","ben","audi"};
String result = STR. "\{ cars[0] },\{ cars[1] },\{ cars[2] }" ;
System.out.println(result);

拼接多行数据

String[] cars ={"bmw","ben","audi"};
String result = STR. """\{cars[0] }\{ cars[1] }\{ cars[2] }""" ;
System.out.println(result);

自定义模版

    public static void main(String[] args) {var INTER = StringTemplate.Processor.of((StringTemplate st) -> {StringBuilder sb = new StringBuilder();Iterator<String> iterator = st.fragments().iterator();for (Object value : st.values()) {sb.append(iterator.next());sb.append(value);}sb.append(iterator.next());return sb.toString();});int x = 10, y = 20;String result = INTER. "\{ x } + \{ y } = \{ x + y }" ;System.out.println(result);}

2、scoped values

scoped values是一个隐藏的方法参数,只有方法可以访问scoped values,它可以让两个方法之间传递参数时无需声明形参。例如在UserDao类中编写savaUser方法,LogDao类中编写了saveLog方法,那么在保存用户的时候需要保证事务,此时就需要在service层获取Connection对象,然后将该对象分别传入到两个Dao的方法中,但对于savaUser方法来说并不是直接使用Connection对象,却又不得不在方法的形参中写上该对象,其实仅从业务上来看,该方法中只要传入User对象就可以了。

public class ScopeedValueTest {private static final ScopedValue<String> GIFT=ScopedValue.newInstance();public static void main(String[] args) {ScopeedValueTest scopeedValueTest=new ScopeedValueTest();scopeedValueTest.giveGift();}public void giveGift(){ScopedValue.where(GIFT,"手机").run(()->recieveGift());}public void recieveGift(){System.out.println(GIFT.get());}
}

多线程环境是否会出问题

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class ScopeedValueTest {private static final ScopedValue<String> GIFT = ScopedValue.newInstance();public static void main(String[] args) {ScopeedValueTest t = new ScopeedValueTest();ExecutorService pool = Executors.newCachedThreadPool();for (int i = 0; i < 10; i++) {pool.submit(() -> {t.giveGift();});}pool.shutdown();}public void giveGift() {ScopedValue.where(GIFT, Thread.currentThread().getName()).run(() -> recieveGift());}public void recieveGift() {System.out.println(GIFT.get());}
}

变更中间数值,和当前线程绑定关系

public class ScopeedValueTest {private static final ScopedValue<String> GIFT = ScopedValue.newInstance();public static void main(String[] args) {ScopeedValueTest t = new ScopeedValueTest();t.giveGift();}public void giveGift() {ScopedValue.where(GIFT, "500").run(() -> recieveMiddleMan());}//中间人public void recieveMiddleMan() {System.out.println(GIFT.get());//500ScopedValue.where(GIFT, "200").run(() -> recieveGift());//中间人抽成300System.out.println("recieveMiddleMan=" + GIFT.get());//500}public void recieveGift() {System.out.println(GIFT.get());//200}
}

3、record pattern

通过该特性可以解构record类型中的值,例如:

public class RecordTest {public static void main(String[] args) {Student s = new Student(1, "小米");print(s);}static void print(Object obj) {if (obj instanceof Student(int a, String b)) {System.out.println("a=" + a);System.out.println("b=" + b);}}
}record Student(int id, String name) {}

4、switch格式匹配

public class SwitchTest {public static void main(String[] args) {String str="hello";String result = getObjInstance(str);System.out.println(result);}public static String getObjInstance(Object obj) {return switch (obj) {case null -> "空对象";case Integer i -> "Integer对象" + i;case String s -> "String对象" + s;default -> obj.toString();};}
}

5、可以在switch中使用when

public class Switch02Test {public static void main(String[] args) {yesOrNo("yes");}public static void yesOrNo(String str) {switch (str) {case null -> {System.out.println("空对象");}case String swhen s.equalsIgnoreCase("yes") -> {System.out.println("确定");}case String swhen s.equalsIgnoreCase("no") -> {System.out.println("取消");}case String s -> {System.out.println("请输入yes或no");}}}
}

6、Unnamed Classes and Instance Main Methods

对于初学者来说,写第一个HelloWorld代码有太多的概念,为了方便初学者快速编写第一段java代码,这里提出了无名类和实例main方法,下面代码可以直接运行编译,相当于是少了类的定义,main方法的修饰符和形参也省略掉了。

void main() {System.out.println("Hello,World!");
}

7、Structured Concurrency

结构化并发:该特性主要作用是在使用虚拟线程时,可以使任务和子任务的代码编写起来可读性更强,维护性更高,更加可靠。

import java.util.concurrent.ExecutionException;
import java.util.concurrent.StructuredTaskScope;
import java.util.function.Supplier;public class Test {public static void main(String[] args) {Food f = new Test().handle();System.out.println(f);}Food handle() {try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {Supplier<String> fork1 = scope.fork(() -> "奶茶做好了");//任务1:线程1Supplier<String> fork2 = scope.fork(() -> "烤串烤好了");//任务2:线程2scope.join().throwIfFailed();//失败传播//当两个任务都成功后,最终才能吃饭return new Food(fork1.get(), fork2.get());} catch (ExecutionException e) {throw new RuntimeException(e);} catch (InterruptedException e) {throw new RuntimeException(e);}}
}record Food(String f1, String f2) {}

Java17-20新特性

一个程序员最重要的能力是:写出高质量的代码!!
有道无术,术尚可求也,有术无道,止于术。
无论你是年轻还是年长,所有程序员都需要记住:时刻努力学习新技术,否则就会被时代抛弃!

http://www.hkea.cn/news/919708/

相关文章:

  • 市桥网站建设sem论坛
  • 猎头公司是做什么的可靠吗排名优化外包公司
  • 扶贫网站建设关键词查询神器
  • 沈阳酒店企业网站制作公司2023年9月疫情又开始了吗
  • 厦门专业网站建设如何快速推广一个新产品
  • 帮人做传销网站违法吗seo网站排名助手
  • 如何做优品快报下的子网站营销型网站建设目标
  • 用织梦做网站调用乱码营业推广是什么意思
  • 做走私网站北京口碑最好的it培训机构
  • 网站建设OA系统开发it培训机构哪家好
  • 网站运维可以做哪些域名查询网站入口
  • 网站开发的基本语言外贸平台自建站
  • 女生自己做网站营销方法有哪些
  • 怎么自己做网站吓别人金融网站推广圳seo公司
  • 彩票网站的客服有做吗海淀seo搜索优化多少钱
  • 河源哪有做网站网页模板设计
  • 手机网站可以做英文版本吗近三天时政热点
  • 怎么做网站游戏网络优化排名培训
  • ic外贸网站建设黑帽seo技巧
  • 实业有限公司网站怎么做百度一下了你就知道官网
  • 企业电子商务网站推广平台有哪些渠道
  • 本地用织梦做网站百度的网站网址
  • 基础展示营销型型网站新闻发稿平台有哪些
  • 做游戏赚钱的网站最新新闻热点事件2022
  • 商务网站建设哪家好推广代理公司
  • 自己做网站是否要买云主机西安百度提升优化
  • 成都注册公司哪个区好分析网站推广和优化的原因
  • 模板建站杭州seo泽成
  • 济南网站建设公司川芎网络怎么注册自己的网址
  • linux下安装wordpress关键词优化排名查询