Kotlin使用静态变量与静态方法详解
作者:水月沐風 时间:2021-10-11 07:03:40
前言
在日常开发过程中,静态变量和 静态方法 是我们常见的用法,Java中相信大家并不陌生了,那么在 Kotlin 中该如何使用呢?
其实很简单,只需要一个将变量和方法包含在 companion object 域中即可,比如这样:
class Constant {
companion object {
// 接口根地址
const val BASE_URL = "http://xxxx.xxx.xxx/"
// 友盟
const val UMENG_APP_KEY = "xxxxxxxxxx"
const val UMENG_CHANNEL = "umeng"
// 微博
const val WEIBO_APP_KEY = "xxxxxxxx"
const val WEIBO_SECRET = "xxxxxxxxxx"
fun getVideoFactor(){
// do some work
}
}
}
看后是不是很简单?在纯kotlin代码中可以直接这样使用:
//初始化各平台的APIKey
PlatformConfig.setWeixin(Constant.WECHAT_APP_ID, Constant.WECHAT_APP_SECRET)
PlatformConfig.setSinaWeibo(Constant.WEIBO_APP_KEY, Constant.WEIBO_SECRET, Constant.WEIBO_AUTH_RETURN_URL)
然而,如果我们使用的是Java和kotlin混合开发,在Java代码中就无法通过 Constant.静态变量 的方式来使用静态变量或者方法来,而是通过如下方式:
//初始化各平台的APIKey
PlatformConfig.setWeixin(Constant.Companion.WECHAT_APP_ID, Constant.WECHAT_APP_SECRET)
PlatformConfig.setSinaWeibo(Constant.Companion.WEIBO_APP_KEY, Constant.WEIBO_SECRET, Constant.WEIBO_AUTH_RETURN_URL)
如果我们想像kotlin那样直接通过 类名.静态变量 方式使用呢?我们可以借助于注解 @JvmField 和 @JvmStatic 来分别标注静态变量和静态方法,之后我就能在Java代码中像以前方式那样直接使用静态的成员啦!
例如这样:
/**
* @author moosphon on 2018/12/12
* desc: 异常的统一处理者
*/
class ExceptionHandler {
companion object {
@JvmField
var errorCode = NetRequestStatus.UNKNOWN_ERROR
@JvmField
var errorMessage = "请求失败,请稍后重试"
@JvmStatic
fun handleException(e : Throwable): String{
e.printStackTrace()
when(e){
is SocketException -> {
Logger.e("ExceptionHandler", "网络连接异常: " + e.message)
errorCode = NetRequestStatus.NETWORK_ERROR
errorMessage = "网络连接异常"
}
is JsonParseException -> {
Logger.e("ExceptionHandler", "数据解析异常: " + e.message)
errorCode = NetRequestStatus.PARSE_ERROR
errorMessage = "数据解析异常"
}
else -> {
try {
Logger.e("ExceptionHandler", "其他错误: " + e.message)
} catch (e1: Exception) {
Logger.e("ExceptionHandler", "未知错误: " + e.message)
}
errorCode = NetRequestStatus.UNKNOWN_ERROR
errorMessage = "未知错误,一起祷告快点好起来吧~"
}
}
return errorMessage
}
}
}
前段时间比较忙,之后会继续为大家带来kotlin方面的文章,大家拭目以待。
来源:https://www.jianshu.com/p/03b21e3cf9cf
标签:kotlin,静态方法,静态变量
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java中maven下载和安装步骤说明
2022-03-05 23:07:59
java实现在性能测试中进行业务验证实例
2022-10-15 09:50:11
SpringBoot工程搭建打包、启动jar包和war包的教程图文详解
2022-05-04 22:09:10
![](https://img.aspxhome.com/file/2023/2/69892_0s.png)
SpringBoot整合Druid数据源过程详解
2023-06-03 19:47:14
![](https://img.aspxhome.com/file/2023/3/103973_0s.png)
利用Spring Session和redis对Session进行共享详解
2021-10-18 18:22:45
![](https://img.aspxhome.com/file/2023/3/105943_0s.png)
C#简单了解接口(Interface)使用方法
2022-01-30 18:33:57
修改maven本地仓库路径的方法
2022-08-09 13:44:16
JVM致命错误日志详解(最新推荐)
2023-08-22 07:42:30
教你怎么用SpringBoot+Mybati-Plus快速搭建代码
2023-08-07 12:42:10
![](https://img.aspxhome.com/file/2023/1/58081_0s.png)
详解java整合solr5.0之solrj的使用
2023-07-23 03:12:06
![](https://img.aspxhome.com/file/2023/3/57493_0s.png)
老生常谈比较排序之归并排序(递归)
2022-05-15 11:43:15
![](https://img.aspxhome.com/file/2023/3/72633_0s.jpg)
Java 网络编程socket编程等详解
2023-09-09 16:43:12
Flutter 分页功能表格控件详细解析
2023-09-22 20:02:45
![](https://img.aspxhome.com/file/2023/2/104692_0s.gif)
c#扫描图片去黑边(扫描仪去黑边)
2022-11-06 11:45:55
解决Spring Cloud Feign 请求时附带请求头的问题
2022-12-11 04:29:46
springboot读取application.yaml文件数据的方法
2023-09-06 05:29:24
![](https://img.aspxhome.com/file/2023/6/63086_0s.jpg)
java中的SpringBoot框架
2022-02-10 16:50:05
详解SpringBoot自定义配置与整合Druid
2023-07-24 20:20:28
![](https://img.aspxhome.com/file/2023/9/96269_0s.jpg)
C# 7.0中解构功能详解
2022-08-11 21:06:34
![](https://img.aspxhome.com/file/2023/2/88352_0s.png)
c#数字图像处理的3种方法示例分享
2021-10-09 09:56:51