-
Notifications
You must be signed in to change notification settings - Fork 0
/
Classes.kt
47 lines (42 loc) · 1.34 KB
/
Classes.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Created by Anish Arya on 27-Jun-17.
*/
package Mark1
fun main(args: Array<String>){
val bowser = Animal("Bowser",20.0,13.5)
bowser.getInfo()
val BlackBeard = Dog("BlackBeard",22.0,14.5,"Sherlock")
BlackBeard.getInfo()
val tweety = Bird("Tweety",true)
tweety.fly(10.0)
}
//Class
open class Animal(val name: String, var height: Double, var weight: Double) {
init {
val regex = Regex(".*\\d+.*")
require(!name.matches(regex)) { "Animal name can't contain numbers" }
require(height > 0) { "Height must be greater than 0" }
require(weight > 0) { "Weight must be greater than 0" }
}
open fun getInfo(): Unit {
println("$name is $height tall and weighs $weight")
}
}
//Inheritance
class Dog(name: String,height: Double,weight: Double, var owner: String): Animal(name,height,weight){
override fun getInfo(): Unit {
println("$name is $height tall and weighs $weight and owned by $owner")
}
}
//Interface
interface Flyable{
var flies:Boolean
fun fly(distMiles:Double): Unit
}
class Bird constructor(val name: String, override var flies: Boolean = true) : Flyable{
override fun fly(distMiles: Double): Unit{
if(flies){
println("$name flies $distMiles")
}
}
}