Welcome everyone

Kotlin 入门体验

Kotlin 汪明鑫 630浏览 0评论

初步认识Kotlin

Kotlin是Java的儿子

Kotlin完全兼容Java,Java 可以调Kotlin,Kotlin也可以调Java

Kotlin比Java更简洁、更安全

Kotlin一般安卓使用的较多

 

都是运行在jvm上的

 

Hello World

开发环境加上Kotlin

Kotlin可以和Java混合开发

 

右击新建Kotlin File/Class, 文件后缀是 .kt

package pers.wmx.springbootfreemarkerdemo

fun main(args:Array<String>){
    println("hello world");
}

像运行Java main方法那样即可

 

对比Java代码和Kotlin代码

Java代码:

package pers.wmx.springbootfreemarkerdemo;
 
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
 
import java.util.List;
 
/**
 * @author wmx
 * @date 2019-11-22
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class RedisTests {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
 
    @Test
    public void testString(){
        stringRedisTemplate.opsForValue().set("haha","hehe");
    }
 
    @Test
    public void testPipelined(){
        List<Object> objects = stringRedisTemplate.executePipelined(new RedisCallback<Object>() {
            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
 
                connection.set("super carry".getBytes(), "doinb".getBytes());
                connection.set("tian".getBytes(), "神僧".getBytes());
 
                connection.get("super carry".getBytes());
                connection.get("tian".getBytes());
 
                return null;
            }
        });
        System.out.println(objects);
    }
 
    @Test
    public void testTransaction(){
        stringRedisTemplate.setEnableTransactionSupport(true);
        stringRedisTemplate.opsForValue().set("1","a");
        stringRedisTemplate.opsForValue().set("2","b");
        stringRedisTemplate.opsForValue().set("3","c");
    }
}

 

右击文件选择转换成kotlin代码

 

package pers.wmx.springbootfreemarkerdemo
 
import lombok.extern.slf4j.Slf4j
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.dao.DataAccessException
import org.springframework.data.redis.connection.RedisConnection
import org.springframework.data.redis.core.RedisCallback
import org.springframework.data.redis.core.StringRedisTemplate
import org.springframework.test.context.junit4.SpringRunner
 
/**
 * @author wmx
 * @date 2019-11-22
 */
@RunWith(SpringRunner::class)
@SpringBootTest
@Slf4j
class RedisTests {
    @Autowired
    private val stringRedisTemplate: StringRedisTemplate? = null
 
    @Test
    fun testString() {
        stringRedisTemplate!!.opsForValue().set("haha", "hehe")
    }
 
    @Test
    fun testPipelined() {
        val objects = stringRedisTemplate!!.executePipelined { connection ->
            connection.set("super carry".toByteArray(), "doinb".toByteArray())
            connection.set("tian".toByteArray(), "神僧".toByteArray())
 
            connection.get("super carry".toByteArray())
            connection.get("tian".toByteArray())
 
            null
        }
        println(objects)
    }
 
    @Test
    fun testTransaction() {
        stringRedisTemplate!!.setEnableTransactionSupport(true)
        stringRedisTemplate.opsForValue().set("1", "a")
        stringRedisTemplate.opsForValue().set("2", "b")
        stringRedisTemplate.opsForValue().set("3", "c")
    }
}

 

 

学习资料

https://www.kotlincn.net/

https://www.w3cschool.cn/uwota/

https://www.cnblogs.com/webor2006/category/1079508.html

 

 

转载请注明:汪明鑫的个人博客 » Kotlin 入门体验

喜欢 (0)

说点什么

您将是第一位评论人!

提醒
avatar
wpDiscuz