美文网首页
笔记:kotlin 学习第六天

笔记:kotlin 学习第六天

作者: s酸菜 | 来源:发表于2018-08-03 13:41 被阅读0次
    1. StandClass

      /**
       * @ 文件名:   StandClass
       * @ 创建者:   ty
       * @ 时间:    2018/8/2 09:14
       * @ 描述:    实例化用户的姓名年龄地址信息
       */
      fun main(args: Array<String>) {
      
          val person = Person("林青霞", 29, Address("深圳市", "宝安区", 10086))
          println(person.address.city + person.address.street + person.address.number)
      }
      
      class Address(var city: String, var street: String, var number: Int)
      
      data class Person(var name: String, var age: Int, var address: Address)
      
    1. DSLClass

      /**
       * @ 文件名:   `02.DSLClass`
       * @ 创建者:   ty
       * @ 时间:    2018/8/2 09:15
       * @ 描述:    完成以下效果
      
       * 第一步:实现person函数,参数以及返回值,Person,Address初始化不能有参数。
       * 第二步:实现字段属性可以添加person对象,接收者为Person对象的函数
       * 第三步:执行block函数
      
       *  person{
       *      name = "徐熊丽"
       *      age = 20
       *      address{
       *          city = "深圳市"
       *          street = "宝安街道"
       *          number = 114
       *      }
       *  }
       */
      
      
      fun main(args: Array<String>) {
          val person = person {
              name = "林青霞"
              age = 29
              address {
                  city = "深圳市"
                  street = "新安街道"
                  number = 10000
              }
          }
      
          println(person.name + person.age)
      
      }
      
      fun address(block: Address.() -> Unit): Address {
          /*val address = Address()
          block(address)
          return address*/
          return Address().apply(block)
      }
      
      fun person(block: Person.() -> Unit): Person {
      
          /*val person = Person()
      
          block(person)
      
          return person*/
      
          return Person().apply(block)
      }
      
      class Address(var city: String? = null, var street: String? = null, var number: Int? = null)
      
      data class Person(var name: String? = null, var age: Int? = null, var address: Address? = null)
      
    1. DSLHtml

      /**
       * @ 文件名:   `03.DSLHtml`
       * @ 创建者:   ty
       * @ 时间:    2018/8/2 09:16
       * @ 描述:    完成如下效果
       *
       *  html{
       *       head{
       *           title{
       *
       *           }
       *       }
       *       body{
       *           div{
       *
       *           }
       *       }
       *   }
       */
      
      fun main(args: Array<String>) {
          val html = html {
              head {
                  title {
      
                  }
              }
              body {
                  div {
      
                  }
              }
          }
      
          println(html)
      
      }
      
      fun Body.div(block: () -> Unit) {
          val div = Div()
          setTag(div)
      }
      
      fun Head.title(block: () -> Unit) {
          val title = Title()
          setTag(title)
      }
      
      fun Html.body(block: Body.() -> Unit) {
          val body = Body()
          body.block()
          setTag(body)
      }
      
      fun Html.head(block: Head.() -> Unit) {
          val head = Head()
          head.block()
          setTag(head)
      }
      
      fun html(block: Html.() -> Unit): Html {
          val html = Html()
          html.block()
          return html
      }
      
      open class Tag(var name: String) {
          private val list = ArrayList<Tag>()
      
          fun setTag(tag: Tag) {
              list.add(tag)
          }
      
          override fun toString(): String {
              val sb = StringBuilder()
              sb.append("<$name>")
              list.forEach {
                  sb.append(it.toString())
              }
              sb.append("</$name>")
              return sb.toString()
          }
      }
      
      
      class Html : Tag("Html")
      
      class Head : Tag("Head")
      
      class Body : Tag("Body")
      
      class Title : Tag("Title")
      
      class Div : Tag("Div")
      
    1. DSLHtml_缩小作用域

      /**
       * @ 文件名:   `04.DSLHtml_缩小作用域`
       * @ 创建者:   ty
       * @ 时间:    2018/8/2 09:20
       * @ 描述:    
       *       @DslMarker
       *       annotation class TAG
       */
      fun main(args: Array<String>) {
          val html = html {
              head {
                  title {
      
                  }
              }
              body {
                  div {
      
                  }
              }
          }
      
          println(html)
      
      }
      
      fun Body.div(block: () -> Unit) {
          val div = Div()
          setTag(div)
      }
      
      fun Head.title(block: () -> Unit) {
          val title = Title()
          setTag(title)
      }
      
      fun Html.body(block: Body.() -> Unit) {
          val body = Body()
          body.block()
          setTag(body)
      }
      
      fun Html.head(block: Head.() -> Unit) {
          val head = Head()
          head.block()
          setTag(head)
      }
      
      fun html(block: Html.() -> Unit): Html {
          val html = Html()
          html.block()
          return html
      }
      
      @TAG
      open class Tag(var name: String) {
          private val list = ArrayList<Tag>()
      
          fun setTag(tag: Tag) {
              list.add(tag)
          }
      
          override fun toString(): String {
              val sb = StringBuilder()
              sb.append("<$name>")
              list.forEach {
                  sb.append(it.toString())
              }
              sb.append("</$name>")
              return sb.toString()
          }
      }
      
      
      class Html : Tag("Html")
      
      class Head : Tag("Head")
      
      class Body : Tag("Body")
      
      class Title : Tag("Title")
      
      class Div : Tag("Div")
      
      @DslMarker
      annotation class TAG
      
    1. DSLClass_构建器设计模式

      /**
       * @ 文件名:   `05.DSLClass_构建器`
       * @ 创建者:   ty
       * @ 时间:    2018/8/2 09:18
       * @ 描述:    builder.build()
       * 字段可以赋值多次  需要只能赋值一次
       * 创建Person时不能传递字段  就需要默认值null  后面不能修改了
       * 创建Person时必须要传递字段
       * Person可以等到所有的字段都有了之后再创建
       *
       * 1,创建person  address 数据类,构造参数可空类型
       * 2,创建personBuilder addressBuilder构建类,有一个build函数返回person address对象。
       * 3,创建person address 高阶函数,
       * 4,由于一个人可能有多个地址,需要用一个集合字段装起来多个地址,定义一个MyList继承ArrayList
       * 5,同时person中有一个list字段addresses
       * 6,有个addresses的高阶方法,收集所有的地址
       *
       *  完成如下效果
       *  val person = person {
       *      name = "林志颖"
       *      age = 19
       *      addresses {
       *          address {
       *              city = "深圳市"
       *              street = "新安街道"
       *              number = 122
       *          }
       *          address {
       *              city = "重庆市"
       *              street = "照母山"
       *              number = 122
       *          }
       *      }
       *  }
       *
       *  Person(name=林志颖, age=19, addresses=[Address(city=深圳市, street=新安街道, number=122), Address(city=重庆市, street=照母山, number=122)])
       *
       */
      
      
      fun main(args: Array<String>) {
      
          val person = person {
              name = "林志颖"
              age = 19
              addresses {
                  address {
                      city = "深圳市"
                      street = "新安街道"
                      number = 122
                  }
                  address {
                      city = "重庆市"
                      street = "照母山"
                      number = 122
                  }
              }
          }
      
          println(person)
      
      }
      
      
      fun MyList.address(block: AddressBuilder.() -> Unit) {
      
          val address = AddressBuilder()
          address.block()
          add(address.build())
      }
      
      fun PersonBuilder.addresses(block: MyList.() -> Unit) {
          val myList = MyList()
          myList.block()
          this.addresses = myList
      
      }
      
      class PersonBuilder {
          var name: String? = null
          var age: Int? = null
          var addresses: MyList? = null
          fun build(): Person {
              return Person(name, age, addresses)
          }
      }
      
      @MYCLASS
      class AddressBuilder {
          var city: String? = null
          var street: String? = null
          var number: Int? = null
          fun build(): Address {
              return Address(city, street, number)
          }
      }
      
      
      @MYCLASS
      fun person(block: PersonBuilder.() -> Unit): Person {
          val person = PersonBuilder()
          person.block()
          return person.build()
      }
      
      data class Address(val city: String?, val street: String?, val number: Int?)
      
      data class Person(val name: String?, val age: Int?, val addresses: MyList?)
      
      @MYCLASS
      class MyList : ArrayList<Address>()
      
      @DslMarker
      annotation class MYCLASS
      

    相关文章

      网友评论

          本文标题:笔记:kotlin 学习第六天

          本文链接:https://www.haomeiwen.com/subject/axvyvftx.html