Java/2012.04 강좌
11일차 Abstract (간단한 도형 계산)
Bohemian life
2012. 4. 18. 11:52
package com.book; public class Exam1 { public static void main(String[] args) { // 원 네모 세모 객체를 만들고 넓이를 구합니다. Circle circle = new Circle(5); Triangle triangle = new Triangle(3,5); Rect rect = new Rect(8,9); circle.area(); triangle.area(); rect.area(); } }
package com.book; public class Circle extends M{ int radian;//반지름 필드 public Circle(int radian){ this.radian=radian; } @Override public void area() {//면적,넓이 System.out.println("원의 넓이 : "+ Math.PI*radian*radian); } }
package com.book; public class Rect extends M{ int horizontal; int vertical; public Rect(int hor, int ver){ this.horizontal=horizontal; this.vertical=vertical; } @Override public void area() {//면적,넓이 System.out.println("사각형의 넓이 : "+ horizontal*vertical); } }
package com.book; import com.korea.Action; //자바는 단일 상속, 단 1개만 상속 받는다. //이것의 해결 하는 법 :인터 페이스 public class Triangle extends M{ int height; int bottom; public Triangle(int height, int bottom){ this.height=height; this.bottom=bottom; } @Override public void area() {//면적,넓이 System.out.println("삼각형의 넓이 : "+ height*bottom); } }
package com.book; public abstract class M { public abstract void area(); void hi(){}; }