'Java'에 해당되는 글 15건

  1. 2011.01.11 파일과 입출력 API
  2. 2011.01.11 java.util 패키지(유틸리티 API)
  3. 2011.01.06 static정리
  4. 2011.01.06 추상클래스 / 인터페이스
  5. 2011.01.05 instanceof연산자
2011. 1. 11. 15:59
[핵심정리]!!!
!프로그램을 기준으로
① Source => 입력처리 스트림
   Destination -> 출력처리 스트림


② 처리되는 단위
    - byte(1byte) : byte 스트림
    - char(2byte) : char 스트림

③ 타겟으로부터 직접 읽거나/쓰는 스트림 => Node Stream
    -노드 스트림 객체는 반드시 하나 이상 있어야 한다.
    -노드 객체를 만드는 것은 파일 open을 의미
     노드가 될 수 있는 것 : File, 메모리(Array, Stream), pipe, socket
     [노드스트림] : 노드에 직접 연결되어 있는 스트림을 의미
     char : FileReader / FileWriter
     byte : FileInpuStream / FileOutputStream

④ 타겟으로부터 직접 읽거나/쓰는 것이 아닌 스트림 => Filter 스트림
 (중간처리는 모두 Filter 스트림이 함)







1. 자바 I/O 스트림 소개
 1) 스트림(Stream)이란?
    데이터의 순차적 흐름을 의미
    - 데이터가 출발하는 출발지 또는 근원지(Source)가 있다면 데이터가 도착하는 종착지(Destination)가 있습니다.
    - 데이터의 출발지와 종착지 사이의 데이터의 흐름을 스트림이라고 부릅니다.

 3)스트림 분류
java.io 패키지는 데이터를 읽고 쓰기 위해 필요한 스트링 클래스들을 모아놓고 있습니다.
 * 바이트스트림 : 주로 binary데이터를 처리  * 문자스트림: 주로 문자를 처리
 입출력 작업 단위 : 8비트
제공되는 API :  InputStream/OutputStream 계열
사용되는 경우 : 이미지 오디오와 같은 바이너리 데이터를 입출력 하고자 할 때
입출력 작업 단위 : 16비트
제공되는 API : Reader /Writer 계열
사용되는 경우 : 텍스트 데이터를 입출력 하고자 할 때 
- 대부분의 프로그램은 텍스트 데이터를 읽고 쓸 때 reader와 writer을 씁니다.



1)노드스트림

문자스트림

바이트스트림

파일

FileReader

FileWriter

FileInputStream

FileOutPutStream

메모리 : 배열

CharArrayReader

CharArrayWriter

ByteArrayInputStream

ByteArrayOutputStream

메모리 : 스트림

StringReader

StringWriter

파이프

PipedReader

PipedWriter

PipedInputStream

PipedOutputStream



2. 필터스트림: 가공하는 작업을 하는 스트림(0개 이상)
                     필요하면 집어 넣어 쓰세요.
  필터 스트림은 프로세싱 스트림이라고 함
  특정 기능을 수행하거나 그런 기능을 갖고 있는 메소드를 사용하기 위해서 기존 스트림을 감싸는 역할을 하는 스트림



File 클래스
File객체는 실제로 파일을 오픈하는 것이 아니고, 체크하는 작업을 한다.
(파일에 대한 정보를 보는 클래스)
File file = new File("파일명");
File file = new File("디렉토리명", "파일명");

