Skip to content

Instantly share code, notes, and snippets.

@javadude
Created June 13, 2018 17:07
Show Gist options
  • Save javadude/63a15e294d206bcb2bcf4318511f8245 to your computer and use it in GitHub Desktop.
Save javadude/63a15e294d206bcb2bcf4318511f8245 to your computer and use it in GitHub Desktop.
package misc
open class AA
class BB : AA()
class CC : AA()
interface A {
fun foo1() { println("A")}
fun foo2() : String { return "A"}
fun foo3() : AA { return BB()}
}
interface B {
fun foo1() { println("B")}
fun foo2() : String { return "B"}
fun foo3() :BB { return BB() }
}
class C : A, B {
override fun foo3(): BB {
return super<B>.foo3()
}
override fun foo1() {
// super<A>.foo1()
super<B>.foo1()
}
override fun foo2(): String {
return super<A>.foo2() + super<B>.foo2()
}
companion object {
@JvmStatic
fun main(args: Array<String>) {
val c = C()
c.foo1()
someFun(c)
println(c.foo2())
}
fun someFun(a : A) {
a.foo1()
}
}
}
enum class BloodType(val displayName: String) {
// abstract fun foo()
A("A"),
B("B"),
AB("AB"),
O("Oooooooooh!")
}
sealed class State
object OnFire : State() {
fun putOutFire() {}
}
class PowerOn : State() {
fun turnPowerOff() {}
}
class PowerOff : State() {
fun turnPowerOn() {}
fun turnLightsOn() {}
}
class LightsOn : State() {
fun turnPowerOff() {}
fun turnLightsOff() {}
}
class Person1(var name : String?) {
}
class Test {
companion object {
@JvmStatic
fun main(args: Array<String>) {
val bt = BloodType.A
bt.displayName
val s : State = s1
val x = when (s) {
is PowerOn -> {
s.turnPowerOff()
42
}
is PowerOff -> {
s.turnPowerOn()
43
}
is LightsOn -> {
s.turnLightsOff()
44
}
is OnFire -> {
s.putOutFire()
45
}
}
// val s2 = s as LightsOn
val s3 =
s as? LightsOn ?: s as? PowerOn ?: s as? PowerOff
val list = listOf(0,1,2,3,4,5,6,7,8,9)
for(i in 0..9) {
println(list[i])
}
for(i in 0 until list.size) {
println(list[i])
}
for(i in 0 until list.size step 2) {
println(list[i])
}
for(i in list.size-1 downTo 0) {
println(list[i])
}
outer@ for(i in 0..10) {
for(j in 0..10) {
if (j == 2) {
break@outer
}
}
}
val st1 : String? = ""
val st2 : String? = ""
val xx = st1?.let foo@ {
st2?.let {
if (true) {
return@foo 42
}
}
10
}
val yy = st1?.let {
st2?.also {
if (true) {
return@let 42
}
}
10
}
val val1 : String? = ""
val val2 : String? = ""
val1?.let {v1 ->
val2?.let { v2 ->
// do something
} ?: throw IllegalStateException("you did not set val2")
} ?: throw IllegalStateException("you did not set val1")
val p = Person1("Scott")
// if (p.name != null) {
// println(p.name.length)
// }
p.name?.let {
println(it.length)
}
val name = p.name
if (name != null) {
println(name.length)
}
}
val s1 = PowerOn()
val s2 = PowerOff()
val s3 = PowerOn()
val s4 = LightsOn()
}
}
package misc
data class Marriage(
val person1 : Person,
val person2 : Person
)
data class Person(
var name : String,
var age : Int,
var bloodType: BloodType
) {
var address : String? = null
operator fun plus(years : Int) = copy(age = age + years)
infix fun married(person : Person) = Marriage(this, person)
}
data class Result(val x : Int, val y : Int)
fun getPoint() = Result(10, 20)
fun getTriple() = Triple(10, 20, 30)
class Test2 {
companion object {
@JvmStatic
fun main(args: Array<String>) {
val p = Person("Scott", 51, BloodType.O)
val (name, age) = p
println(name)
println(age)
val (x, y) = getPoint()
val (x1, y1, z1) = getTriple()
val p2 : Person? = p.copy(name ="Mike")
val p3 = p.copy(name ="Mike")
println(p2)
println(p2 == p3)
println(p2 === p3)
println(p2 == null)
val dim2 = TwoDimensionalIntArray(5,4, 42)
dim2[2,3] = 10
println(dim2[2,3])
println(dim2[3,3])
val p4 = p3 + 42
println(p4)
val s11 = "A"
val s12 = "B"
val s13 = s11
if (s11 < s13) {
}
val mom = Person("Margaret", 74, BloodType.O)
val dad = Person("Oliver", 78, BloodType.O)
val marriage = mom married dad
}
}
}
class TwoDimensionalIntArray(val rows:Int, val cols:Int, val defaultValue : Int) {
val values = Array(rows) { Array(cols) {defaultValue} }
operator fun get(row : Int, col : Int) = values[row][col]
operator fun set(row : Int, col : Int, value : Int) {
values[row][col] = value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment