BIG
abstract class Shape {
String type;
Shape(String type) {
this.type = type;
}
abstract double area();
abstract double length();
}
class Circle extends Shape{
int r;
Circle(int r) {
super("원");
this.r = r;
}
@Override
double area() {
return r * r * Math.PI;
}
@Override
double length() {
return 2 * r * Math.PI;
}
@Override
public String toString() {
return "Shape [type=" + type + ", r=" + r + "]";
}
}
class Rectangle extends Shape {
int width, height;
Rectangle(int width, int height) {
super("사각형");
this.width = width;
this.height = height;
}
@Override
double area() {
return width * height;
}
@Override
double length() {
return 2 * (width + height);
}
@Override
public String toString() {
return "Shape [type=" + type + ", width=" + width + ", height=" + height+"]";
}
}
public class ShapeEx {
public static void main(String[] args) {
Shape[] shapes = new Shape[2];
shapes[0] = new Circle(10);
shapes[1] = new Rectangle(5,5);
for(Shape s : shapes) {
System.out.println(s);
System.out.println("넓이:"+s.area()+" 둘레:"+s.length());
}
}
}
LIST
'!!...JAVA > !!...JAVA' 카테고리의 다른 글
[java]_예제_추상클래스 & 상속_02 (0) | 2022.05.17 |
---|---|
[java]_예제_For 문 사용법 두가지 (0) | 2022.05.17 |
[java]_예제_배열_연습문제_03 (0) | 2022.05.04 |
[java]_예제_배열_연습문제_02 (0) | 2022.05.03 |
[java]_예제_배열_연습문제_01 (0) | 2022.05.03 |