import java.io.File;
import java.io.IOException;
public class FileTest {
 public static void main(String[] args) {
  File file = new File("c:/data/test.txt");
  System.out.println("파일명 : " + file.getName());
  System.out.println("파일 절대 경로 : " + file.getAbsolutePath());
  System.out.println("파일size : " + file.length());
  if( file.canExecute())
   System.out.println("실행할  수 있다.");
  if( file.canRead())
   System.out.println("읽을 수 있다.");
  if( file.canWrite())
   System.out.println("쓸 수 있다.");
  
  File file2 = new File("c:/data/hello.txt");
  if( file2.exists())
   System.out.println("파일이 존재합니다.");
  else{
   System.out.println("파일이 존재하지 않습니다");
   try {
    if(file2.createNewFile())
     System.out.println("파일이 성공적으로 생성하였습니다.");
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  } 
 }
}
[console]
파일명 : test.txt
파일 절대 경로 : c:\data\test.txt
파일size : 62
실행할  수 있다.
읽을 수 있다.
쓸 수 있다.
파일이 존재합니다.






FileReader/FileWriter 클래스

∎FileReader/FileWriter는 반드시 Exception처리를 해 주어야 한다.
∎FileReader객체와 FileWriter객체를 생성해 주었다면, 반드시 close시켜주어야 한다.

<Reader의 메소드>
* int read() 
 : 입력소스로부터 하나의 문자를 읽어 온다. char의 범위인  0~65535범위의 정수를 반환하며, 입력스트림의 마지막 데이터에 도달하면, -1을 반환한다.

* int read(char[] c)
: 입력소스로부터 매개변수로 주어진 배열 c의 크기만큼 읽어서 배열 c에 저장한다. 읽어 온 데이터의 개수 또는 -1을 반환한다..

*abstract void closd()
: 입력 스트림을 닫음으로써 사용하고 있던 자원을 반환한다.

<Writer의 메서드>
* void write(int b)
: 주어진 값을 출력  소스에 쓴다.

* abstract void close()
: 출력스트림을 닫음으로써 사용하고 있던 자원을 반환한다.

* abstract void flush()
: 스트림의 버퍼에 있는 모든 내용을 출력소스에 쓴다.(버퍼가 있는 스트림에만 해당됨)

 

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileReadTest {
 public static void main(String[] args) {
  File file = new File("c:/data/test.txt");
  FileReader in = null;
  FileWriter out = null;
  try {
   in = new FileReader(file);
   out = new FileWriter("c:/data/test.backup.txt");
   int ch = 0;
   while( (ch = in.read()) != -1)
    out.write(ch);
   
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   if(in != null) try { in.close();} catch (IOException e) {e.printStackTrace();}
   if(out != null) try { out.close();} catch (IOException e) {e.printStackTrace();}
  }
 }
}

 




Copy클래스 만들기.. java simple.syeong.ch13.Copy args[0] args[1]







BufferedReader/BufferedWriter
라인단위로 읽고 쓰는 필터 스트림


public class Copy {
 public static void main(String[] args) {
  if( args.length < 2){
   System.out.println("사용법 : java simple.syeong.ch13.Copy args[0] args[1]");
   System.exit(2);
   //argument에러는 2로 처리.
   
  }
  
  File sfile = new File(args[0]);
  File tfile = new File(args[1]);
  
  if(! sfile.canRead()){
   System.out.println(args[0] + "파일은 읽을 수 없습니다.");
   System.exit(1);
   //error코드로 처리
  }
  
  if( tfile.exists()){
   System.out.println(args[1] + "이미 존재하는 파일입니다.");
   System.exit(1);
  }
  
  FileReader in = null; //입력노드스트림
  FileWriter out = null; //출력노드스트림
  BufferedReader br = null; //라인 단위로 읽을 수 있는 필터스트림
  BufferedWriter bw = null; //라인 단위로 쓸 수 있는 필터스트림
  
  try {
   //FileReader in = new FileReader(new File("c:/data/test.txt"));
   in = new FileReader(sfile); //char단위로 파일에서 읽기
   out = new FileWriter(tfile); //char단위로 파일에서 쓰기.
   br = new BufferedReader(in);
   bw = new BufferedWriter(out);
   String line = null;
   while( (line = br.readLine()) != null){
    bw.write(line);
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   if(in != null) try { in.close();} catch (IOException e) {e.printStackTrace();}
   if(out != null) try { out.close();} catch (IOException e) {e.printStackTrace();}
  }
 }
  
}
위의 코드는 약간의 문제가 있다.
쓸데 없는 로컬변수는 in과 out을 썼기 때문이다. 이경우 코드를 다음과 같이 고쳐조는 것이 좋다.
public class Copy {
 public static void main(String[] args) {
  if( args.length < 2){
   System.out.println("사용법 : java simple.syeong.ch13.Copy args[0] args[1]");
   System.exit(2);
   //argument에러는 2로 처리.
   
  }
  
  File sfile = new File(args[0]);
  File tfile = new File(args[1]);
  
  if(! sfile.canRead()){
   System.out.println(args[0] + "파일은 읽을 수 없습니다.");
   System.exit(1);
   //error코드로 처리
  }
  
  if( tfile.exists()){
   System.out.println(args[1] + "이미 존재하는 파일입니다.");
   System.exit(1);
  }
  
  //FileReader in = null; //입력노드스트림
  //FileWriter out = null; //출력노드스트림
  BufferedReader br = null; //라인 단위로 읽을 수 있는 필터스트림
  BufferedWriter bw = null; //라인 단위로 쓸 수 있는 필터스트림
  
  try {
   //FileReader in = new FileReader(new File("c:/data/test.txt"));
   //in = new FileReader(sfile); //char단위로 파일에서 읽기
   //out = new FileWriter(tfile); //char단위로 파일에서 쓰기.
   br = new BufferedReader(new FileReader(sfile));
   bw = new BufferedWriter(new FileWriter(tfile));
   String line = null;
   while( (line = br.readLine()) != null){
    bw.write(line);
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   if(br != null) try { br.close();} catch (IOException e) {e.printStackTrace();}
   if(bw != null) try { bw.close();} catch (IOException e) {e.printStackTrace();}
  }
 }
  
}





1.파일 객체를 생성합니다.
  File = fi = new File("a.txt");

2.FileInputStream 객체를 생성합니다.
  FileInputStream fi = new FileInputStream(file)

3. InputStreamReader (1바이트씩 들어오는 데이터를 2바이트로 묶는..)
   InputStreamReader isr = new InputStreamReader(fi);

4. BufferedReader 객체를 생성합니다.(line단위로 읽는)
   Buffered br = new BufferedReader(isr)

5. 파일에서 데이터를 읽음
(콘솔에서는 입출력이 byte이기에 이러한 작업을 함)









'Java' 카테고리의 다른 글

표현언어(Expression Language)  (0) 2011.01.27
java.util 패키지(유틸리티 API)  (0) 2011.01.11
static정리  (0) 2011.01.06
추상클래스 / 인터페이스  (0) 2011.01.06
instanceof연산자  (0) 2011.01.05
Posted by Triany
2011. 1. 11. 13:45

java.util 패키지는 사용자 프로그램에서 필요로 하는 편리한 기능들을 모아 둔 클래스들의 모임이다.

Date클래스
: Date 클래스는 날짜와 시간에 관한 정보를 표현한다.
그러나 JDK의 버전이 향상되면서 deprecate되는 메서드들이 많다.
Date보다는 Calendar클래스를 더 권장.





Calendar클래스(추상클래스)
Calendar 클래스도 Date클래스처럼 날짜와 시간에 관한 정보를 표혈할 때 사용한다.(Calendar클래스를 Date클래스보다 더 권장)
⊙Calendar 클래스는 추상 클래스로 생성자를 제공하지 않는다.
   - 따라서 Calendar 클래스의 객체를 생성하기 위해 new 연산자를 사용할 수 없다. 
  - 추상 클래스이므로 객체를 직접 생성할 수 없지만, 
     getInstance() 메서드를 이용하여 시스템의 날짜와 시간의 객체를 얻어올 수 있다
.

int get(int field)
//날짜에서 표현할 수 있는 값은 다 상수값으로 제공
//calendar 클래스는 날짜와 시간 표현을 위해 다양한 상수를 제공하고 있다.







'Java' 카테고리의 다른 글

표현언어(Expression Language)  (0) 2011.01.27
파일과 입출력 API  (0) 2011.01.11
static정리  (0) 2011.01.06
추상클래스 / 인터페이스  (0) 2011.01.06
instanceof연산자  (0) 2011.01.05
Posted by Triany
2011. 1. 6. 09:33

<객체와 상관 없이!! 객체 생성 이전에 로드되는!!>
-static  멤버들을 객체의 생성 없이 호출할 수 있는 것은 객체 생성 이전에 메모리가 할당되기 때문입니다.
-즉 객체 생성 이전에 클래스 로더에 의해 클래스가 로딩될 때 메모리에 올라가기 때문에 객체를 만들기 전에 클래스 이름만으로도 호출이 가능합니다.

그렇기 때문에 이미 메모리에 올라간 static 멤버가 아직 메모리에 올라가지 않은, static이 아닌 멤버를 호출하는 것은 불가능하다.


