• [Kotlin] Using Kotlin from Java


    Create a Kotlin class:

    package com.rsk.kotlin
    
    class Meeting(val title: String) {
        // in Java, you can use getLocation and setLocation
        var location = ""
        // you cannnot directly use m.description = 'xxx' or System.out.println(m.description)
        // you have to add @JvmField, so that in Java we can access as a property
        @JvmField
        var description = ""
    
        //  This class might throw exception
        @Throws(MeetingException::class)
        fun addAttendee(attendee: String) {
            if (attendee.isNullOrEmpty()) throw MeetingException("Attendee must have a name")
        }
    
        companion object {
            // by marking @JvmField & @JvmStatic, we can make it easy to use in java
            @JvmField
            val APP_VERION = 1
    
            @JvmStatic
            fun getAppVersion(): Int {
                return APP_VERION
            }
        }
    }
    
    class MeetingException(message: String): Exception(message) {
    
    }

    Create a Java class to use Kotlin:

    package com.rsk.java;
    
    import com.rsk.kotlin.Meeting;
    import com.rsk.kotlin.MeetingException;
    
    public class Program {
        public static void main(String[] args) {
            Meeting board = new Meeting("Board Meeting");
            board.setLocation("London");
            System.out.println(board.getLocation());
    
            board.description = "React meeting";
            System.out.println(board.description);
    
            System.out.println(Meeting.APP_VERION);
            System.out.println(Meeting.getAppVersion());
    
            try {
                board.addAttendee("");
            } catch(MeetingException me) {
                me.printStackTrace();
            }
    
        }
    }
  • 相关阅读:
    Working with Deployment Configurations in CodeDeploy
    ECS 容器实例生命周期
    设置 API Gateway 金丝雀版本部署
    Elastic Beanstalk 滚动环境配置更新
    Kinesis Data Firehose 中的数据保护
    为 API Gateway REST API 资源启用 CORS
    高级 AWS Elastic Beanstalk 环境配置
    Amazon SWF Actors
    AWS CloudFormation 模板结构
    字符编码
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13823246.html
Copyright © 2020-2023  润新知