반응형
import kotlin.math.PI
import kotlin.math.sqrt
fun main() {
val squareCabin = SquareCabin(6, 60.0)
val roundHut = RoundHut(3, 10.0)
val roundTower = RoundTower(4, 10.0)
with(squareCabin){
println("\nSquare Cabin\n==================== ")
println("Capacity : ${capacity}")
println("Material : ${buildingMaterial}")
println("Floor area : %.2f".format(floorArea()))
println("Has room? ${hasRoom()}")
}
with(roundHut){
println("\nRoundHut \n==================== ")
println("Capacity : ${capacity}")
println("Material : ${buildingMaterial}")
println("Floor area : %.2f".format(floorArea()))
println("Carpet Size : %.2f".format(calculateMaxCarpetSize()))
println("Has room? ${hasRoom()}")
}
with(roundTower){
println("\nRoundTower \n==================== ")
println("Capacity : ${capacity}")
println("Material : ${buildingMaterial}")
println("Floor area : %.2f".format(floorArea()))
println("Carpet Size : %.2f".format(calculateMaxCarpetSize()))
println("Has room? ${hasRoom()}")
}
}
/**
* 주택 전체에 대한 청사진 클래스.
* 주택의 공통 기능인
* 주택 주재료, 수용가능인원, 잔여방 체크, 방면적, 입주 확인
* 인스턴스 만들 때는 현재 거주자 수(residents) 를 입력받는다.
* 추상클래스로 만들어 상속받는 클래스들이 기능과 속성을 정의하도록 한다.
* */
abstract class Dwelling (private var residents: Int) {
abstract val buildingMaterial: String // 주재료
abstract val capacity : Int // 수용가능인원
/* 기능은 같으나 계산법이 다름 */
abstract fun floorArea(): Double // 방 면적 계산기능
/* 공통 기능 */
fun hasRoom() : Boolean { // 잔여방 체크
return residents < capacity
}
/* 입주 결과 */
fun getRoom() {
if(capacity > residents){ // 자리 있으면 입주
residents++
println("You got a room!")
} else { // 자리 없으면 거절메시지
println("Sorry, at capacity and no room left.")
}
}
}
/**
* SquareCabin은 정사각형 통나무 집으로 Dwelling 클래스를 상속받는다.
* @param residents 현재 거주자 수
* @param length 방 모서리 길이
* */
class SquareCabin (residents: Int,
val length: Double) : Dwelling(residents) {
override val buildingMaterial = "Wood"
override val capacity = 6
override fun floorArea(): Double {
return length * length
}
// 사각형 집은 바닥면적과 동일하므로 추가 계산 안해도 된다
}
/**
* 둥근 바닥 집, Dwelling 클래스를 상속받는다
* @param residents 현재 거주자 수
* @param radius 집 반지름길이
* */
open class RoundHut(residents: Int,
val radius: Double) : Dwelling(residents){
override val buildingMaterial = "Straw"
override val capacity = 4
override fun floorArea(): Double {
return PI * radius * radius
}
// 둥근집에 사용할 최대길이의 사각형의 카페트 크기 구하기
fun calculateMaxCarpetSize(): Double {
val diameter = 2 * radius // 지름
return sqrt(diameter * diameter / 2)
}
}
/** 둥근 돌집 타워
* @param residents 현재 거주자 수
* @param radius 집 반지름 길이
* @param floors 층수 : 기본값 2
* */
class RoundTower(residents: Int, radius: Double,
val floors: Int = 2): RoundHut(residents, radius){
override val buildingMaterial = "Stone"
override val capacity = 4 * floors // 각 층마다 수용인원 있으므로 층수 곱해줌
/**
* 주택 타워의 총 면적 계산, 이 클래스가 원형주택을 상속받으므로, 바닥면적 메소드도 상속받음
* 이를 super 를 통해 값 사용 가능
* @return floor area
* */
override fun floorArea(): Double {
// return PI * radius * radius * floors
return super.floorArea() * floors
}
}
- 상속
- 클래스 및 상속
- 생성자
- 상속(추상, 개방, 재정의, 비공개)
- with(공식 정의)
반응형
'Study > Android' 카테고리의 다른 글
[펌] 안드로이드 개발자라면 꼭 해야하는(꼭 알아야하는) 6가지 (0) | 2022.06.03 |
---|---|
Unit1-Pathway4-5. 단위테스트(Unit Test) 작성 (0) | 2022.05.11 |
Unit 1-Pathway4-4 : 주사위 앱에 이미지를 추가해보자 (0) | 2022.05.10 |
ImageView without contentDescription 경고 (앱의 접근성 확인) (0) | 2022.04.21 |
Android 문자열 리소스, 하드코딩된 문자열 (strings.xml) (0) | 2022.04.21 |