  ex)  Class A{
             int a; //
             static int c; //클래스 변수
             static void abc(){
                    int b = a;
             }  //Error! 불가능.
         }
ex)
System.out.println()  //System클래스에 있는 out이라는 객체를 가져와서, out이라는 객체의 println()이라는 함수를 쓰겠다.


1, static 메서드 - static 변수 OK
                         클래스 멤버변수 X

2. 클래스 멤버 메서드 - static 변수 OK
                                 class 멤버 변수 O.K


<static 키워드의 정리>

1. static 멤버는 클래스 변수, 클래스 메소드라고 불리웁니다.
2. 객체 생성없이 [클래스이름.멤버이름]으로 static 멤버를 호출할 수 있습니다.
3. 그 클래스로부터 만들어진 모든 객체들이 static 멤버를 공유합니다.
4. static 멤버는 객체 생성 이전에 클래스가 로딩될 때, 메모리에 먼저 올라갑니다.
5. static 멤버는 static이 아닌 멤버를 직접 호출할 수 없습니다.
6. this와 함께 쓰일 수 없습니다.
7. 부모 클래스의 static 메소드는 메소드 오버라이딩이 안됩니다.



main() 메소드
main()메소드가 static으로 정의되어져야 하는 이유
main()메소드는 JVM이 프로그램을 시작하는 시작점이기 때문에 main()을 가지고 있는 클래스가 객체를 생성하는 것과 상관없이 무조건 실행되어야 하기 때문입니다.

main()메소드의 접근 지정자가 public 인 이유
클래스가 어느 위치에 있건 간에 JVM에서 접근하려면 가장 넓은 범위의 접근 범위를 설정해야 하기 때문입니다.


static 이니셜라이저(Initializer)

static 이니셜라이저(Initializer)란, 메소드 바깥 쪽에 다음과 같은 형태로 static 코드 블록을 만드는 것을 의미합니다.

