This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Square { | |
| type = "Square" as const | |
| constructor(public side: number) {} | |
| } | |
| class Circle { | |
| type = "Circle" as const | |
| constructor(public radius: number) {} | |
| } | |
| class Rectangle { | |
| type = "Rectangle" as const |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Shape { | |
| constructor() { | |
| this.type = this.constructor.name | |
| } | |
| } | |
| class Square extends Shape { | |
| constructor(side) { | |
| super() | |
| this.side = side | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface Shape { | |
| fun area(): Double | |
| fun perimeter(): Double | |
| } | |
| class Square(val side: Double) : Shape { | |
| override fun area() = side * side | |
| override fun perimeter() = 4 * side | |
| } | |
| class Circle(val radius: Double) : Shape { | |
| override fun area() = radius * radius * Math.PI |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sealed class Shape | |
| class Square(val side: Double) : Shape() | |
| class Circle(val radius: Double) : Shape() | |
| class Rectangle(val width: Double, val height: Double) : Shape() | |
| fun main() { | |
| val shapes = listOf( | |
| Square(5.0), | |
| Circle(4.0), | |
| Rectangle(6.0, 7.0) |