2. Hello Kotlin!
fun main(args: Array<String>) {
println("Hello World!!")
}
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!!");
}
}
8. “✘Find the restrooms which are the distance
of 5 kilometers from me and public Toilet.
The restroom list is sorted and the restroom
name only.
data class Toilet(val id: Int,
val name: String,
val quantity: Int,
val type: Int,
val isPublic: Boolean,
val distance: Double)
14. DSL in Java
class Car {
private String name;
private double price;
private String model;
private int year;
private Car(String name, double price, String
model, int year) {
this.name = name;
this.price = price;
this.model = model;
this.year = year;
}
// getter and setter
// ...
}
static class Builder {
private String name;
private double price;
private String model;
private int year;
public Builder setName(String name) {
this.name = name;
return this;
}
public Builder setPrice(double price) {
this.price = price;
return this;
}
// ...
public Car build() {
return new Car(this.name, this.price, this.model,
this.year)
}
}
new Car.Builder()
.setName("Car")
.setPrice(12000)
.build();
Usage
Define
15. DSL in Kotlin
class Car(val name: String?, val price: Double, val model: String?, val year:
Int) {
private constructor(builder: Builder):
this(builder.name, builder.price, builder.model, builder.year)
companion object {
inline fun build(block: Builder.() -> Unit) =
Builder().apply(block).build()
}
internal class Builder() {
var name: String? = null
var price: Double = .0
var model: String? = null
var year: Int = 0
fun build() = Car(this)
}
}
val car = Car.build {
name = "CAR"
price = 1000.0
model = "BMW"
}
Usage
Define