 public class MyClass {
   static {
      
   }
   //멤버 변수 정의
   //멤버 메소드 정의
}
 - static 코드 블록 안의 코드는 클래스가 로딩될 때 단 한번만 실됩니다.
-  static형식으로 초기화 됨.


'Java' 카테고리의 다른 글

표현언어(Expression Language)  (0) 2011.01.27
파일과 입출력 API  (0) 2011.01.11
java.util 패키지(유틸리티 API)  (0) 2011.01.11
추상클래스 / 인터페이스  (0) 2011.01.06
instanceof연산자  (0) 2011.01.05
Posted by Triany
2011. 1. 6. 00:21
추상:낱낱의 구체적 표상이나 개념에서 공통된 성질을 뽑아 이를 일반적인 개념으로 파악하는 정신 작용
 
추상화 - 클래스간의 공통점을 찾아내서 공통의 조상을 만드는 작업 
구체화 - 상속을 통해 클래스를 구현, 확장하는 작업
추상클래스? 왜 구현할까? 자식클래스에 반드시 구현해야 하기 때문에 사용.
1) fA(){}
2) abstract void fB(); =>규격

 2)번 함수는 상속받아야 하는 자식에이 반드시 구현을 해야 하는 함수이기에, abstract로 선언을 하여 강제성을 주어 상속받은 클래스가 "반드시 구현"할 것을 요구.
ex) 탈것이라는 부모클래스에 abstract 키워드를 사용한 연비계산 추상메소드를 만들고
이를, 자동차 배, 기차등이 상속하여 연비계산을 반드시 구현하도록 함.


1. 추상클래스 ( abstract class 클래스이름{} )

  - 추상클래스에도 생성자가 있으며, 멤버변수와 메서드도 가질 수 있다.
  - 추상메서드가 있으면 반드시 abstract.(추상메소드를 단 하나라도 가지고 있는 클래스를 의미한다.)
  - 추상메서드가 없는 완성된 클래스라 할 지라도 추상클래스로 지정되면 클래스의 인스턴스를 생성할 수 없다.
  - abstract class A -> 객체생성 못 (규격으로 쓰겠다.)
public abstract class Flyer{
   abstract public void takeOff();
   public void getSpeed(){
      System.out.println(" ");
   }
}
클래스를 설계도에 비유한다면, 추상클래스는 비완성 설계도에 비유할 수 있다. 미완성 설계도란, 단어의 뜻 그대로 완성되지 못한  채로 남겨진 설계도를 말한다.
추상클래스 자체로는 클래스로서의 역할을 다 못하지만, 새로운 클래스를 작성하는데 있어서 바탕이 되는 조상 클래스로서 중요한 의미를 갖는다. 새로운 클래스를 작성할 때 아무 것도 없는 상태에서 시작하는 것보다 완전하지 못하더라도 어느 정도 틀을 갖춘 상태에서 시작하는 것이 나을 것이다.


