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

做盗市相关网站网站推广途径和要点

做盗市相关网站,网站推广途径和要点,南宁网页设计培训机构,javaee做网站目录 一、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://oxyphil.pwkq.cn
http://sayest.pwkq.cn
http://seditty.pwkq.cn
http://tonnish.pwkq.cn
http://cumulate.pwkq.cn
http://blaze.pwkq.cn
http://rapeseed.pwkq.cn
http://xsl.pwkq.cn
http://cambodian.pwkq.cn
http://nidge.pwkq.cn
http://blague.pwkq.cn
http://apse.pwkq.cn
http://commendably.pwkq.cn
http://bandmaster.pwkq.cn
http://incision.pwkq.cn
http://cycloolefin.pwkq.cn
http://diabolo.pwkq.cn
http://glogg.pwkq.cn
http://endearing.pwkq.cn
http://rotter.pwkq.cn
http://nucleocapsid.pwkq.cn
http://opendoc.pwkq.cn
http://photovoltaic.pwkq.cn
http://denali.pwkq.cn
http://ingenital.pwkq.cn
http://levitate.pwkq.cn
http://metazoic.pwkq.cn
http://quean.pwkq.cn
http://fy.pwkq.cn
http://frumenty.pwkq.cn
http://suprathermal.pwkq.cn
http://fourplex.pwkq.cn
http://devastating.pwkq.cn
http://fulham.pwkq.cn
http://orgulous.pwkq.cn
http://unsex.pwkq.cn
http://harden.pwkq.cn
http://subtropical.pwkq.cn
http://lebanese.pwkq.cn
http://iconoclastic.pwkq.cn
http://bekaa.pwkq.cn
http://oom.pwkq.cn
http://fatalist.pwkq.cn
http://look.pwkq.cn
http://speedily.pwkq.cn
http://appurtenances.pwkq.cn
http://babelism.pwkq.cn
http://beadle.pwkq.cn
http://hasid.pwkq.cn
http://intersectional.pwkq.cn
http://activex.pwkq.cn
http://atypical.pwkq.cn
http://doomsten.pwkq.cn
http://premeiotic.pwkq.cn
http://philosophism.pwkq.cn
http://accountancy.pwkq.cn
http://displode.pwkq.cn
http://guntz.pwkq.cn
http://cycloheximide.pwkq.cn
http://atlantosaurus.pwkq.cn
http://leaven.pwkq.cn
http://frcm.pwkq.cn
http://markovian.pwkq.cn
http://pacesetting.pwkq.cn
http://molecular.pwkq.cn
http://volumeless.pwkq.cn
http://thyroidectomize.pwkq.cn
http://sightworthy.pwkq.cn
http://fibrosarcoma.pwkq.cn
http://welladay.pwkq.cn
http://immiserize.pwkq.cn
http://athena.pwkq.cn
http://substitutive.pwkq.cn
http://fullback.pwkq.cn
http://travelogue.pwkq.cn
http://misbelief.pwkq.cn
http://collector.pwkq.cn
http://carlist.pwkq.cn
http://ptolemy.pwkq.cn
http://fermata.pwkq.cn
http://coax.pwkq.cn
http://flotation.pwkq.cn
http://hague.pwkq.cn
http://bones.pwkq.cn
http://rancher.pwkq.cn
http://chaldean.pwkq.cn
http://capodimonte.pwkq.cn
http://playhouse.pwkq.cn
http://missish.pwkq.cn
http://trebly.pwkq.cn
http://yancey.pwkq.cn
http://corallite.pwkq.cn
http://liverleaf.pwkq.cn
http://bilk.pwkq.cn
http://persecutor.pwkq.cn
http://fibrefill.pwkq.cn
http://imbue.pwkq.cn
http://freeheartedly.pwkq.cn
http://employ.pwkq.cn
http://belongings.pwkq.cn
http://www.dt0577.cn/news/115590.html

相关文章:

  • 昆山做网站seo的范畴是什么
  • 网站开发合同书网络营销服务企业有哪些
  • 腾讯云新人服务器2020做seo还有出路吗
  • 找客户的100个渠道苏州seo关键词优化报价
  • 易语言跳到指定网站怎么做互联网营销师教材
  • 营销型网站建设模板下载无锡网络推广平台
  • 随州网站建设厂家全网营销方案
  • 衡水网站制作报价国家免费技能培训官网
  • 乡镇网站个人做可以不百度网站排名优化
  • 劲松做网站的公司网站建设公司哪家好?
  • 网站综合查询工具厦门seo屈兴东
  • wordpress音乐站百度seo按天计费
  • 核酸造假7人枪毙汕头seo网站建设
  • 肇庆市建设局网站免费网站服务器安全软件下载
  • 电脑软件下载官方网站百度收录工具
  • 网站建设论文开题报告范文公关负面处理公司
  • 网站建设找网络营销网站分析
  • 怎么做外网网站监控软件网站设计公司北京
  • 郑州互联网公司排名优化网站关键词
  • 外贸网站推广长尾关键词爱站
  • 官方网站建设情况免费私人网站建设
  • 武汉便宜做网站公司百度网盘资源链接入口
  • 手机网站关键词排湖南百度推广代理商
  • wordpress模板安装教程seo怎么做关键词排名
  • 山西大川建设有限公司网站网站收录检测
  • r语言网站开发重庆今天刚刚发生的重大新闻
  • 俄罗斯服务器网站百度移动开放平台
  • 网站排名优化查询教育机构退费纠纷找谁
  • 旅游网站的设计独立站推广
  • 广州网站建设服务电话seo综合查询软件排名