Break and Continue statement
The break statement is used to terminate the nearest enclosing loop the break keyword terminates the controller flow if some condition has failed . before going to examples here is one thing to learn that is label any expression in kotlin may be marked with a label labels have an followed by @ signs for example foo@,myfunc@ etc are valid labels to label an expression we just put a label in front of it here is an example
loop@ for (i in 1..5){
for (j in 1..3){
if (i ==2) break@loop
}
}
for (j in 1..3){
if (i ==2) break@loop
}
}
A break qualified with the label jumps to the execution point right after the loop marked with that label
Continue statement is the opposite of the break statement if continue is put here it will proceed to the next iteration of that loop
Continue statement is the opposite of the break statement if continue is put here it will proceed to the next iteration of that loop
Classes and Inheritance
A class is a blueprint or prototype from were which objects are created it represents the set of properties or methods that are common to all objects of one type when we are creating a class it's describing how the object should be made an object may have different instance variable values the objects of the class may have same methods but its behavior may be different simply an object have states and behaviors and an object is the instance of a class
now we should go somewhat briefly to explain the class with samples so the classes in kotlin are declared using the class keyword
now we should go somewhat briefly to explain the class with samples so the classes in kotlin are declared using the class keyword
class Myclass {
}
}
The class name here is Myclass the class declaration consists of the class name the class header (specifying its type parameters the primary constructors etc(type parameter and primary constructor are explained below)) and the class body surrounded by curly braces both the header and body are optional if the class has no body curly braces can be omitted like
class Empty
Now we can see what is a type parameters and the primary constructors
Type parameters
The type parameters are placeholder for a specific type which we will see in the following lessons
Primary Constructors
The constructors are the block of code which are similar to method (methods are like functions inside the class ) thats called when an instance of an object is created and like methods constructor doesn't have return types and the name of constructor must be same as the class name
A class in kotlin can have a primary constructor and one or more secondary constructor it comes after the class name(optional type parameter)
A class in kotlin can have a primary constructor and one or more secondary constructor it comes after the class name(optional type parameter)
class Dog constructor(Name : String){
}
}
if the primary constructor doesn't have any annotations or visibility modifiers we can omit the constructor keyword the primary constructor cannot contain any code initilization code is placed in the initilizer block with init keyword here is an example
class myclass(name: String){
val firstname = "firstname = $name".also (::println)
init{
println ("first initializer block that prints ${name}")
}
val namelength = "namelength = ${name.length}".also(::println)
init{
println ("second initializer block that prints ${name.length}")
}
}
fun main(args: Array<String>) {
InitOrderDemo("nadeem")
}
//the out put will be as following
firstname = nadeem
first initializer block that prints nadeem
namelength = 6
second initializer block that prints 6
As given above the first constructor second constructor are made and outputs as first initializer and second initializer you can clearly learn from the above code parameters of the primary constructor can be used in the initializer block they can also be used in the initializers declared in the class body
if the constructor has annotations or visibility modifiers the constructor keyword is required and the modifiers go before it
if the constructor has annotations or visibility modifiers the constructor keyword is required and the modifiers go before it
class Customer public @Inject constructor(name: String){
}
}
The constructor given above have the public which is a visibility modifier (which defines the visibility of this class) and @Inject is the annotation here
No comments:
Post a Comment