Kotlin Extension
$count++; if($count == 1) { include "../mobilemenu.php"; } if ($count == 2) { include "../sharemediasubfolder.php"; } ?>
In Kotlin we can extend a class with new functionality. We can do that without having to sub-class it or change the class definition.
This feature is called Extension. Let us see an example to understand better.
Say we have a class C that has a function printHi, which as the name suggests prints a Hello message.
class C
{
fun printHi()
{
println("Hello There")
}
}
Now, I would like to add an extra member function to the class C, say printBye, which prints a good bye message. Instead of creating a subclass or modifying C itself, I can create an extension as shown below.
fun C.printBye()
{
println("Bye There")
}
As you can see in the above example we extended class C with new method printBye.Calling the methods
We can now call two methods in class C. Both printHi and the extended printBye method and they are both part of class C.
fun main(args: Array<String>)
{
var x = C()
x.printHi()
x.printBye()
}
Output:
Hello There
Bye There