【javascript – Kotlin中的平台常见类是什么?】教程文章相关的互联网学习教程文章

Android开发——在Java项目中引用Kotlin的库出现的问题【代码】【图】

在Android开发当中,我遇到Kotlin底层的库比Java的库好用。看到那个第三方库支持Java实现,于是我就想在Java底层项目中调用Kotlin底层库。但是我编译的时候,遇到了下面的错误错误: 无法访问Unit 找不到kotlin.Unit的类文件解决方案apply plugin: kotlin-android apply plugin: kotlin-android-extensions在当前的module的build.gradle中加入上面的代码。希望我的这篇博客对Android开发的小伙伴有用!推广一下我微信公众号,我会经...

使用Kotlin Data类替换Java DTO类的问题【代码】

我读到了Kotlin数据类,并认为它们在描述数据传输对象(DTO)的情况下非常有用.在我的Java项目中,我已经有了用Java编写的DTO类,类似于:public class Tweet {private String id;private String profileId;private String message;public Tweet() {}public String getId() {return id;}public String getProfileId() {return profileId;}public String getMessage() {return message;}public void setId(String id) {this.id = id;}pub...

Kotlin反射与Java的互操作性【代码】

在编写适用于Java和Kotlin的反射代码时,开发人员应该注意哪些警告? 例如,我有一个使用反射的现有库,它适用于Java.但是,当我在Kotlin中使用相同的代码时,我的反射代码似乎没有在字段上获取注释. 以下是我注意到的一些差异. 1.获取Class实例// Example 1.1 - Java Class<?> userClass = User.class; // From a class name userClass = userInstance.getClass(); // OR from an instance在Kotlin中获取Java类实例// Exam...

java – Kotlin Spring AliasFor【代码】

我一直在尝试在Kotlin项目中使用Springs @AliasFor注释,但似乎注释参数上的注释在运行时不可见(因此它不会被Spring选中). 例如:@RequestMapping(method = arrayOf(RequestMethod.POST)) annotation class PostMapping(@get:AliasFor(annotation = RequestMapping::class, attribute = "value")vararg val value: String = arrayOf())和…// Returns empty array instead of array with the @AliasFor annotation PostMapping::cla...

如何在Kotlin中为@PropertySource创建Java数组?【代码】

我正在尝试为基于Spring的应用程序配置@PropertySource. 在Java中,我可以这样做:@PropertySource(value = {"application.properties","other.properties" })我在Kotlin中尝试过arrayOf,但最终我的类型不匹配:@PropertySource(value = arrayOf("application.properties", "other.properties"))什么是正确的方式去这里?解决方法:值注释参数在Kotlin中以特殊方式处理(遵循其在Java中的特殊处理),如果它具有数组类型,Kotlin将其转换...

java – 在Kotlin中编写单元测试,共享变量?【代码】

我正在尝试在Kotlin中创建一些功能测试,以使用Rest Assured库向Cart Java服务发出请求. 由于我希望测试在程序上表现,我希望我可以存储第一个A??PI请求的结果并将其传递给下一个单元测试. 即 createCartTest() – > cartId – > getCartForWebsiteTest(cartId)class CartTest : RestAssuredSupport {val port = 8080val url = "http://localhost:"val cartId = null/*** Create a cart object*/@Test fun createCartTest() {given(...

java – Android kotlin覆盖onCreateView()方法内部的接口方法【代码】

我是Kotlin的新手.我有一个包含两个方法定义的接口:fun onSuccess(result: T) fun onFailure(e: Exception)现在,在我的片段中,我实现了这个接口,并希望在里面使用这些方法:override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,savedInstanceState: Bundle?): View? {....................................override fun onSuccess(result: String) {}override fun onFailure(e: Exception) {} }在java中...

在Kotlin项目中包含使用Java9或Java10构建的maven依赖项【代码】

是否可以包含Java9(或10)依赖项?因为Kotlin到目前为止只能编译到Java 8.我收到了一个java.lang.UnsupportedClassVersionError错误,显然说明我所包含的JAR /依赖项中的类已由更新版本的Java Runtime编译.但是我无法指定一个更新的JDK,它能够运行用旧版本编译的类(Kotlin的东西?).<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.7.0</version><configuration><rel...

java – 检查null时的惯用Kotlin【代码】

在Java中,我会做这样的事情:class Person {private Record record;public String name() {record().get("name");}private Record record() {if (record == null) {refetch();}return record;}private void refetch() {record = service.doSomething()}}在Kotlin,我有这个等价的代码:class Person(private var record: Record?) {fun name() : String {record().get("name");}private fun record() : Record {record ?: refetch()...

java – 使用Proxy时,为什么Kotlin会抛出IllegalArgumentException【代码】

这是使用InvocationHandler的Kotlin等效的Java代码:override fun invoke(proxy: Any?, method: Method?, args: Array<out Any>?): Any {println("before httprequest--->" + args)val ret = method!!.invoke(obj, args)println("after httprequest--->")return ret }Java代码:public Object invoke(Object o, Method method, Object[] args) throws Throwable {System.out.println("jdk--------->http" + args);Object result=m...

spring – Java Stream with :: new to Kotlin【代码】

我正在尝试将以下Spring Security代码从Java转换为Kotlin. Java的:Collection<? extends GrantedAuthority> authorities =Arrays.stream(claims.get(AUTHORITIES_KEY).toString().split(",")).map(SimpleGrantedAuthority::new).collect(Collectors.toList());科特林:val authorities = Arrays.stream(claims[AUTHORITIES_KEY].toString().split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()).map(SimpleGrant...

java – kotlin内部类无法访问私有方法吗?【代码】

嗨,我是kotlin的新手,并试图从内部匿名类中调用私有函数. 以下是电话:object callback : Callback<ResponseBody> {override fun onResponse(call: Call<ResponseBody>?, response: Response<ResponseBody>?) {Log.d("jjj", " response is " + response.toString())displaySuccess()}override fun onFailure(call: Call<ResponseBody>?, t: Throwable?) {}}private fun displaySuccess(){Toast.makeText(activity,"succesful", Toa...

javascript – 如何在没有stdlib的情况下使用KotlinJS?【代码】

因为在大多数情况下我们有:dependencies {compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version" }Kotlin标准库对我来说似乎很大.我想通过仅创建我需要的外部方法来创建我自己的stdlib(可以更小)来最小化它. 我试图删除该方法,它编译,但生成的JS代码具有:if (typeof kotlin === 'undefined') {throw new Error("Error loading module 'streaking'. Its dependency 'kotlin' was not found. Please, check whether 'k...

java – Kotlin – 获取Ints的ArrayList中的最大值【代码】

我有这个ArrayListvar amplititudes : ArrayList<Int> = ArrayList()amplititudes.add(1)amplititudes.add(2)amplititudes.add(3)amplititudes.add(4)amplititudes.add(3)amplititudes.add(2)amplititudes.add(1)现在我想获得最大值,即4.找到最大元素的最简单方法是什么?我知道max()方法,但它会强迫我使用?返回值,因为它可能为null.有没有比这更好的解决方案?解决方法:您可以使用内置功能:val amplitudes = listOf(1,2,3,4,3,2...

android – 当我们开始新的活动到kotlin为什么我们把.java放在intent而不是.kt

根据kotlin doc,我们使用以下语法开始新活动 startActivity(Intent(this @ MainActivity,NextActivity :: class.java)) 它的kotlin为什么我们在课后添加.java?为什么不kt?解决方法:因为NextActivity :: class为您提供了KClass< NextActivity>,并且KClass有一个名为java的方法/扩展属性,它为您提供了java.lang.Class< NextActivity>对于给定的类. 你甚至可以check out the source-code for that java property.