diff --git a/src/main/kotlin/AbstractKlass/abstractclass.kt b/src/main/kotlin/AbstractKlass/abstractclass.kt new file mode 100644 index 0000000..e523508 --- /dev/null +++ b/src/main/kotlin/AbstractKlass/abstractclass.kt @@ -0,0 +1,33 @@ +package AbstractKlass + +// Базовый абстрактный класс Shape +abstract class Shape { + // Абстрактный метод для вычисления площади фигуры + abstract fun calculateArea(): Double +} + +// Подкласс Circle, представляющий круг +class Circle(val radius: Double) : Shape() { + // Переопределите метод calculateArea(), чтобы он возвращал площадь круга + override fun calculateArea(): Double { + return Math.PI * radius * radius + } +} + +// Подкласс Rectangle, представляющий прямоугольник +class Rectangle(val width: Double, val height: Double) : Shape() { + // Переопределите метод calculateArea(), чтобы он возвращал площадь прямоугольника + override fun calculateArea(): Double { + return width * height + } +} + +fun main() { +// Создайте экземпляр круга и прямоугольника + val circle = Circle(5.0) + val rectangle = Rectangle(4.0, 6.0) + +// Выведите площади фигур + println("Площадь круга: ${circle.calculateArea()}") + println("Площадь прямоугольника: ${rectangle.calculateArea()}") +} \ No newline at end of file diff --git a/src/main/kotlin/Demo1.kt b/src/main/kotlin/Demo1.kt new file mode 100644 index 0000000..9b94f1f --- /dev/null +++ b/src/main/kotlin/Demo1.kt @@ -0,0 +1,2 @@ +class Demo1 { +} \ No newline at end of file diff --git a/src/main/kotlin/Nasledovanie/avto.kt b/src/main/kotlin/Nasledovanie/avto.kt new file mode 100644 index 0000000..ef69bc1 --- /dev/null +++ b/src/main/kotlin/Nasledovanie/avto.kt @@ -0,0 +1,4 @@ +package Nasledovanie + +class avto { +} \ No newline at end of file diff --git a/src/main/kotlin/Personal/Department.kt b/src/main/kotlin/Personal/Department.kt new file mode 100644 index 0000000..da20bec --- /dev/null +++ b/src/main/kotlin/Personal/Department.kt @@ -0,0 +1,38 @@ +package Personal + +class Department(private var _name: String, private var _employees: List) { + + var name: String + get() = _name + set(value) { + _name = value + } + + var employees: List + get() = _employees + set(value) { + if (value.isNotEmpty()) { + _employees = value + } else { + println("Список сотрудников не может быть пустым.") + } + } +} + +fun main() { + val department = Department("IT", listOf("Алиса", "Дима")) + + println(department.name) // Output: IT + println(department.employees) // Output: [Alice, Bob] + + // Попытка установить пустой список сотрудников + department.employees = listOf() + // Output: Список сотрудников не может быть пустым. + + // Список сотрудников остается прежним + println(department.employees) // Output: [Alice, Bob] + + // Устанавливаем новый непустой список сотрудников + department.employees = listOf("Андрей", "Мишка") + println(department.employees) // Output: [Charlie, Dave] +} \ No newline at end of file diff --git a/src/main/kotlin/nasledovanie/avto.kt b/src/main/kotlin/nasledovanie/avto.kt new file mode 100644 index 0000000..e2cc1b3 --- /dev/null +++ b/src/main/kotlin/nasledovanie/avto.kt @@ -0,0 +1,67 @@ +package Nasledovanie + +class avto { +} + +abstract class Vehicle(val speed: Int, val color: String) { + abstract fun makeSound() + + open fun displayInfo() { + println("Транспортное средство: ${this::class.simpleName}") + println("Скорость: $speed") + println("Цвет: $color") + } +} + +class Bike(speed: Int, color: String, val countOfWheels: Int) : Vehicle(speed, color) { + override fun makeSound() { + println("Звонок велосипеда") + } + + override fun displayInfo() { + super.displayInfo() + println("Количество колес: $countOfWheels") + } +} + +class ElectricCar(speed: Int, color: String, val batteryCapacity: Int) : Vehicle(speed, color) { + override fun makeSound() { + println("Звук гудка электромобиля") + } + + override fun displayInfo() { + super.displayInfo() + println("Емкость аккумулятора: $batteryCapacity") + } +} + +interface FuelEfficient { + fun fuelEfficiency() +} + +class Car(speed: Int, color: String) : Vehicle(speed, color), FuelEfficient { + override fun makeSound() { + println("Звук гудка автомобиля") + } + + override fun fuelEfficiency() { + println("Автомобиль - эффективное использование топлива") + } +} + +fun main() { + val vehicles = listOf( + Car(100, "красный"), + Bike(30, "синий", 2), + ElectricCar(80, "зеленый", 100) + ) + + for (vehicle in vehicles) { + vehicle.displayInfo() + vehicle.makeSound() + if (vehicle is FuelEfficient) { + vehicle.fuelEfficiency() + } + println() + } +} \ No newline at end of file diff --git a/src/main/kotlin/one/Bike.kt b/src/main/kotlin/one/Bike.kt new file mode 100644 index 0000000..f8a509c --- /dev/null +++ b/src/main/kotlin/one/Bike.kt @@ -0,0 +1,6 @@ +package one + +class Bike : Vehicle() { + + +} \ No newline at end of file diff --git a/src/main/kotlin/polemorfizm/DiagramClass.kt b/src/main/kotlin/polemorfizm/DiagramClass.kt new file mode 100644 index 0000000..add76a0 --- /dev/null +++ b/src/main/kotlin/polemorfizm/DiagramClass.kt @@ -0,0 +1,65 @@ +package polemorfizm + +class DiagramClass { +} + +//Диаграмма 1 +open class Click { } +class Clack : Click() { } + + +//Диаграмма 2 +open class Top { } +class Tip : Top() { } + + +//Диаграмма 3 +abstract class Alpha { } +class Omega : Alpha() { } + + +//Диаграмма 4 +open class Foo { } +open class Bar : Foo() { } +class Baz : Bar() { } + + +//Диаграмма 5 +interface Fi { } +open class Fee : Fi { } +class Fo : Fee() { } +class Fum : Fo() { } + +//2-е Задание +interface Flyable{ + val x:String + fun fly(){ + println("$x is flying") + } +} + +class Bird : Flyable{ + override val x="Bird" +} + +class Plane : Flyable{ + override val x="Plane" +} +class Superhero : Flyable{ + override val x="Superhero" +} + +fun main(args: Array){ + val f= arrayOf(Bird(),Plane(),Superhero()) + var x=0 + while (x in 0..2){ + when(f[x]){ + is Bird->{ + x++ + f[x].fly() + } + is Plane,is Superhero->f[x].fly() + } + x++ + } +} \ No newline at end of file