U E D R , A S I H C RSS

Java Study2004/클래스상속

로그램 개념 다. 는 문 다. 다른 보를 동 다른 를 명면 된다.
(superclass)를가 (subclass)를 가다. 따라 내려가는 것 된다고 다.
로부 모든 메 는다. 나 다른 로부 다. 로 가게 된다. 는 Object라는 다. 모든 로부 는다. 각 보를 로 가게 되는 것다.
때 대부 다른 가 가 몇 가 보를 가다. 를 들 Button 만들려고 다면 Button로부 기만 면 된다. 따라 Button과 다른 면 된다.
subclassing라고부른다.

만들기

를 만들기 는 기 고, 만들 다. 만들기 는 몇 가 다.
  • 다른 보를 다.
  • 를 바꾸면 를 바꾸게 다. 따라 다.
를 들 Motorcycle Car라는 를 만드는 것 . Car Motorcycle 다. 다. 또 변 를 가다. 면, Object라는 Vehicle라는 를 만들고 는 것과 는 방PersonPoweredVehicleEnginePoweredVehicle 를 만들 다. EnginePoweredVehicle 는 Motorcycle, Car, Truck 를 가 다. 그렇다면 make color라는 Vehicle 다.

를 만들다면, 된 모든 변를 가 다. 따라 모든 릿 게되고 보를 는 것다.
다. 모든 메 다. 그러나 메드가 될 때다 동로 메된다. 드를 바는 그객 본다. 그 객 다면 그 메를 발견 때까 게될 것다.
똑같드를 다고 떻게 되는가? 는 것 되게 되 다. 드를 감 드를 다. 바로 복(overriding)라고 부르는 것다.

Point

  • x,y 값 바로
  • x,y
  • x값 는 메드, 는 메
  • y값 는 메드, 는 메

  • ~cpp 
     
    import javax.swing.JOptionPane;
    
    public class Point {
    
    	private int x;
    
    	private int y;
    
    	public Point() {
    
    	}
    
    	public Point(int aX, int aY) { // 다.
    		this.x = aX;
    		y = aY;		
    	}
    
    	public int getX() {
    		return x;
    	}
    
    	public int getY() {
    		return y;
    	}
    
    	public void setX(int Ax) {
    		x = Ax;
    	}
    
    	public void setY(int Ay) {
    		y = Ay;
    	}
    
    	public String getName() {
    		return "Point";
    	}
    
    	public void Say(String str) {
    		JOptionPane.showMessageDialog(null, str);
    	}
    
    	public void Say(int temp) {
    		String str = Integer.toString(temp);
    
    		JOptionPane.showMessageDialog(null, str);
    	}
    
    	public static void main(String[] args) {
    		Point P1 = new Point(20,20);
    		Point P2 = new Point(10,10);
    		//RECTANGLE
    		Rectangle p = new Rectangle(P1,P2);
    //		System.out.println(p.getArea());
    		//circle
    		Circle c = new Circle(P1,P2);
    		System.out.println(c.getArea());
    	}	
    }
     

Shape

  • x,y, 래 x,y
  • 는 메


~cpp 
public class Shape {
	protected Point left_top;
	protected Point right_bottom;
	public String name;
	
	public Shape()
	{
		
	}
	public  Shape ( Point p1 , Point p2)
	{
		left_top =	p1;
		right_bottom = p2;
		name = "Shape";
	}
	public Shape ( int x1, int y1, int x2, int y2)
	{
		this.left_top = new Point(x1,y1);
		this.right_bottom = new Point( x2,y2 );
		name = "Shape";
	}
	
	public double getArea()
	{
		return 0;
	}
	public String getName()
	{
		return name;
	}
}

Circle

~cpp public class Circle extends Shape {

	private double radius;

	public Circle() {

	}

	public Circle(Point p1, Point p2) {
		super(p1, p2);
		name = "Circle";
	}

	public Circle(int x1, int y1, int x2, int y2) {
		super(x1, y1, x2, y2);
		name = "Circle";
	}

	public double getArea() {
		if(Math.abs(left_top.getX()-right_bottom.getX()) >= Math.abs(left_top.getY()-right_bottom.getY()))
			radius = Math.abs(left_top.getY()-right_bottom.getY());
		else
			radius = Math.abs(left_top.getX()-right_bottom.getX());
		return Math.PI * radius * radius;
	}

	public String getName() {
		return super.getName();
	}
}

Rectangle

~cpp public class Rectangle extends Shape {
	//right_bottom
	//Point left_top
	
	public Rectangle() {

	}

	public Rectangle(Point P1, Point P2) {
		super(P1, P2);
		name = "Rectangle";
	}

	public Rectangle(int x1, int y1, int x2, int y2) {
		super(x1, y1, x2, y2);
		name = "Rectangle";
	}

	public double getArea() {
		return Math.abs(right_bottom.getX()-left_top.getX()) * Math.abs(left_top.getY()-right_bottom.getY());		
	}

}

  • Unit
    • HP, 공격력, (공격, 드, 무브, ), 공공격가능
    • 공격다, 다, 무브다, 다(를 바꿔)
    • 공격면 HP가 깎다( 공격력 ) (ex public void underattack(int aAttackPoint) )

  • 롯(Unit )
    • HP 100, 공격력10, 공공격 가능
    • 드 100
  • 드라군(Unit )
    • HP 150, 공격력20, 공공격 가능
    • 드 150
  • 리, 드라군 만든다.
  • 로 드라군 공격다.

  • java 는 디 ?? 디 다면 것 같데. -
    • 라는 것 모르겠다 ; --iruril
      • : type functionName( type A1, type A2, A3 = 0 ); 달 되 달되는 값 기능다.
  • 또 super() 부모 만 되는 ?? --
    • 나 메 가능
      를들면 circle getName 메 super.getName() 렇게 --iruril
      • 고맙다~^^
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:32
Processing time 0.0192 sec