2. 추상메서드
- 선언부만 작성하고 구현부는 작성하지 않은 채로 남겨 둔 것.
- 추상클래스로부터 상속받는 자손클래스는 오버라이딩을 통해 조상인 추상클래스의 추상메소드를 모두 구현해중어야 한다.
  만일 조상으로 부터 상속받은 추상메서드 중 한라도 구현하지 않는다면, 자손클래스 역시 추상클래스로 지정해 주어야 한다.

[예제] - [추상클래스의 사용예와, inner class]
public abstract class ParentNode {
//The type ParentNode must be an abstract class to define abstract methods
 int age;
 String name;
 
 public abstract void display();
 //The abstract method display in type ParentNode can only be defined by an abstract class
 public void hello(){
  System.out.println(name + "님 안녕하세요");
 }
}
public class NodeTest {

public static void main(String[] args) {
  // TODO Auto-generated method stub
  //ParentNode pn = new ParentNode();
  //Cannot instantiate the type ParentNode
  
  
  NodeTest nt = new NodeTest();
  NodeTest.ChildNode cn = nt.new ChildNode();

  //multiple markers at this line
 }
 
 class ChildNode extends ParentNode{
  //The type NodeTest.ChildNode must implement the inherited abstract method ParentNode.display()
  @Override
  public void display() {
      // TODO Auto-generated method stub
   
  }
 }
}
내부클래스는, 해당 클래스 안에서만 쓸 수 있다.
내부 클래스를 사용 하기 위해서는,
NodeTest node = new NodeTest();
NodeTest.ChildNode pn = nod.new ChildNode();
의 식으로 선언한다.
 1)
 Class NodeTest{
     ......
 }
 Class ChildNode{
    .....
 }
 2)
 Class NodeTest{
    .....
    Class ChildNode{
       ...
    }
 }
 클래스 NodeTest와 클래스 ChildNode는 별개의 클래스다.
 NodeTest.java 파일을 컴파일하면,
 NodeTest.class와
 ChildNode.class 두 클래스 파일이 생성된다.
 [내부클래스이다.]
ChildNode를 쓸려면 NodeTest를 만들어야 가능하다.
컴파일 하면, NodeTest.class와
                  NodeTest$ChildNode.class 파일이 생성된다.
그냥 ChildNode cn = new ChildNode()로 선언하면 에러
=>해결법 ? static으로 클래스를 선언하든지,
   내부 클래스를 1)의 형식으로 해주면 OK.

 
# 인터페이스는 왜 사용하는가?
예)
  * 핸드폰과 핸드폰 충전기가 있을 때,
 - 핸드폰 종류별로 충전기가 다 다를 경우 외부 출장 등을 갔을 때 핸드폰 충전을 하기가 불편합니다.
 - 핸드폰 종류별 동일한 충전기를 사용할 수 있도록 하기 위해서는 핸드폰 제조 업체와 충전기 제조 업체 쪽에 동일한 인터페이스를 사용할 수 있도록 하기 위한 규약서를 만들어서 제공해야 합니다.
 - 이 규약서는 핸드폰과 충전기 사이의 인터페이스가 됩니다.
이러한 규약서를 자바 프로그래밍으로 표현할 때 인터페이스가 된다.

<인터페이스> : 규격을 정하는 용도로 사용(규약서)
- 인터페이스는 형태적으로 추상 클래스가 더욱 추상화된 형태로, 모든 메소드가 추상 메소드인 클래스를 의미합니다.
- 인터페이스는 일종의 추상클래스이다.
- 인터페이스는 추상클래스처럼 추상메서드를 갖지만 추상클래스보다 추상화 정도가 높아서,
   추상클래스와 달리 몸통을 갖춘 일반 메서드 또는 멤버변수를 구성원으로 가질 수 없다.
- 오직 추상메서드와 상수만을 멤버로 가질 수 있으며, 그 외의 다른 어떠한 요소도 허용하지 않는다.
- 접근제어자로 public 또는 default를 사용한다.
- 인터페이스는 인터페이스로부터만 상속받을 수 있으며, 
  클래스와는 달리 다중상속, 즉 여러 개의 인터페이스로부터 상속을 받는 것이 가능하다.

