Extension Function in Kotlin

 package com.example.my_kotlin_practice


fun main(args: Array<String>) {
var circle = Circle1()
println("Area of circle is : ${circle.area(4)}")
println("Perimeter of circle is : ${circle.perimeter(4)}")
var str = "Madam"
println("Is given '$str' string palindrome ? : ${str.isPalindrome()}")
var str1 = "Nurses run"
println("Is given '$str1' string palindrome ? : ${str1.isPalindrome()}")
}
class Circle1{
fun area(r: Int): Double{
return 3.14*r*r
}
}
fun Circle1.perimeter(r: Int): Double{
return 2*3.14*r
}
fun String.isPalindrome(): Boolean{
return this.lowercase() == this.lowercase().reversed()
}

OUTPUT :

Area of circle is : 50.24 Perimeter of circle is : 25.12 Is given 'Madam' string palindrome ? : true Is given 'Nurses run' string palindrome ? : false Process finished with exit code 0

Comments

Popular posts from this blog

Maps (Collections) in kotlin

Functions in Kotlin

Arrays in Kotlin