zoukankan      html  css  js  c++  java
  • Kotlin Groovy Style Builder

    fun main(args:Array<String>):Unit{
    
        val x = a{
            aa{
                +"01.01"
                +"01.02"
            }
            aa{
                +"02.01"
                +"02.02"
            }
        }
        print(x)
    }
    
    fun a(p:A.()->Unit):A {
    
        val a = A()
        a.p()
        return a;
    }
    
    class A {
    
        val children=arrayListOf<AA>()
    
        fun aa(p:AA.()->Unit){
    
            val aa = AA();
            aa.p();
            children.add(aa);
        }
    
        override fun toString(): String {
            val builder = StringBuilder("<a>")
            children.forEach { builder.append(it.toString()) }
            builder.append("
    </a>")
            return builder.toString();
        }
    
    }
    
    class AA {
        val children=arrayListOf<String>()
    
        operator fun String.unaryPlus() {
            children.add("
    		<text>${this}</text>")
        }
    
        override fun toString(): String {
            val builder = StringBuilder("
    	<aa>")
            children.forEach { builder.append(it.toString()) }
            builder.append("
    	</aa>")
            return builder.toString();
        }
    }
    

     执行结果

    <a>
        <aa>
            <text>01.01</text>
            <text>01.02</text>
        </aa>
        <aa>
            <text>02.01</text>
            <text>02.02</text>
        </aa>
    </a>
    import java.util.*
    
    import List.Companion.list
    import Map.Companion.map
    
    
    fun main(args: Array<String>): Unit {
    
        val x = list {
            values("a", "b", "c")
            list {
                values("a", "b", "c")
                map {
                    values("name" to "Fred")
                }
            }
            map {
                values(
                        "name" to "Fred",
                        "age" to "28",
                        "fav" to list {
                            values("java", "c++", "kotlin")
                        }
                )
            }
        }
    
        val y = map {
            values("languages" to list {
                values("java", "c++", "perl")
                values("scala","kotlin","ruby")
            })
        }
    
        println(x)
        println(y)
    }
    
    
    class List : ArrayList<Any>() {
    
        companion object {
    
            fun list(p: List.() -> Unit): List {
                val l = List()
                l.p()
                return l
            }
        }
    
        fun values(vararg x: Any) {
            this.addAll(x)
        }
    
        fun list(p: List.() -> Unit) {
            val l = List()
            l.p()
            this.add(l as Any)
        }
    
        fun map(p: Map.() -> Unit) {
            val m = Map()
            m.p()
            this.add(m as Any)
        }
    
    }
    
    class Map : HashMap<String, Any>() {
    
        companion object {
    
            fun map(p: Map.() -> Unit): Map {
                val m = Map()
                m.p()
                return m
            }
        }
    
        fun values(vararg x: Pair<String, Any>) {
            this.putAll(x)
        }
    
        fun list(p: List.() -> Unit): List {
            val l = List()
            l.p()
            return l;
        }
    
        fun map(p: Map.() -> Unit): Map {
            val m = Map()
            m.p()
            return m
    
        }
    
    }
    

    执行结果

    [a, b, c, [a, b, c, {name=Fred}], {name=Fred, fav=[java, c++, kotlin], age=28}]
    {languages=[java, c++, perl, scala, kotlin, ruby]}

      

      

  • 相关阅读:
    玩转动态编译
    [源码]RandomId 生成随机字符串
    玩转动态编译:四、封装
    玩转动态编译:三、提高性能,抛弃反射
    玩转动态编译:一、初识
    封装和内置函数property classmethod staticmethod
    面向对象--命名空间和组合
    初始面向对象
    模块之序列化模块和包
    模块 time模块 sys模块 os模块
  • 原文地址:https://www.cnblogs.com/scala/p/5284408.html
Copyright © 2011-2022 走看看