Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Nadav class
- ===============
- package com.example.teacher.mykotlin2
- /**
- * All classes are final by defualt
- * to allow ingeritance use open 'keyword'
- * to override method of super class, user 'override' keyword
- * use 'override' for overriding properties too
- * to call super implementation , use 'super' keyword
- * tp declare class/method/property abstract use 'abstract' key word
- */
- //classes can be empty
- class Ayelet
- /** Inheritance class **/
- open class Base(value: String)
- class Derived : Base("")
- /** Overriding class **/
- open class A {
- open val a: Int = 10
- open var eyal: String = "Hello"
- open fun method(x: Any): Unit {
- }
- }
- class B : A() {
- override val a: Int = 100
- override var eyal: String = "Goodbye"
- override fun method(x: Any) {
- //calling super implementation
- super.method(x)
- }
- }
- /** abstract class */
- abstract class abstro(val a: String) {
- //abstract method
- abstract fun killAmjad()
- abstract fun killHusam(): Boolean
- abstract fun kaboomBMW(explosiveKG: Int): Boolean
- //concrete method
- fun concretA(): String = a
- }
- /** Interfaces **/
- interface MyInterface {
- val number: Int
- var deadpool2: String
- fun husamTheKing()
- fun justKidding()
- fun heNotSoKing()
- {
- this.javaClass.name
- println("Default implementation - $deadpool2")
- }
- }
- //Enum
- enum class Types{
- a,b,c;
- fun getType():String{
- return this.name
- }
- }
- /**
- * Kotlin object keyword
- * used to create singleton or static like java
- * also used to instantiate anonymous class
- */
- object MyObject{
- val groot:Int=1
- var bubu : Empty? = Empty()
- }
- kill tomer class
- ======================
- package com.example.teacher.mykotlin2
- /**
- * Classes are the same as Java
- * Every Class can hold Kotlin.main constructor, which is written after class name
- */
- class KillTomer constructor(val param1: Boolean, param2: Any) {
- /**
- * constructors
- * we can add secondary constructor
- */
- constructor() : this(true, { map: Map<Int, Boolean> -> map.values.size }) {
- println("Secondery constructor")
- }
- //init block for Kotlin.main constructor
- init {
- println("constructor $param1")
- }
- /*
- * access modifiers
- * - public : default
- * - protected : private + subclasses
- * - internal: accessible in module, all over the project
- * - private: accessible only in call, not including inner classes
- */
- /*
- * properties
- * function that looks like a field, accessing value calls getter.
- * changing values call setter
- *
- * val hold getter
- * var hold getter+setter
- */
- val readOnlyProp: String
- get() {
- if (param1)
- return "Cool"
- else
- return "Not Cool"
- }
- var Property:Int =0
- set (value){
- //field to access value of Property
- field=value
- }
- get (){
- return field*4
- }
- }
- MainActivity(collection)
- ==========================
- package com.example.teacher.mykotlin2
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- import java.util.ArrayList
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- myCollection()
- }
- private fun myCollection()
- {
- /*
- * immutable collection
- */
- val arr = arrayOf("1","2")
- arr[1]="0" //call list.get(0)
- val set= setOf(1,2,3,2,1)
- println(set.size)
- val map = mapOf(Pair(1,"one"), Pair(2, "two"))
- println("${map.size} ${map[0]}")
- /*
- * mutable collections
- */
- val mArr = ArrayList<String>()
- mArr.add("Chusam Uskut")
- mArr.removeAt(0)
- val mset = mutableSetOf<Boolean>()
- mset.add(true)
- mset.add(false)
- mset.add(false)
- val mList = mutableListOf<Int>()
- mList.add(100)
- mList.add(0,19)
- //100->19,100
- val mMap = HashMap<String,Int>()
- mMap.put("one",1)
- mMap.put("two",2)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement