OpenFrameWork
오픈프레임워크_Day10
px
2015. 4. 20. 09:58
### : 목차 구분 기호
--- : 목차 내에 항목 구분 기호
목차
1. 이론 및 정보
2. 설정 및 그 밖에
3. 소스코드
4. 과제
###################################
1. 이론 및 정보
-----------------------------------
--- : 목차 내에 항목 구분 기호
목차
1. 이론 및 정보
2. 설정 및 그 밖에
3. 소스코드
4. 과제
###################################
1. 이론 및 정보
-----------------------------------
상속성
1. 상속 제외
1) 생성자
2) private(상속해줘도 쓰질 못해)
2. 부모의 생성자는 호출 가능하다.
super()
super.변수 or super.메서드()
부모의 생성자를 호출할 경우 반드시 가장 처음에 사용해야한다.
자식의 생성자는 부모의 생성자가 기본적으로 들어가 있다.
super();가 코딩되어 있지 않아도 실행됨
3. 생성자의 호출 순서
4. 상속은 확장 개념이다.
5. 자바에서는 단일 상속만 지원을 한다.
상속 설계 :
1) 중복기능을 가지지 않게 한다.
2) 부모클래스는 반드시 최소의 기능을 갖고 있다
6. 부모클래스와 자식클래스의 참조관계
1) 인스턴스가 결정되지 않았을 경우
A, B extend A, C extend A, D extend A
B b1 = new B();
C c1 = // 실행중에 무엇으로 생성할지 모를 수가 있음 B,C,D 인지 어떤 건지
A a1 = // 모든 자식의 주소를 받을 수 있는 부모의 클래스로 받으면 됨
... 동적 처리가 적당히 끝났을때, 적당한 타이밍에 주소를 돌려 줌
B b2 = (B)a1;
2) 여러 개의 자식 인스턴스를 관리
A a[] = new A[5];
for(int i=0;i<5;i++){
a[i] = new A();
}
// 위와 같은 방법으로 할 것인가, 아래와 같은 방법으로 할 것인가?
// 하나의 예시임
a[0] = new B();
a[1] = new C();
a[2] = new D();
a[3] = new B();
a[4] = new D();
- 서로 다른 클래스들은 절대로 참조할 수 없다.
- 부모자식 관계는 참조가 가능하다.
- 단, 부모만 자식의 주소를 참조 할 수 있다.
- 단, 부모는 자식에게 물려준 것만 접근 가능 (이렇게 해도 충분히 해결 되는 경우가 있음)
7. 메서드의 오버라이딩(Overriding)
1) 오버로딩
- 메서드
- 다형성
- 중복 정의
- everywhere
2) 오버라이딩
- 메서드
- 다형성
- 재정의 - 부모로 부터 물려받은 메서드를 수정하는 것
- 반드시 상속에서만 사용가능
8. 추상 클래스, 추상 메서드(abstract)
1) 추상 메서드
- 내용 없이 선언만 되어 있는 메서드
- 반드시 오버라이딩을 해야한다.
- 반드시 메서드명 앞에 abstract 키워드를 사용
2) 추상 클래스
- 추상 메서드를 한개 이상 보유
- 반드시 클래스 앞에 abstract 키워드
- 절대로 인스턴스를 생성할 수 없다
9. final - 실수를 미리 방지해 주는 안전 장치임, 의도치 않게 값의 변경을 막겠다는 의미
1) 변수 - 값을 보호할 목적, 데이터의 의미를 명확하게 위한 목적
final은 전부 대문자로 작성하는것이 관례임
final int MAX = 10;
MAX = 100; (X) -> 상수가 됨. constant
if( x < MAX )...
2) 메서드
- Overriding 금지! 물려 받은거 그대로 쓰라!
final void method(){
...
}
3) 클래스
- 더 이상 자식 클래스를 만들 수 없다.
- 상속관계에서 더 이상 확장이 불가능하다.
final class A{
...
}
10. object
모든 클래스의 조상 object
-----------------------------------
Interface
1. 클래스를 만들기 위한 설계도, 더 잘 만들기 위한 것
2. 완전 추상 클래스(c,c++ => 순수 추상 클래스) - 말만 인터페이스지 문법적인 부분은 똑같음
- 100 퍼센트 추상 메서드 만 가짐
3. 표준화를 위한 약속, 규칙 - 일관석을 가지고 일을 하겠다는 의미
4. 의존성(dependency)를 최소화
5. 다중 상속을 지원
- interface는 껍데기만 주니까 implement라는 키워드로 구현해서 사용
- 변수는 모두 public static final 변수임
- 메서드는 모두 public final 메서드
-----------------------------------
-----------------------------------
-----------------------------------
2. 설정 및 그 밖에
-----------------------------------
-----------------------------------
-----------------------------------
-----------------------------------
###################################
3. 소스코드
-----------------------------------
3-1
/day09/src/ConsCallOrder.java
class A { A() { System.out.println("A 생성자 호출"); } } class B extends A{ B(){ } B(int i) { //super();//생략되어 있음 System.out.println("B 생성자 호출"); } } class C extends B{ C() { //super();//생략되어 있음 super(10); System.out.println("C 생성자 호출"); } } public class ConsCallOrder { public static void main(String[] args) { // TODO 생성자 호출 순서 new C(); } } | cs |
-----------------------------------
3-2
/day09/src/RefTest.java
class First { int a = 10; void display() { System.out.println("a : " + a); } } class Second extends First { int b = 100; /* * void print(){ System.out.println("b : "+ b); } */ void display() { System.out.println("b : " + b); } } public class RefTest { public static void main(String[] args) { // TODO 부모클래스와 자식클래스의 참조관계 First f1 = new First(); f1.display(); First f2 = f1; f2.display(); Second s1 = new Second(); // s1.print(); s1.display(); // f2 = s1; //서로 다른 클래스는 절대로 참조 할 수 없다. f2 = s1; f2.display(); // Second s2 = (Second)f1; // f2.print(); Second s2 = (Second) f2; // s2.print(); // ///////////////// s2.display(); } } | cs |
-----------------------------------
3-3
/day09/src/OverrideTest.java
abstract class TwoDShape { private double width; private double height; private String name; public TwoDShape() { } public TwoDShape(double w, double h, String n) { width = w; height = h; name = n; } public abstract double getArea(); public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public String getName() { return name; } public void setName(String name) { this.name = name; } } class Triangle extends TwoDShape { Triangle(double w, double h, String n) { super(w, h, n); } public double getArea() { return getWidth() * getHeight() / 2; } } class Rectangle extends TwoDShape { Rectangle(double w, double h, String n) { super(w, h, n); } public double getArea() { return getWidth() * getHeight(); } } public class OverrideTest { public static void main(String[] args) { // TODO Override 테스트 //TwoDShape t1 = new TwoDShape(8.0, 7.5, "Jeneral"); Triangle tr1 = new Triangle(9.5, 9.5, "정삼각형"); Rectangle r1 = new Rectangle(8.0, 8.0, "정사각형"); Triangle tr2 = new Triangle(9.2, 7.5, "이등변삼각형"); Rectangle r2 = new Rectangle(4.0, 5.5, "직사각형"); //TwoDShape t2[] = { t1, tr1, tr2, r1, r2 }; TwoDShape t2[] = { tr1, tr2, r1, r2 }; for (int i = 0; i < t2.length; i++) { System.out.println(t2[i].getName() + " : "+ t2[i].getArea()); } } } | cs |
-----------------------------------
3-4
/day09/src/InterfaceTest.java
interface inter1{ int a = 10; } interface inter2 extends inter1{ int b = 20; } interface inter3 extends inter1, inter2{ int c = 30; void intertest(); } //public class InterfaceTest implements inter1,inter2,inter3{ public class InterfaceTest implements inter3{ public void intertest() { } public static void main(String[] args) { // TODO 인터페이스 예제1 //System.out.println(inter1.a); System.out.println(a);//implements inter1 라서 가능 //System.out.println(inter2.b); System.out.println(b);//implements inter2 라서 가능 //System.out.println(inter3.c); System.out.println(c);//implements inter3 라서 가능 //final, static 속성 //inter1.a = 100; } } | cs |
-----------------------------------
3-5
/day09/src/InterfaceHomework.java
interface Volume { void volumeUp(int size); void volumeDown(int size); } interface Power { void powerOn(); void powerOff(); } class Radio implements Volume, Power{ private int vol; @Override public void powerOn() { // TODO Auto-generated method stub System.out.println("파워 ON"); } @Override public void powerOff() { // TODO Auto-generated method stub System.out.println("파워 OFF"); } @Override public void volumeUp(int size) { // TODO Auto-generated method stub vol += size; System.out.println("라디오 소리가 " + size + " 만큼 증가되었습니다."); System.out.println("현재 라디오 소리 : "+ vol); } @Override public void volumeDown(int size) { // TODO Auto-generated method stub vol -= size; System.out.println("라디오 소리가 " + size + " 만큼 감되었습니다."); System.out.println("현재 라디오 소리 : "+ vol); } } class Tv implements Volume, Power{ private int vol; @Override public void powerOn() { // TODO Auto-generated method stub System.out.println("파워 ON"); } @Override public void powerOff() { // TODO Auto-generated method stub System.out.println("파워 OFF"); } @Override public void volumeUp(int size) { // TODO Auto-generated method stub vol += size; System.out.println("Tv 소리가 " + size + " 만큼 증가되었습니다."); System.out.println("현재 Tv 소리 : "+ vol); } @Override public void volumeDown(int size) { // TODO Auto-generated method stub vol -= size; System.out.println("Tv 소리가 " + size + " 만큼 감되었습니다."); System.out.println("현재 Tv 소리 : "+ vol); } } class Speaker implements Volume, Power{ private int vol; @Override public void powerOn() { // TODO Auto-generated method stub System.out.println("파워 ON"); } @Override public void powerOff() { // TODO Auto-generated method stub System.out.println("파워 OFF"); } @Override public void volumeUp(int size) { // TODO Auto-generated method stub vol += size; System.out.println("Speaker 소리가 " + size + " 만큼 증가되었습니다."); System.out.println("현재 Speaker 소리 : "+ vol); } @Override public void volumeDown(int size) { // TODO Auto-generated method stub vol -= size; System.out.println("Speaker 소리가 " + size + " 만큼 감되었습니다."); System.out.println("현재 Speaker 소리 : "+ vol); } } public class InterfaceHomework { public static void main(String[] args) { // TODO 인터페이스 과제 // 자유과제 /* * 1. 소리를 키울때 100 이상 넘지 못하게 한다. * 2. 소리를 죽일때 0 미만으로 줄일 수 없도록 한다. * 0은 mute 기능으로 더 좋음 * 3. 소리를 줄이거나 키울때 전원이 켜있을때만 가능하게 한다. * Tv 전원을 켜시겠습니까?(0 or 1로 전원 키거나 끄기) * 1. 소리증가 2. 소리감소 * 입력 : 1 * 값 : 10 * Tv 소리가 10 만큼 증가되었습니다. * 현재 Tv 소리 : 20 * 라디오와 티비와 스피거가 제대로 동작할 수 있겠끔 만들어 보라 */ } } | cs |
-----------------------------------
-----------------------------------
-----------------------------------
-----------------------------------
###################################
4. 과제
-----------------------------------
4. 과제
-----------------------------------
1. 주제를 정해서 상속설계를 하라
속성 - 인스턴스 변수
기능 - 메서드
3개의 level 정도로 상속 될 수 있게
만들어 보라
이거 가지고 프로그램을 짜게 될 것임
-----------------------------------
2.
2.
/day09/src/InterfaceHomework.java
주석 내용 해결하기
interface Volume { void volumeUp(int size); void volumeDown(int size); int getVolume(); } interface Power { void powerOn(); void powerOff(); boolean isOn(); } class Radio implements Volume, Power { public static final int RADIONUM = 2; private int vol = 0; private boolean flag = false; public void powerOn() { flag = true; System.out.println("라디오 파워 ON"); } public void powerOff() { flag = false; System.out.println("라디오 파워 OFF"); } public void volumeUp(int size) { if( vol + size > 100 ){ size = 100 - vol; vol = 100; }else vol += size; System.out.println("라디오 소리가 " + size + " 만큼 증가되었습니다."); System.out.println("현재 라디오 소리 : " + vol); } public void volumeDown(int size) { if (vol == 0) size = 0; if( vol - size < 0 ){ size = vol; vol = 0; }else vol -= size; System.out.println("라디오 소리가 " + size + " 만큼 감소되었습니다."); System.out.println("현재 라디오 소리 : " + vol); if(vol == 0) System.out.println("라디오가 음소거 되었습니다."); } public boolean isOn() { return flag; } public int getVolume() { return vol; } } class Tv implements Volume, Power { public static final int TVNUM = 1; private int vol = 0; private boolean flag = false;; public void powerOn() { flag = true; System.out.println("Tv 파워 ON"); } public void powerOff() { flag = false; System.out.println("Tv 파워 OFF"); } public void volumeUp(int size) { if( vol + size > 100 ){ size = 100 - vol; vol = 100; }else vol += size; System.out.println("Tv 소리가 " + size + " 만큼 증가되었습니다."); System.out.println("현재 라디오 소리 : " + vol); } public void volumeDown(int size) { if (vol == 0) size = 0; if( vol - size < 0 ){ size = vol; vol = 0; }else vol -= size; System.out.println("Tv 소리가 " + size + " 만큼 감소되었습니다."); System.out.println("현재 Tv 소리 : " + vol); if(vol == 0) System.out.println("Tv가 음소거 되었습니다."); } public boolean isOn() { return flag; } public int getVolume() { return vol; } } class Speaker implements Volume, Power { public static final int SPEAKERNUM = 3; private int vol = 0; private boolean flag = false; public void powerOn() { flag = true; System.out.println("스피커 파워 ON"); } public void powerOff() { flag = false; System.out.println("스피커 파워 OFF"); } public void volumeUp(int size) { if( vol + size > 100 ){ size = 100 - vol; vol = 100; }else vol += size; System.out.println("Speaker 소리가 " + size + " 만큼 증가되었습니다."); System.out.println("현재 Speaker 소리 : " + vol); } public void volumeDown(int size) { if (vol == 0) size = 0; if( vol - size < 0 ){ size = vol; vol = 0; }else vol -= size; System.out.println("Speaker 소리가 " + size + " 만큼 감소되었습니다."); System.out.println("현재 Speaker 소리 : " + vol); if(vol == 0) System.out.println("Speaker가 음소거 되었습니다."); } public boolean isOn() { return flag; } public int getVolume() { return vol; } } public class InterfaceHomework { static Tv tv = new Tv(); static Radio radio = new Radio(); static Speaker speaker = new Speaker(); public static void main(String[] args) { int input_onoff; int input_app; int input_volue; boolean isOnOff; String selected; final int ON = 1; final int OFF = 2; final int EXIT = 0; for(;;){ isOnOff = false; input_onoff = 0; input_app = 0; input_volue = 0; selected = ""; java.util.Scanner scan = new java.util.Scanner(System.in); for(;;){ System.out.println("1. Tv"); System.out.println("2. Radio"); System.out.println("3. Speaker"); System.out.println("세개의 가전제품 중 하나를 선택 하시오(정수 0 ~ 3, 0은 exit) : "); input_app = scan.nextInt(); if( input_app == tv.TVNUM || input_app == radio.RADIONUM || input_app == speaker.SPEAKERNUM) break; else if (input_app == EXIT ) return; else System.out.println("다시 입력하세요. 0 ~ 3을 입력하세요."); } if (input_app == tv.TVNUM) { selected = "Tv"; isOnOff = tv.isOn(); } else if (input_app == radio.RADIONUM) { selected = "Radio"; isOnOff = radio.isOn(); } else if (input_app == speaker.SPEAKERNUM) { selected = "Speaker"; isOnOff = speaker.isOn(); } if(isOnOff) System.out.println(selected + "는 현재 켜져 있습니다."); else System.out.println(selected + "는 현재 꺼져 있습니다."); System.out.println(selected + "전원을 켜거나 끄시겠습니까?(1 켜기, 2 끄기, 0 현재 상태로 진행 ) "); for(;;){ input_onoff = scan.nextInt(); if( input_onoff == EXIT || input_onoff == ON || input_onoff == OFF) break; else System.out.println("다시 입력하세요. 0 ~ 2를 입력하세요."); } if (input_app == tv.TVNUM) { if (input_onoff == ON) tv.powerOn(); else if (input_onoff == OFF) tv.powerOff(); } else if (input_app == radio.RADIONUM) { if (input_onoff == ON) radio.powerOn(); else if (input_onoff == OFF) radio.powerOff(); } else if (input_app == speaker.SPEAKERNUM) { if (input_onoff == ON) speaker.powerOn(); else if (input_onoff == OFF) speaker.powerOff(); } if (input_app == tv.TVNUM) { if (tv.isOn()) SoundDisplay(tv.TVNUM); } else if (input_app == radio.RADIONUM) { if (radio.isOn()) SoundDisplay(radio.RADIONUM); } else if (input_app == speaker.SPEAKERNUM) { if (speaker.isOn()) SoundDisplay(speaker.SPEAKERNUM); } } } static void SoundDisplay(int app) { boolean isUp = false; int input = 0; int value = 0; java.util.Scanner scan = new java.util.Scanner(System.in); if (app == tv.TVNUM) { System.out.println("현재 volume : "+tv.getVolume()); } else if (app == radio.RADIONUM) { System.out.println("현재 volume : "+radio.getVolume()); } else if (app == speaker.SPEAKERNUM) { System.out.println("현재 volume : "+speaker.getVolume()); } System.out.println("1. 소리증가 2. 소리감소"); for(;;){ System.out.println("입력 :"); input = scan.nextInt(); if(input == 1 || input == 2 ) break; else System.out.println("다시 입력하세요. 1 or 2를 입력하세요."); } if (input == 1) isUp = true; else if (input == 2) isUp = false; for(;;){ System.out.println("값 :"); value = scan.nextInt(); if( (value > 100) || (value < 0) ){ System.out.println("다시 입력하세요. 0 ~ 100을 입력하세요."); } else { break; } } if (app == tv.TVNUM) { if (tv.isOn()) { if(isUp) tv.volumeUp(value); else tv.volumeDown(value); } } else if (app == radio.RADIONUM) { if (radio.isOn()) { if(isUp) radio.volumeUp(value); else radio.volumeDown(value); } } else if (app == speaker.SPEAKERNUM) { if (speaker.isOn()) { if(isUp) speaker.volumeUp(value); else speaker.volumeDown(value); } } } } |
-----------------------------------
-----------------------------------
-----------------------------------
###################################
5. 과제 해결
-----------------------------------
5. 과제 해결
-----------------------------------
-----------------------------------
-----------------------------------
-----------------------------------
-----------------------------------