2012년 12월 30일 일요일

[Ch11Ex02] Up-cating 7 method overwiritng

Result :


This is 3.141592653589793
도형의 면적은 78.53981633974483
도형의 면적은 100.0
도형의 면적은 50.0



Sample Code

File : Ch11Ex02_02.java


import kame.ch11.ex02.Circle;
import kame.ch11.ex02.Rectangle;
import kame.ch11.ex02.Shape;
import kame.ch11.ex02.Triangle;

public class Ch11Ex02_02 {
   public static void main(String[] args) {

   Shape ref=null;
   ref=new Circle( );
   System.out.println("도형의 면적은 " + ref.area( ));
   ref=new Rectangle( );
   System.out.println("도형의 면적은 " + ref.area( ));
   ref=new Triangle( );
   System.out.println("도형의 면적은 " + ref.area( ));
  }
}

File : Shape


package kame.ch11.ex02;

public class Shape {

  public double res=0;
 
  public double area( ){
    return res;
  }
}



File : Circle


package kame.ch11.ex02;

public class Circle extends Shape {
public int r=5;

 public double area( ){
 res=r*r*Math.PI;
 System.out.println(" This is "+ Math.PI );
 return res;
 }
}




File : Rectangle


package kame.ch11.ex02;

public class Rectangle extends Shape {
public int w=10;
public int h=10;
public double area( ){
 res=w*h;
 return res;
 }
}




File : Triangle


package kame.ch11.ex02;

public class Triangle extends Shape {
public int w=10;
public int h=10;
 public double area( ){
 res=w*h*0.5;
 return res;
 }
}


댓글 없음:

댓글 쓰기