interface 인터페이스이름{
     public static final 타입 상수이름 = 값;
     public abstract 메서드이름(매개변수 목록);
     //static final과 abstract 생략가능 
}
public interface Flyer{
     public void takeOff();
     public void land();
     public void fly();
}
public class Airplane implements Flyer{
     public void takeOff(){
     /*                      구현                      */
     }
     public void land()(){
     /*                      구현                      */
     }
     public void fly()(){
     /*                      구현                      */
     }
}

 

일반적인 클래스의 멤버들과 달리 인터페이스의 멤버들은 다음과 같은 제약사항을 가지고 있다.
- 모든 멤버변수는 public static final 이어야 하며, 이를 생략할 수 있다.
    -> 상수값만 가능. (멤버 변수x)
- 모든 메서드는 public abstract 이어야 하며, 이를 생략할 수 있다.
    ->인터페이스에 추상 메소드 선언 시 abstract이라는 키워드를 사용하지 않습니다. 왜냐하면 인터페이스는 추상메소드만 가질 수 있기 때문입니다.
[참고] 인터페이스에 정의된 모든 멤버에 예외없이 적용되는 사항이기 때문에 제어자를 생략할 수 있는 것이며, 편의상 생략하는 경우가 많다. 생략된 제어자는 컴파일 시에 컴파일러가 자동으로 추가해준다.


*인터페이스 구현
class 클래스이름 implements 인터페이스이름 {
   //인터페이스에 정의된 추상메서드를 구현해야 한다.
}
만일 구현하는 인터페이스의 메서드 중 일부만 구현한다면, 추상클래스로 선언되어야 한다.


예제소스1)
public interface Flyer {
 public void takeOff();
 public void land();
 public void fly();
}
public class Airplane implements Flyer {
 @Override
 public void takeOff() {
  System.out.println("앞바퀴부터 떨어짐.");
 }
@Override
 public void land() {
  System.out.println("뒷바퀴부터 닿음");
 }
 @Override
 public void fly() {
  System.out.println("쌩하고 날라감");
 }
}
public class Bird implements Flyer {
 @Override
 public void takeOff() {
  System.out.println("열심히 뛰어서 날아오름");
 }
 @Override
 public void land() {
  System.out.println("바람을 타고 내려옴.");
 }
 @Override
 public void fly() {
  System.out.println("훨훨 날아다님");
 }
 public void eat(){
  System.out.println("부리로 쪼아서 먹음.");
 }
}
public class TestFlyer {
 public static void main(String[] args) {
  Flyer airplane = new Airplane();
  Flyer bird = new Bird();
        //Flyer flyer = new Flyer();//error
  //Cannot instantiate the type Flyer
  System.out.println("\n비행기========");
  airplane.takeOff();
  airplane.land();
  airplane.fly();
  
  
  System.out.println("\n\n새========");
  bird.takeOff();
  bird.land();
  bird.fly();
  //bird.eat(); //error. Flyer로 선언된 bird는 eat()메소드에 접근하지 못함
  if(bird instanceof Bird)
  {
   Bird b = (Bird)bird;
   b.eat();
  }
 }
}
[console]
비행기========
앞바퀴부터 떨어짐.
뒷바퀴부터 닿음
쌩하고 날라감


새========
열심히 뛰어서 날아오름
바람을 타고 내려옴.
훨훨 날아다님
부리로 쪼아서 먹음.
[인터페이스를 자료형으로 사용]
Flyer airplane = new Airplane();
Flyer bird = new Bird();

[형변환]
if ( bird instanceof Bird)
    Bird b = (Bird)bird;



[클래스 상속, 인터페이스 구현 클래스]
클래스는 상속(extends)과 구현(implement)을 동시에 할 수있다.
순서 중요!!!!! (extends가 먼저 쓰여지고 뒤에 implement를 써야 함.)
public interface Human {
}
public interface Flyer {
}
public class Bird implements Flyer {
}

public class SuperMan2 extends Bird implement Human {
}

public class 클래스명 extends 부모클래스명 implements 인터페이스명1, 인터페이스명2 { }


