interface Base{
}
class Electron : Base{
}
class Particle : Base{
}
open class Element(val name: String){
open fun Particle.react(name: String): Unit{
println("$name is reacting with a particle")
}
open fun Electron.react(name: String): Unit{
println("$name is reacting with an electron to make an isotope")
}
fun react(particle: Particle): Unit{
particle.react(name)
}
}
class NobleGas(name: String): Element(name){
override fun Particle.react(name: String):Unit{
println("$name is noble, it does not react with particles")
}
override fun Electron.react(name: String): Unit{
println("$name is noble it does not react with electrons")
}
fun react(particle: Electron): Unit{
particle.react(name)
}
}
fun main(args: Array<String>){
val selenium = Element("Selenium")
selenium.react(Particle())
}
fun Int.Companion.random():Int{
val random = Random()
return random.nextInt()
}
fun testCompainExtensionFun(){
val int = Int.random()
}
fun roots3(k: Int):Pair<Double, Double>{
require(k >= 0)
val root = Math.sqrt(k.toDouble())
return Pair(root, -root)
}
fun testMulitReturnFun(){
val (pos,neg) = roots3(16)
}
fun testInfixFun1(){
val pair = "LonDon" to "UK"
}
class Account{
var balance = 0.0
fun add(amount: Double): Unit{
this.balance = balance +amount
}
}
fun testAccount(){
val account = Account()
account.add(100.00)
}
class InfixAccount{
var balance = 0.0
infix fun add(amount: Double): Unit{
this.balance = balance +amount
}
}
fun testInfixAccount(){
val account2 = InfixAccount()
account2 add 110.00
val pair1 = Pair("london","pairs")
val pair2 = "london" to "paris"
}