/
GermanezZ
/
JavaExercises
Обзор
Документация
Войти
/
GermanezZ
/
JavaExercises
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Clases/BoxDemo.java
117 строк
2 KB
germa
Новое упражнение
28 янв 2025, 16:53
28 янв 2025, 16:53
4a2e79b
Код
Авторство
О чём код?
package Clases; public class BoxDemo { public static void main(String[] args) { Box MyBox = new Box(); MyBox.setDim(12,3,5); MyBox.volume(); Box myBox1 = new Box(); myBox1.setDim(3,5,53); myBox1.volume(); BoxWeight myBox2 = new BoxWeight(1,2,4,6); myBox2.volume(); var myBox3 = new BoxWeight(10,20,15,34.3); myBox3.volume(); var myBox4 = new Box(); myBox4.volume(); System.out.println(); System.out.println("//////////////////////////////////////////////////////////////////"); System.out.println(); BoxWeight weightbox = new BoxWeight(3,5,7,8.37); Box plainbox = new Box(); weightbox.volume(); System.out.println("Вес коробки: " + weightbox.weight); System.out.println(); // В этом случае суперклассу plainbox будут доступны только члены, которые определены в суперклассе plainbox = weightbox; plainbox.volume(); // Будет ошибка (метода нет в суперклассе) //plainbox.weight; } } class Box{ private double width; private double height; private double depth; Box(Box ob){ width = ob.width; height = ob.height; depth = ob.depth; } Box(double width, double height, double depth){ this.width = width; this.height = height; this.depth = depth; } Box(){ width = -1; height = -1; depth = -1; } Box(double length){ width = height = depth = length; } void setDim(int width, int height, int depth) { this.width = width; this.height = height; this.depth = depth; } public void volume() { System.out.println("Объём коробки равен " + (width * height * depth)); } @Override public String toString() { return "Box width=" + width + ", height=" + height + ", depth=" + depth; } } class BoxWeight extends Box{ double weight; BoxWeight(double w, double h, double d, double m){ super(w,h,d); weight = m; } } class ColorBox extends Box{ int color; public ColorBox(double width, double height, double depth, int color) { super(width, height, depth); this.color = color; } }