Hi ,
Here is another post related to Kotlin basics for beginners , in this post I will discuss the Kotlin Extension Function ,let's get started :
So sometimes , we feel that one important functionality is missing from a class and
we want to add that function :
we need to extend that class, and then add that function and then use that extended class.
But in Kotlin , helps definitely on this by providing Extension Functions
Extension Functions are:
Here we can see the there was no function "findTheGreaterNum()" in Int class , but we can make this like extension function of Int class .
Here this will annotate for i , bcoz the function is class for i object.
Same like this we can make the extension functions for our own classes as well.
That's all for extension functions , will post more for kotlin , till then Happy Coding :)
Here is another post related to Kotlin basics for beginners , in this post I will discuss the Kotlin Extension Function ,let's get started :
So sometimes , we feel that one important functionality is missing from a class and
we want to add that function :
we need to extend that class, and then add that function and then use that extended class.
But in Kotlin , helps definitely on this by providing Extension Functions
Extension Functions are:
- - can "add" new functions to a class without declaring it
- - the new functions added behaves like "static" in java
So let's take a look in code first , then we will discuss briefly on this :
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) var i : Int = 45 var j : Int = 91 println("In I: $i & in J: $j , greater number is : "+ i.findTheGreaterNum(j)) } /** * Extension Function */ fun Int.findTheGreaterNum(j :Int) : Int { if(this > j) return this else return j }
Here we can see the there was no function "findTheGreaterNum()" in Int class , but we can make this like extension function of Int class .
Here this will annotate for i , bcoz the function is class for i object.
Same like this we can make the extension functions for our own classes as well.
That's all for extension functions , will post more for kotlin , till then Happy Coding :)
No comments:
Post a Comment