Companion Objects (2)
Create Date: May 15, 2019 at 12:32 AM         | Tag: KOTLIN         | Author Name: Sun, Charles |
Companion Objects: used for factory objects and static members
Kotlin classes do not have static members
'static' methods:
class Person { companion object { fun main(){...} } }
'factory' methods:
class Student { companion object { fun createUndergrad(name: String) : Undergraduate {...} fun createPostgrad(name: String) : Postgraduate {...} } }
Using Companion Objects:
fun main() { var charles = Student3("charles", "Sun", 1) var charles2 = Student3("charles", "Sun", 1, "some tutor") println(charles.id) Student3.createPostgrad("Charles") Student3.createUndergrad("Eagle") }
open class Student3(firstName: String, lastName: String, _id: Int, var tutor: String = "") : Person3(firstName, lastName) { val id: Int init{ id = _id } fun enrole(courseName: String) { val course = Courses.allCourses .filter{it.title == courseName} .firstOrNull() } companion object : XmlSerializer<Student3> { override fun toXml(item: Student3) { } fun createUndergrad(name: String) : Undergraduate { return Undergraduate(name) } fun createPostgrad(name: String) : Postgraduate { return Postgraduate(name) } } } class Undergraduate(firstName: String) : Student3(firstName, "", 1) { } class Postgraduate(firstName: String) : Student3(firstName, "", 1) { } interface XmlSerializer<T> { fun toXml(item: T) }
Run externally: Run -> Edit Configurations -> + (add a Kotlin class) -> Main class: rsk.Program
it can be run in java
class Program { companion object { //args: Array<String> must be included @JvmStatic fun main(args: Array<String>) { var charles = Student3("charles", "Sun", 1) var charles2 = Student3("charles", "Sun", 1, "some tutor") println(charles.id) Student3.createPostgrad("Charles") Student3.createUndergrad("Eagle") } } }New Comment
Companion Objects
Create Date: May 15, 2019 at 12:12 AM         | Tag: KOTLIN         | Author Name: Sun, Charles |
Static methods:
- Kotlin does not have static methods
- however can have 'singletons'
- use 'object' keyword
- use 'companion object' to get 'statics'
Object keyword:
- create a singleton
- actually defines a class and creates an instance
Objects:
- can have properties, methods, initializers
- cannot have constructors
class Course(val id: Int, val title: String) { } //create a singleton object Courses { var allCourses = arrayListOf<Course>() fun initialize() { allCourses.add(Course(1, "Kotlin Fundasmentals")) } }
class Student3(firstName: String, lastName: String, _id: Int, var tutor: String = "") : Person3(firstName, lastName) { val id: Int init{ id = _id } fun enrole(courseName: String) { val course = Courses.allCourses .filter{it.title == courseName} .firstOrNull() } }
Objects:
- can derive from other classes/interfaces
- can be used where any 'instance' is used
- can be declared inside another class
import java.io.File object CaseInsensitiveComparator: Comparator<File> { override fun compare(...) : Int {} }
class Person { object CaseInsensitiveComparator: Comparator<File> { override fun compare() : Int {} } }New Comment
Programming with types (4)
Create Date: May 14, 2019 at 11:54 PM         | Tag: KOTLIN         | Author Name: Sun, Charles |
Data Classes:
- provide a convenient way to override euqals, hashCode and toString
- typically immutable classes
- kotlin also generates 'copy' method
data class Meeting(val name: String, val location: String) val aMeeting = Meeting("A Meeting", "Denver") val anotherMeeting = aMeeting.copy(location = "Atlanta")
fun main() { var charles = Student4(1, "Charles") var sun = Student4(1, "Charles") //class shows reference address //data class shows "Student4(id=1, name=Charles)" println(charles) //because references are different for class //will be equal for data class. Kotlin just compares each value in the class if (charles == sun) { println("Equal") } else { println("Not Equal") } var newCharles = charles.copy(name = "Eagle") println(newCharles) } data class Student4(val id: Int, val name: String) {}
New Comment