首页 > 编程语言 >Java: Record

Java: Record

时间:2022-10-30 19:00:18浏览次数:36  
标签:Java DuRecord Point System Record println public pgeovindu

/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 *
 * 历史版本: JDK 17.01
 * 2022-09-12 创建者 geovindu
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc DuRecord.java
 * Interface
 * Record
 * Annotation
 * Enum
 * */



package CoreJava.twelfth;

import java.util.*;

/**
 *
 *
 * */
public class DuRecord {

    /**
     *
     *
     * */
    public record Point(double x, double y)
    {
        // A custom constructor
        public Point() { this(0, 0); }
        // A method
        public double distanceFromOrigin()
        {
            return Math.hypot(x, y);
        }
        // A static field and method
        public static Point ORIGIN = new Point();
        public static double distance(Point p, Point q)
        {
            return Math.hypot(p.x - q.x, p.y - q.y);
        }
    }
    /**
     *
     *
     * */
    public record PointInTime(double x, double y, Date when) { }
    /**
     *
     *
     * */
    public record Range(int from, int to)
    {
        // A compact constructor
        public Range
        {
            if (from > to) // Swap the bounds
            {
                int temp = from;
                from = to;
                to = temp;
            }
        }
    }


}

  

调用:

// Record
            DuRecord.Point pgeovindu =new DuRecord.Point(3, 4);
            System.out.println("pgeovindu 坐标: " + pgeovindu.x() + " " + pgeovindu.y());
            System.out.println("距离原点: " + pgeovindu.distanceFromOrigin());
            // Same computation with static field and method
            System.out.println("距离原点: " +  DuRecord.Point.distance( DuRecord.Point.ORIGIN, pgeovindu));

            // A mutable record
            var ptgeovindu = new  DuRecord.PointInTime(3, 4, new Date());
            System.out.println("前: " + ptgeovindu);
            ptgeovindu.when().setTime(0);
            System.out.println("后: " + ptgeovindu);

            // Invoking a compact constructor

            var rgeovindu = new  DuRecord.Range(4, 3);
            System.out.println("rgeovindu: " + rgeovindu);

  

输出:

pgeovindu 坐标: 3.0 4.0
距离原点: 5.0
距离原点: 5.0
前: PointInTime[x=3.0, y=4.0, when=Sun Oct 30 18:45:44 CST 2022]
后: PointInTime[x=3.0, y=4.0, when=Thu Jan 01 08:00:00 CST 1970]
rgeovindu: Range[from=3, to=4]

  

标签:Java,DuRecord,Point,System,Record,println,public,pgeovindu
From: https://www.cnblogs.com/geovindu/p/16841921.html

相关文章

  • javaweb期中考试总结
    本次期中考试的内容和往年的类似,需要实现的功能为增删改查,利用的工具有IDEA集成环境,TomCat本地部署的服务器,MySQL数据库。利用的技术有JDBC规范,HTML标签语言,以及利用Servle......
  • Java学习——初始化对象
    一、如何使用通过注释@PostConstruct标明是初始化方法@PostConstructpublicvoidinit(){}二、注意事项初始化方法和构造方法不同,构造方法只是生成了一个对象,而初始......
  • JAVA 参数数量可变的方法 歧义
    publicclassT12{publicvoidss(Object...list){for(Objecto:list){System.out.println(o);}}publicvoidss(inti1......
  • JavaScript--Express框架重构项目逻辑
     1.Express框架介绍 *Express是高度包容、快速而极简的Node.js-Web框架   中间件  上手简单,学习门槛低具有丰富的基础API支持强大的路由功能灵活的......
  • JavaScript 使用 Notification 发送系统通知
    使用Notification可以在系统级别发送页面外部显示的桌面通知,即使浏览器在后台运行也可以向用户发出消息检查权限发送通知需要用户授权,通过只读属性Notification.per......
  • 2.单例模式(Singleton)JAVA语言实现
    单例模式实现1.私有的构造方法2.私有的静态的当前类对象作为属性3.共有的静态的方法返回当前类对象单例模式实现方式有三种(指的时对象的加载)1.饿汉式(立即加载)对象启动时就......
  • cryptoJs DES_CBC_Pkcs7 转成 Java
    前端DES加密:importcryptoJsfrom'crypto-js';//DES加密functionencrypt(message,key,iv){//字符串转16进制constkeyHex=cryptoJs.enc.Utf8.parse......
  • Java基础------方法
    什么是方法Java方法是语句的集合,他们在一起执行一个功能设计方法的原则:方法的本意是功能模块,就是实现某个功能的语句块的集合。我们设计方法的时候,最好保持方法的原子性,......
  • JavaScript学习
    (只用于自己学习,只是个目录形式,具体内容涉及不多)JavaScript用于用户和网页之间的交互,比如提交的时候,用于用户名是否为空的判断 1.document是JavaScript的内置对象,代表浏......
  • JAVAWeb - HttpServletRequest
    一.Request入门HttpServletRequest:代表客户端的请求,通过http协议访问服务器,Http请求中的所有信息会被封装到HttpServletRequest,通过这个HttpServletRequest的方法,获得客户......