'Java > Interface' 카테고리의 다른 글
| 인터페이스 다중상속 (0) | 2012.04.11 |
|---|---|
| 인터페이스 (0) | 2012.04.11 |
| 인터페이스 공통자료형 공통표준화 (0) | 2012.04.11 |
| 클래스 -> 인터페이스 형변환 (0) | 2012.04.11 |
| 인터페이스 쓰임새 (0) | 2012.04.11 |
| 인터페이스 다중상속 (0) | 2012.04.11 |
|---|---|
| 인터페이스 (0) | 2012.04.11 |
| 인터페이스 공통자료형 공통표준화 (0) | 2012.04.11 |
| 클래스 -> 인터페이스 형변환 (0) | 2012.04.11 |
| 인터페이스 쓰임새 (0) | 2012.04.11 |
package com.inter5;//클래스간 다중상속
interface IHello{
public abstract void sayHello(String name);
}
interface IGoodBye{
public abstract void sayGoodBye(String name);
}
//인터페이스는 클래스에 다중 구현(우회적 다중상속)할 수 있다
class Subclass implements IHello, IGoodBye{
public void sayHello(String name){
System.out.println(name+"씨 안녕??");
}
public void sayGoodBye(String name){
System.out.println(name+"씨 바이");
}
}
public class AbstractTest04 {
public static void main(String[] args) {
Subclass test = new Subclass();
test.sayHello(args[0]);
test.sayGoodBye(args[0]);
}
}| 예비 (0) | 2012.04.11 |
|---|---|
| 인터페이스 (0) | 2012.04.11 |
| 인터페이스 공통자료형 공통표준화 (0) | 2012.04.11 |
| 클래스 -> 인터페이스 형변환 (0) | 2012.04.11 |
| 인터페이스 쓰임새 (0) | 2012.04.11 |
package com.inter3;
interface IHello{
void sayHello(String name);
}
class Hello implements IHello{
public void sayHello(String name){
System.out.println(name + "씨 안녕하세요!");
}
}
public class InterfaceTest01 {
public static void main(String[] args){
Hello obj = new Hello();
obj.sayHello(args[0]);
}
}| 예비 (0) | 2012.04.11 |
|---|---|
| 인터페이스 다중상속 (0) | 2012.04.11 |
| 인터페이스 공통자료형 공통표준화 (0) | 2012.04.11 |
| 클래스 -> 인터페이스 형변환 (0) | 2012.04.11 |
| 인터페이스 쓰임새 (0) | 2012.04.11 |
package com.inter3;//인터페이스 공통자료형 , 공통표준화
class A{
void autoPlay(I i){ //여러 자료를 받기 위해 인터페이스의 I값을 가져옴
i.play();
}
}
interface I{
public abstract void play();
}
class B implements I {
public void play(){
System.out.println("play in B class");
}
}
class C implements I {
public void play(){
System.out.println("play in C class");
}
}
public class InterfaceTest2 {
public static void main(String[] args){
A a = new A();
a.autoPlay(new B()); //B -> I (클래스 타입 -> 인터페이스타입
a.autoPlay(new C()); //C -> I 자동 형변환)
}
}play in B class
| 인터페이스 다중상속 (0) | 2012.04.11 |
|---|---|
| 인터페이스 (0) | 2012.04.11 |
| 클래스 -> 인터페이스 형변환 (0) | 2012.04.11 |
| 인터페이스 쓰임새 (0) | 2012.04.11 |
| 인터페이스 다중 상속 (0) | 2012.04.11 |
package com.inter2;//인터페이스 타입으로 형변환
interface A{
public abstract int getA();
}
public class Round02 implements A{
//인터페이스 A의 추상메소드 getA()를 구현
public int getA(){
return 10;
}
public String getMsg(){
return "겨울";
}
public static void main(String[] args) {
Round02 rd = new Round02();
System.out.println(rd.getA());
System.out.println(rd.getMsg());
A a = rd; //클래스 타입 -> 인터페이스 타입
//자동적으로 형변환
System.out.println(a.getA());
/*System.out.println(a.getMsg());
호출 영역을 벗어나 호출 불가
인터페이스 안에 있는 추상메소드만 인식됨*/
Round02 rd2 = (Round02)a; //인터페이스 타입 -> 클래스 타입
//명시적 형변환
System.out.println(rd2.getA());
System.out.println(rd2.getMsg());
}
}10
| 인터페이스 (0) | 2012.04.11 |
|---|---|
| 인터페이스 공통자료형 공통표준화 (0) | 2012.04.11 |
| 인터페이스 쓰임새 (0) | 2012.04.11 |
| 인터페이스 다중 상속 (0) | 2012.04.11 |
| 인터페이스 2 (0) | 2012.04.11 |
인터페이스 용도
1.구조화된 클래 사용(클래스 설계를 위해서)
2.표준화(메소드 생성을 강요하기 때문에 구조 동일화)
3.우회적 다중상속 지원
4.자료형으로 사용(서로 관계가 없는 클래스에 관계형성)
인터페이스
//메뉴판을 보고 주문하면 진짜 음식이 생성되어 호출 // 다구현해둘 필요없다.
//여행후 목차-결과물에대한 목차//여행전 목차-목차대로 여행// 클래스르 도와주기위한 중요한 요소인 인터페이스!!
인터페이스의 탄생.
자바의 클래스 상속과 깊은관계
클래스
Object (toString) <=상속=> Parent (toString)<=상속=> Child (toString)(toString) 두개가 들어오면 명확히 구별할 방법이없다.
<=상속=> Uncle (toString)<= 상속=X 다중상속 불허 그래서 원척적으로 X 다중상속 불허 단일상속만 가능
interface(추상메소드) <=구현=> 일반클래스 (추상메소드 구현)
인퍼페이스의 다중상속
interface<=상속=>S<=상속=> C 다중상속이 가능하다 : 클래스의 정의를 도와줄뿐 메모리엔 올라기지않는다.
<=상속=>A<=상속=>
인터페이스의 다중구현
S <=구현=> 클래스 Child
A <=구현=>
C <=구현=>
class Hello{ = 컴파일 => Hello.class
파일명으론 확인불가
interface I{ = 컴파일 => I.class
인터페이스는 클래스가 아니다. 클래스와는 다른요소 = 클래스를 정의하기위한 용도로 사용 // 인터페이스는 객체 생성 불가능
= 상수와 추상메소드 로만 만든다.
상수
public static final 이 원형이다. 모두 public하고 static(주소만으로 호출가능)하며 final(상수이기때문에 변경불가)하다.
메소드
public abstract void 이 원형이다.
인터페이스를 implements 하면 추상메소드를 구현하도록 의무화시킴 (강제성)
생략해도 호출하면 public abstract void 형태로 불러짐 // 구현시 public 생략불가
인터페이스의 용도
1.구조화된 클래스사용 (클래스설계)
//글을쓸때 목차를 정해두고 글을쓰는것처럼
//구현하고자하는 메소드를 정의해둬서 클래스 설계를 잘하기위해 미리 인터페이스 설계
2.표준화 (구조 동일화)
//인터페이스가 메소드를 강요하기때문에
3.우회적 다중상속지원
4.자료형 (★서로 관계가 없는 클래스간에 관계를 형성)
같은 인터페이스를 구현
클래스Child 구현=> 인터페이스 I <= 클래스B 구현
자바의 자료형
1.기본자료형
2.참조자료형 1.클래스
2.배열
3.인터페이스
구현후 호출영역 형변환
클래스 -> 인터페이스형 자동형변환 호출영역 감소
인터페이스 -> 클래스형 명시적형변환 호출영역 확장
| 인터페이스 공통자료형 공통표준화 (0) | 2012.04.11 |
|---|---|
| 클래스 -> 인터페이스 형변환 (0) | 2012.04.11 |
| 인터페이스 다중 상속 (0) | 2012.04.11 |
| 인터페이스 2 (0) | 2012.04.11 |
| 인터페이스 기본 (0) | 2012.04.11 |
package com.inter2;//인터페이스 다중 상속
interface Inter1{
public abstract int getA(); //추상 메소드
}
interface Inter2{
public int getB(); //추상 메소드(abstract 생략가능)
}
//인터페이스간 다중 상속
interface Inter3 extends Inter1,Inter2{
public int getDate(); //추상 메소드
}
interface Inter4{
public int getC();//추상 메소드
}
public class Round01 implements Inter3,Inter4{
//Round01에 인터페이스를 구현하면
//해당인터페이스의 추상 메소드는 반드시 일반 메소드로 구현되어야 함
public int getA(){
return 10;
}
public int getB(){
return 20;
}
public int getC(){
return 30;
}
public int getDate(){
return 40;
}
public static void main(String[] args){
}
}| 인터페이스 공통자료형 공통표준화 (0) | 2012.04.11 |
|---|---|
| 클래스 -> 인터페이스 형변환 (0) | 2012.04.11 |
| 인터페이스 쓰임새 (0) | 2012.04.11 |
| 인터페이스 2 (0) | 2012.04.11 |
| 인터페이스 기본 (0) | 2012.04.11 |
package com.inter;//추상 인터페이스
//다중상속이 불허하기 때문에
//인터페이스는 상속이 아닌 구현이라 표현되고
//다중상속이 가능하게 느껴짐(우회적 다중상속을 위해 사용함)
//interface는 일반메소드를 가질 수 없음(자동적으로 추상메서드)
interface A2{
void aaa(); //default가 아니라 public으로 자동적으로 생성됨
public abstract void bbb(); //원형
}
//class면 extends interface는 implements
class B2 implements A2{
/* default생성자로 호출시 에러남 항상 public
* void aaa(){
System.out.println("aaa() 메소드");
}*/
//interface A2의 추상메소드 구현
public void aaa(){
System.out.println("aaa() 메소드");
}
public void bbb(){
System.out.println("bbb() 메소드");
}
}
public class Round02 {
public static void main(String[] args) {
//interface는 객체생성이 불가하기 때문에 클래스로 구현시켜 객체생성뒤 호출
B2 bp = new B2();
bp.aaa();
bp.bbb();
}
}aaa() 메소드
| 인터페이스 공통자료형 공통표준화 (0) | 2012.04.11 |
|---|---|
| 클래스 -> 인터페이스 형변환 (0) | 2012.04.11 |
| 인터페이스 쓰임새 (0) | 2012.04.11 |
| 인터페이스 다중 상속 (0) | 2012.04.11 |
| 인터페이스 기본 (0) | 2012.04.11 |
package com.inter;//인터페이스 사용
//외형상으로 본다면 A1.class로 저장되어서 클래스로 보임
//인터페이스
//상수 , 추상 메서드 만 사용가능
interface A1{
int W = 10;
static int X = 20;
final int Y = 30;
public static final int Z = 40; //원형
//생략한것도 상수로 인식함(쓴것은 변수여도)
//인터페이스는 생성자가 없어서 객체생성이 안됨
}
public class Round01 {
public static void main(String[] args) {
/*인터페이스는 객체 생성이 불가능
A1 a = new A1();*/
/*A1.W는 상수기 때문에 값을 변경할 수 없음
A1.W = 20;*/
System.out.println(A1.W);
System.out.println(A1.X);
System.out.println(A1.Y);
System.out.println(A1.Z);
}
}10
20
30
40
| 인터페이스 공통자료형 공통표준화 (0) | 2012.04.11 |
|---|---|
| 클래스 -> 인터페이스 형변환 (0) | 2012.04.11 |
| 인터페이스 쓰임새 (0) | 2012.04.11 |
| 인터페이스 다중 상속 (0) | 2012.04.11 |
| 인터페이스 2 (0) | 2012.04.11 |