Advertisement
Guest User

final exmaples of kotlin

a guest
Jan 4th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.33 KB | None | 0 0
  1. Nadav class
  2. ===============
  3. package com.example.teacher.mykotlin2
  4.  
  5. /**
  6.  * All classes are final by defualt
  7.  * to allow ingeritance use open 'keyword'
  8.  * to override method of super class, user 'override' keyword
  9.  * use 'override' for overriding properties too
  10.  * to call super implementation , use 'super' keyword
  11.  * tp declare class/method/property abstract use 'abstract' key word
  12.  */
  13.  
  14. //classes can be empty
  15. class Ayelet
  16.  
  17. /** Inheritance class **/
  18. open class Base(value: String)
  19.  
  20. class Derived : Base("")
  21.  
  22. /** Overriding class **/
  23. open class A {
  24.     open val a: Int = 10
  25.     open var eyal: String = "Hello"
  26.     open fun method(x: Any): Unit {
  27.  
  28.     }
  29. }
  30.  
  31. class B : A() {
  32.     override val a: Int = 100
  33.     override var eyal: String = "Goodbye"
  34.     override fun method(x: Any) {
  35.         //calling super implementation
  36.         super.method(x)
  37.     }
  38. }
  39.  
  40. /** abstract class */
  41. abstract class abstro(val a: String) {
  42.     //abstract method
  43.     abstract fun killAmjad()
  44.  
  45.     abstract fun killHusam(): Boolean
  46.     abstract fun kaboomBMW(explosiveKG: Int): Boolean
  47.  
  48.     //concrete method
  49.     fun concretA(): String = a
  50. }
  51.  
  52. /** Interfaces **/
  53. interface MyInterface {
  54.     val number: Int
  55.     var deadpool2: String
  56.     fun husamTheKing()
  57.     fun justKidding()
  58.     fun heNotSoKing()
  59.     {
  60.         this.javaClass.name
  61.         println("Default implementation  - $deadpool2")
  62.     }
  63. }
  64.  
  65. //Enum
  66. enum class Types{
  67.     a,b,c;
  68.     fun getType():String{
  69.         return this.name
  70.     }
  71. }
  72.  
  73. /**
  74.  * Kotlin object keyword
  75.  * used to create singleton or static like java
  76.  * also used to instantiate anonymous class
  77.  */
  78. object MyObject{
  79.     val groot:Int=1
  80.     var bubu : Empty? = Empty()
  81. }
  82.  
  83.  
  84. kill tomer class
  85. ======================
  86. package com.example.teacher.mykotlin2
  87.  
  88. /**
  89.  * Classes are the same as Java
  90.  * Every Class can hold Kotlin.main constructor, which is written after class name
  91.  */
  92. class KillTomer constructor(val param1: Boolean, param2: Any) {
  93.     /**
  94.      * constructors
  95.      * we can add secondary constructor
  96.      */
  97.     constructor() : this(true, { map: Map<Int, Boolean> -> map.values.size }) {
  98.         println("Secondery constructor")
  99.     }
  100.  
  101.     //init block for Kotlin.main constructor
  102.     init {
  103.         println("constructor $param1")
  104.     }
  105.  
  106.     /*
  107.      * access modifiers
  108.      * - public : default
  109.      * - protected : private + subclasses
  110.      * - internal: accessible in module, all over the project
  111.      * - private: accessible only in call, not including inner classes
  112.      */
  113.  
  114.     /*
  115.      * properties
  116.      * function that looks like a field, accessing value calls getter.
  117.      * changing values call setter
  118.      *
  119.      * val hold getter
  120.      * var hold getter+setter
  121.      */
  122.  
  123.     val readOnlyProp: String
  124.         get() {
  125.             if (param1)
  126.                 return "Cool"
  127.             else
  128.                 return "Not Cool"
  129.         }
  130.     var Property:Int =0
  131.         set (value){
  132.             //field to access value of Property
  133.             field=value
  134.         }
  135.         get (){
  136.             return field*4
  137.         }
  138. }
  139.  
  140.  
  141. MainActivity(collection)
  142. ==========================
  143. package com.example.teacher.mykotlin2
  144.  
  145. import android.support.v7.app.AppCompatActivity
  146. import android.os.Bundle
  147. import java.util.ArrayList
  148.  
  149. class MainActivity : AppCompatActivity() {
  150.  
  151.     override fun onCreate(savedInstanceState: Bundle?) {
  152.         super.onCreate(savedInstanceState)
  153.         setContentView(R.layout.activity_main)
  154.         myCollection()
  155.     }
  156.  
  157.     private fun myCollection()
  158.     {
  159.         /*
  160.          * immutable collection
  161.          */
  162.         val arr = arrayOf("1","2")
  163.         arr[1]="0" //call list.get(0)
  164.  
  165.         val set= setOf(1,2,3,2,1)
  166.         println(set.size)
  167.  
  168.         val map = mapOf(Pair(1,"one"), Pair(2, "two"))
  169.         println("${map.size} ${map[0]}")
  170.  
  171.  
  172.         /*
  173.          * mutable collections
  174.          */
  175.  
  176.         val mArr = ArrayList<String>()
  177.         mArr.add("Chusam Uskut")
  178.         mArr.removeAt(0)
  179.  
  180.         val mset = mutableSetOf<Boolean>()
  181.         mset.add(true)
  182.         mset.add(false)
  183.         mset.add(false)
  184.  
  185.         val mList = mutableListOf<Int>()
  186.         mList.add(100)
  187.         mList.add(0,19)
  188.         //100->19,100
  189.  
  190.         val mMap = HashMap<String,Int>()
  191.         mMap.put("one",1)
  192.         mMap.put("two",2)
  193.     }
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement