Kotlin Nested and Inner Classes Code SampleCode
Issue: BAEL-1759
This commit is contained in:
parent
5eea4fc6a6
commit
74e3e7ff95
@ -106,22 +106,22 @@
|
|||||||
<artifactId>klaxon</artifactId>
|
<artifactId>klaxon</artifactId>
|
||||||
<version>${klaxon.version}</version>
|
<version>${klaxon.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.ktor</groupId>
|
<groupId>io.ktor</groupId>
|
||||||
<artifactId>ktor-server-netty</artifactId>
|
<artifactId>ktor-server-netty</artifactId>
|
||||||
<version>${ktor.io.version}</version>
|
<version>${ktor.io.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.ktor</groupId>
|
<groupId>io.ktor</groupId>
|
||||||
<artifactId>ktor-gson</artifactId>
|
<artifactId>ktor-gson</artifactId>
|
||||||
<version>${ktor.io.version}</version>
|
<version>${ktor.io.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>ch.qos.logback</groupId>
|
<groupId>ch.qos.logback</groupId>
|
||||||
<artifactId>logback-classic</artifactId>
|
<artifactId>logback-classic</artifactId>
|
||||||
<version>1.2.1</version>
|
<version>1.2.1</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@ -166,13 +166,13 @@
|
|||||||
<target>${java.version}</target>
|
<target>${java.version}</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
<executions>
|
<executions>
|
||||||
<!-- Replacing default-compile as it is treated specially
|
<!-- Replacing default-compile as it is treated specially
|
||||||
by maven -->
|
by maven -->
|
||||||
<execution>
|
<execution>
|
||||||
<id>default-compile</id>
|
<id>default-compile</id>
|
||||||
<phase>none</phase>
|
<phase>none</phase>
|
||||||
</execution>
|
</execution>
|
||||||
<!-- Replacing default-testCompile as it is treated specially
|
<!-- Replacing default-testCompile as it is treated specially
|
||||||
by maven -->
|
by maven -->
|
||||||
<execution>
|
<execution>
|
||||||
<id>default-testCompile</id>
|
<id>default-testCompile</id>
|
||||||
@ -224,10 +224,10 @@
|
|||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<kotlin-maven-plugin.version>1.2.51</kotlin-maven-plugin.version>
|
<kotlin-maven-plugin.version>1.2.60</kotlin-maven-plugin.version>
|
||||||
<kotlin-test-junit.version>1.2.51</kotlin-test-junit.version>
|
<kotlin-test-junit.version>1.2.60</kotlin-test-junit.version>
|
||||||
<kotlin-stdlib.version>1.2.51</kotlin-stdlib.version>
|
<kotlin-stdlib.version>1.2.60</kotlin-stdlib.version>
|
||||||
<kotlin-reflect.version>1.2.51</kotlin-reflect.version>
|
<kotlin-reflect.version>1.2.60</kotlin-reflect.version>
|
||||||
<kotlinx.version>0.22.5</kotlinx.version>
|
<kotlinx.version>0.22.5</kotlinx.version>
|
||||||
<ktor.io.version>0.9.2</ktor.io.version>
|
<ktor.io.version>0.9.2</ktor.io.version>
|
||||||
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
|
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
|
||||||
@ -235,9 +235,9 @@
|
|||||||
<klaxon.version>3.0.4</klaxon.version>
|
<klaxon.version>3.0.4</klaxon.version>
|
||||||
<khttp.version>0.1.0</khttp.version>
|
<khttp.version>0.1.0</khttp.version>
|
||||||
<commons-math3.version>3.6.1</commons-math3.version>
|
<commons-math3.version>3.6.1</commons-math3.version>
|
||||||
<junit.platform.version>1.0.0</junit.platform.version>
|
<junit.platform.version>1.1.1</junit.platform.version>
|
||||||
<junit.vintage.version>5.2.0</junit.vintage.version>
|
<junit.vintage.version>5.2.0</junit.vintage.version>
|
||||||
<assertj.version>3.10.0</assertj.version>
|
<assertj.version>3.10.0</assertj.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
75
core-kotlin/src/main/kotlin/com/baeldung/nested/Computer.kt
Normal file
75
core-kotlin/src/main/kotlin/com/baeldung/nested/Computer.kt
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
package com.baeldung.nested
|
||||||
|
|
||||||
|
import org.slf4j.Logger
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
|
||||||
|
class Computer(val model: String) {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val originCountry = "China"
|
||||||
|
fun getBuiltDate(): String {
|
||||||
|
return "2018-05-23"
|
||||||
|
}
|
||||||
|
|
||||||
|
val log: Logger = LoggerFactory.getLogger(Computer::class.java)
|
||||||
|
}
|
||||||
|
|
||||||
|
//Nested class
|
||||||
|
class MotherBoard(val manufacturer: String) {
|
||||||
|
fun getInfo() = "Made by $manufacturer installed in $originCountry - ${getBuiltDate()}"
|
||||||
|
}
|
||||||
|
|
||||||
|
//Inner class
|
||||||
|
inner class HardDisk(val sizeInGb: Int) {
|
||||||
|
fun getInfo() = "Installed on ${this@Computer} with $sizeInGb GB"
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Switcher {
|
||||||
|
fun on(): String
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Protector {
|
||||||
|
fun smart()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun powerOn(): String {
|
||||||
|
//Local class
|
||||||
|
var defaultColor = "Blue"
|
||||||
|
|
||||||
|
class Led(val color: String) {
|
||||||
|
fun blink(): String {
|
||||||
|
return "blinking $color"
|
||||||
|
}
|
||||||
|
|
||||||
|
fun changeDefaultPowerOnColor() {
|
||||||
|
defaultColor = "Violet"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val powerLed = Led("Green")
|
||||||
|
log.debug("defaultColor is $defaultColor")
|
||||||
|
powerLed.changeDefaultPowerOnColor()
|
||||||
|
log.debug("defaultColor changed inside Led class to $defaultColor")
|
||||||
|
//Anonymous object
|
||||||
|
val powerSwitch = object : Switcher, Protector {
|
||||||
|
override fun on(): String {
|
||||||
|
return powerLed.blink()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun smart() {
|
||||||
|
log.debug("Smart protection is implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun changeDefaultPowerOnColor() {
|
||||||
|
defaultColor = "Yellow"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
powerSwitch.changeDefaultPowerOnColor()
|
||||||
|
log.debug("defaultColor changed inside powerSwitch anonymous object to $defaultColor")
|
||||||
|
return powerSwitch.on()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toString(): String {
|
||||||
|
return "Computer(model=$model)"
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.nested
|
||||||
|
|
||||||
|
import org.assertj.core.api.Assertions.assertThat
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
|
||||||
|
class ComputerUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenComputer_whenPowerOn_thenBlink() {
|
||||||
|
val computer = Computer("Desktop")
|
||||||
|
|
||||||
|
assertThat(computer.powerOn()).isEqualTo("blinking Green")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenMotherboard_whenGetInfo_thenGetInstalledAndBuiltDetails() {
|
||||||
|
val motherBoard = Computer.MotherBoard("MotherBoard Inc.")
|
||||||
|
|
||||||
|
assertThat(motherBoard.getInfo()).isEqualTo("Made by MotherBoard Inc. installed in China - 2018-05-23")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun givenHardDisk_whenGetInfo_thenGetComputerModelAndDiskSizeInGb() {
|
||||||
|
val hardDisk = Computer("Desktop").HardDisk(1000)
|
||||||
|
|
||||||
|
assertThat(hardDisk.getInfo()).isEqualTo("Installed on Computer(model=Desktop) with 1000 GB")
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user