java공부하기94

 

추상클래스의 사용 이유

 

- 클래스의 구조를 디자인하기 위함

: 작업을 수평적으로 분할

: 레벨 단위의 구조로 만듦

 

 

 

 

추상클래스의 사용

 

abstract class Shape

{

public abstract void draw();

public abstract void delete();

public void doAll()

{

draw();

delete();

}

}

class Circle extends Shape{

public void draw() {

System.out.println("원을 그립니다.");

}

public void delete() {

System.out.println("원을 지웁니다");

}

}

 

class Triangle extends Shape {

public void draw() {

System.out.println("삼각형을 하나, 둘, 셋, 그립니다.");

}

public void delete() {

System.out.println("삼각형을 지웁니다");

}

}

 

class Rectangle extends Shape {

public void draw() {

System.out.println("사각형을 원, 투, 쓰리, 포 그립니다.");

}

public vlid delete() {

System.out.println("사각형을 지웁니다");

}

}

//abstract을 테스트하기 위한 클래스

public class FigureTest {

public static void main(String[] args) {

Circle c = new Circle();

Triangle t = new Triangle();

Rectangle r = new Rectangle();

Shape s;

s= c;         // 에러발생 안함. C의 클래스 Circle은 s의 서브타입임

s.draw();    // s은 draw라는 메소드를 가진 타입임

s.doAll();

c.draw();

t.draw();

r.draw();

c.delete();

t.delete();

r.delete();

}

}

 

 


 

JAVA공부하기 93

 

추상클래스란?

 

- 미완성 클래스

 

- 추상메서드가 포함된 클래스

: 자동으로 추상클래스가 됨

 

- abstract키워드가 붙은 완전한 클래스

: 구조상 상속해서 재정의해야만 사용가능

 

- 자체적으로 객체를 생성할 수 없음

: 하위클래스에서 추상메서드를 반드시 구현해야함

 

-타입 정보로는 사용

 


 

추상클래스와 추상메서드

 

- 추상 메서드를 포함하는 클래스는 반드시 추상클래스

: 클래스에 abstract를 표시하지 않으면 에러

 

- 추상클래스는 추상 메서드를 포함 가능

 

 

 

간단한 예제

 

: 반드시 포함해야 하는 것은 아님

 

- 하위클래스에서 상속받은 추상 메서드를 반드시 재정의해야함

: 하나라도 재정의하지 않으면 하위클래스도 추상클래스가 됨

 

- 추상 메서드

: 메서드의 원형만 선언된 메서드 : 블록({})이 없는 메서드

 

 

 

JAVA공부하기92

super의 사용형식

 

상위 클래스의 멤버변수나 메서드를 명시할 때

- super.variable_name

- super.method_name(arguments)

 

상위 클래스의 생성자를 호출할 때

- super(arguments)

 

super의 사용의 예

 

 

JAVA 공부하기 91.

this의 사용

 

public class ThisTest4

{

static String str1;

 

ThisTest4()

{ this("jsun"); }        // 생성자에서 생성자 호출

 

ThisTest4(String v1)

{ str1="hello"+ v1; }

 

public static void main(String[] args)

{

ThisTest4 obj1= new ThisTest4 ();

System.out.println(obj1.str1);

}

}

 

 


 

 

public class ThisTest5

{

int Q1=4;

 

public static void main(String[] args)

{

ThisTest5 obj1=new ThisTest5();

obj1.method1(6);

}

 

public void method1(int v1)

{

this.Q1 +=v1;

System.out.println(this.Q1);        // this.변수

}

}

 

 

 

 

 

JAVA공부하기 90

 

this의 사용

 

public class ThisTest2

{

int i=4;

 

public static void main(String args[])

{

ThisTest2 obj1 = new ThisTest2();

obj1.mth1();

System.out.println(this.i);

}

 

void mth1()

{

int i=10;

System.out.println("local i=" +i);

System.out.println("Member i=" + this.i);

}

}

 


 

public class ThisTest3

{

public static void main(String[] args)

{

ThisTest3 obj1 = new ThisTest3();

obj1.mth1();

}

public void mth1()

{

int result1= this.mth2(2,4);        // this.메서드

System.out.println(result1);

}

public int mth2(int v1, int v2)

{

int v3=v1+v2;

return v3;

}

}

 

 

 

JAVA 공부하기 89

 

this의 의미

- this는 자기 자신을 포함시키는 객체를 말함

 

 

 

this의 사용법

 

this 

언젠가 생성될 자신 객체의 메모리의 주소

 

this. 멤버필드

- 클래스내의 자신의 멤버를 직접 이용

- 메서드의 매개변수와 클래스의 멤버필드가 동일할 때 이를 구분하기 위해 사용

- this 키워드를 사용하지 않아도 무방

 

this. 멤버 메서드

- this. 멤버필드와 마찬가지로 자신의 멤버 메서드를 직접 이용

 

this()

- 클래스 자신의 생성자 메서드를 호출할 때 사용

- 자신의 생성자 메서드를 재이용

: 생성자 메서드에서 다른 생성자 메서드를 호출할 때 생성자 메서드의 호출은 제일 윗부분에서 사용해야함

 

JAVA공부하기 88

클래스 추상화와 상속 실습

 

- 클래스의 추상화 -> 일반화

- 경찰, 군인, 소방관이라는 각각의 클래스 생성

: InheritanceText.java

 

 

 

- 변수와 메서드의 중복 발생

 

- 추상화를 통한 상속

: InheritanceTest2.java

 

 

 

- 다형성 제공

: overriding, overloading

 

 

- 보다 유연한 개발이 가능

 

JAVA공부하기87

상속의 이해

 

public class Ant

{

private String name="Ant";

public String getName()

{ return name; }

public String toString()

{ return "이름 : "+name;}

public void pring()

{ System.out.println(name+"은 동굴에 산다.") }

}

 

public class WaterAnt extends Ant

{

private String where="water";

public void show()

{ System.out.println(where+"주변"); }

public void print()

{ System.out.println(getName()+"은 "+where+"에 산다."); }

}

 

public class WaterAntMain

{

public static void main(String[] args)

{

Ant ant=new Ant();

ant.print();

System.out.println(ant.toString());

 

WaterAnt sant=new WaterAnt();

want.print();

want.show();

System.out.println(want.toString());

}

}

 

 

 

WaterAnt want=new WaterAnt();

 

 

 

 

 

Ant ant1 = new Ant();

 

 

WanterAnt want= new WanterAnt();

+ Recent posts