[비교] abstract클래스와 interface의 용도를 비교하여 설명.
 * abstract
    1) 일부기능은 제공, 일부기능 규격을 제공
    추상메서드를 한개이상 가지고 있는 클래스
    단일상속만 된다.

 * interface
    1) 규격을 제공
    모든메서드가 추상메서드인 클래스
    다중상속이 가능하다.



 인터페이스의 장점
- 개발시간을 단축시킬 수 있다.
- 표준화가 가능하다.
- 서로 관계없는 클래스들에게 관계를 맺어 줄 수 있다.
- 독립적인 프로그래밍이 가능하다.

'Java' 카테고리의 다른 글

표현언어(Expression Language)  (0) 2011.01.27
파일과 입출력 API  (0) 2011.01.11
java.util 패키지(유틸리티 API)  (0) 2011.01.11
static정리  (0) 2011.01.06
instanceof연산자  (0) 2011.01.05
Posted by Triany
2011. 1. 5. 22:49
참조변수가 참조하고 있는 인스턴스의 실제 타입을 알아보기 위해 사용한다. 주로 조건문에 사용되며, instanceof의 왼족에는 참조변수를 오른쪽에는 타입(클래스명)이 피연산자로 위치한다. 그리고 연산의 결과로 boolean값인 true, false 중의 하나를 반환한다.
instanceof를 이용한 연산결과로 true를 얻었다는 것은 참조변수가 검사한 타입으로 형변환이 가능하다는 것을 뜻한다.
[참고] 값이 null인 참조변수에 대해 instanceof연산을 수행하면 false를 결과로 얻는다.

void dowork(Car c){
     if ( c instanceof FireEngine) {
          FireEngine fe = (FireEngine)c;
          fe.water();
     }
    else if( c instance of Ambulance) {
          Ambulance a = (Ambulance)c;
          a.siren();
    }

  . . . . . . .
}




참조변수와 인스턴스의 연결
-메서드 : 조상클래스의 메서드를 자손의 클래스에서 오버라이딩 한 경우에도 참조변수의 타입에 관계없이 항상 실제 인스턴스의 메서드(오버라이딩된 메서드가 호출됨)

멤버변수의 경우 참조변수의 타입에 따라 달라짐
-멤버변수가 조상 클래스와 자손 클래스에 중복으로 정의된 경우,
조상 타입의 참조변수를 사용했을 때 조상 클래스에 선언된 멤버변수가 사용되고,
자손타입의 참조변수를 사용했을 때는 자손 클래스에 선언된 멤버변수가 사용된다.

예1)
class Parent{
     int x = 100;
  
     void method() {
          System.out.println("Parent Method");
     }
}

class Child extends Parent {
     int x = 200;

     void method() {
          System.out.println("Child Method");
     }
 
}


Class BindingTest{
     public static void main(String[] args) {
          Parent p = new Child();
          Child c = new Chile();
          System.out.println("p.x = " + p.x);
          p.method();
          System.out.println("c.x = " + c.x);
          c.method();
     }
}      
[결과값]
p.x = 100;
Chiild Method
c.x = 200;
Child Method




예2)
public class Parent {
    public int su = 10;
    void pr(){
          System.out.println(su);
    }
}
public class Child extends Parent {
    public int su = 20;
    void pr(){
         System.out.println(su);
    }
}
public class TestParentChild { 
   public static void main(String[] args) {
  
       System.out.println("*Parent p1 = new Parent() *");
       Parent p1 = new Parent();
       System.out.print("Parent의 pr(): su = "); p1.pr();
       System.out.println("p1.su = " + p1.su);
  
       System.out.println("\n *Child c1 = new Child() *");
       Child c1 = new Child();
       System.out.print("Child의 pr() : su = "); c1.pr();
       System.out.println("c1.su= "+c1.su);
  
       System.out.println("\n *Parent p2 = new Child() *");
       Parent p2 = new Child();
       System.out.print("Child의 pr(): su = "); p2.pr();
       System.out.println("p2.su = " +p2.su);
   }
}
[Console]
*Parent p1 = new Parent() *
Parent의 pr(): su = 10
p1.su = 10

 *Child c1 = new Child() *
Child의 pr() : su = 20
c1.su= 20

 *Parent p2 = new Child() *
Child의 pr(): su = 20
p2.su = 10
마지막 선언 Parent p2 = new Child() 를 보면,
p2.su의 값을 출력해 보면 Parent클래스의 멤버변수 su값이 출력된다.
(조상타입의 참조변수를 이용하여 Child()클래스를 접근했으므로 참조 변수는 Parent의 su이다.)
하지만 참조메소드는 현재 인스턴스가 접근하는 클래스(Child())의 멤버 메소드 이므로,
void pr(){
         System.out.println(su);
    }
함수의 su가 this.su를 생략하고 있다고 보면, Child의 멤버변수 su의 값이 출력된다.



예3)
public interface Drawable {
    public int PLAIN_PEN = 1;
    public int BOLD_PEN = 2;
    public int ITALIC_PEN = 3;
    public void draw();
    public void move(int x, int y);
}
public class Shape1 {
    protected int x = 0;
    protected int y = 0;
    public Shape1() {
          this(0,0);
    }
    public Shape1(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
public class Circle1 extends Shape1 implements Drawable {
     protected int radius;
     public Circle1(int x, int y, int radius) {
          super(x, y);
          this.radius = radius;
     }
  
    @Override
    public void draw() {
          System.out.println("x = " + x + ",  y = " + y + "  radius = "+radius);
    }

    @Override
     public void move(int x, int y) {
          System.out.println("x = " + (this.x + x) + ",  y = " + (this.y+y) + "   radius = "+ radius);
     }
}
public class Rectangle1 extends Shape1 implements Drawable {
    protected int width;
    protected int height;
 
    public Rectangle1(int x, int y, int width, int height) {
         super(x, y);
         this.width = width;
         this.height = height;
    }

    @Override
    public void draw() {
         System.out.println("x = " + x + ",  y = " + y + "  width = "+ width + ",  height = " + height);
    }

    @Override
    public void move(int x, int y) {
          System.out.println("x = " + (this.x + x) + ",  y = " + (this.y+y) + "  width = "+ width + ",  height = " + height);
    }
}
public class InterfaceTest {
    public static void main(String args[]){
  Circle1 c = new Circle1(10,10,100);
  Rectangle1 r = new Rectangle1(20,20,50,50);
  c.draw();
  c.move(5, 5);
  r.draw();
  r.move(5, 10);
  
    }
}
[Console] InterfaceTest.java
x = 10,  y = 10  radius = 100
x = 15,  y = 15   radius = 100
x = 20,  y = 20  width = 50,  height = 50
x = 25,  y = 30  width = 50,  height = 50
public class InterfaceTestEx {
    public static void main(String args[])
   {
  Circle1 c = new Circle1(10,10,100);
  Shape1 s = new Circle1(20,20,200);
  Drawable d = new Circle1(30, 30, 300);
  c.draw();
  //s.draw();  //The method draw()is undefined for the type Shape1
  d.draw();
  System.out.println("x = " + c.x + ",  y = " + c.y);
  System.out.println("x = " + s.x + ",  y = " + s.y);
  // System.out.println("x = " + d.x + ",  y = " + d.y);
//  Multiple makers at this line
// -x cannot be resolved or is not a fild
// -y cannot be resolved or is not a file
    }
}
[Console] InterfaceTestEx.java
x = 10,  y = 10  radius = 100
x = 30,  y = 30  radius = 300
x = 10,  y = 10
x = 20,  y = 20
InterfaceTestEx.java 부분을 살펴보자
s.draw();는 error가 뜬다. 이유는, 참조변수 Shape1으로 선언한 s 객체는 Shape1이 draw() 함수를 갖고 있지 않기 때문에
 //The method draw()is undefined for the type Shape1 이라는 메시지가 뜨며 에러라고 표시된다.
마찬가지로 System.out.println("x = " + d.x + ",  y = " + d.y);도 error 메시지가 뜨게 되는데, Drawable 클래스는 x, y라는 멤버변수를 갖고 있지 않기때문에,  "Multiple makers at this line   -x cannot be resolved or is not a fild   -y cannot be resolved or is not a file" 라는 메시지가 뜨면서 에러가 뜬다.





'Java' 카테고리의 다른 글

표현언어(Expression Language)  (0) 2011.01.27
파일과 입출력 API  (0) 2011.01.11
java.util 패키지(유틸리티 API)  (0) 2011.01.11
static정리  (0) 2011.01.06
추상클래스 / 인터페이스  (0) 2011.01.06
Posted by Triany