728x90 밍글링글링399 [JAVA] ch07-21. 팩토리 패턴(Factory Pattern) public class Ex21 { //Factory Pattern // 객체를 생성하는 과정이 복잡할 경우 사용 public static void main(String[] args){ new XMLParser().parse("문서"); new HTMLParser().parse("문서"); Parsable parser = ParserManager.getParser("XML"); parser.parse("a.XML"); parser = ParserManager.getParser("HTML"); parser.parse("b.HTML"); } } interface Parsable{ void parse(String fileName); //문서의 이름을 받아오기위해서는 String타입이 적당. } class XM.. JAVA/예제 2017. 8. 22. [JAVA] ch07-20. 객체 지향 20 public class Ex20 { public static void main(String[] args){ Fighter fighter = new Fighter(); if(fighter instanceof Thing) //fighter는 Thing에 소속되어있다.. (true)이므로 출력. System.out.println("fight instanceof Thing"); if(fighter instanceof Fightable) //fighter is instance of Fightable. System.out.println("fight instanceof Fightable"); if(fighter instanceof Movable) System.out.println("fight instanceof Mo.. JAVA/예제 2017. 8. 22. [JAVA] ch07-19. 객체 지향 19 import java.util.ArrayList; /*library API 일부에 collection framework api 단순한 객체의 집합 {ArrayList - 제일 많이씀 ,Map, Set} 배열써도 되는데 arraylist 쓰는 이유 배열은 사이즈가 고정이되고 arraylist는 사이즈 자동조정 */ public class Ex19 { //개발하는 시점에는 계속 테스트 public static void main(String[] args){ Shopper shopper = new Shopper(); Tablet tablet = new Tablet(); PC pc = new PC(); Audio audio = new Audio(); shopper.buy(tablet); shopper.buy(pc.. JAVA/예제 2017. 8. 22. [JAVA] ch07-18. 객체 지향 18 public class Ex18 { public static void main(String[] args){ Yoker yoker = new Yoker(); Tablet tablet = new Tablet(); PC pc = new PC(); Audio audio = new Audio(); yoker.buy(tablet); yoker.buy(pc); yoker.buy(audio); yoker.summary(); } } class Audio extends Product{ Audio(){ super(50); } public String toString(){ return "Audio"; } } class Yoker{ int money = 1000; int bonus = 0; Product[] item = new.. JAVA/예제 2017. 8. 22. [JAVA] ch07-17. 객체 지향 17 public class Ex17 { public static void main(String[] args){ Buyer buyer = new Buyer(); Tablet tablet = new Tablet(); PC pc = new PC(); buyer.buy(tablet); buyer.buy(pc); } } class Product{ int price; int bonus; Product(int price){ this.price = price; bonus = price/10; } } class Tablet extends Product{ Tablet(){ super(100); } public String toString(){ return "Tablet"; } } class PC extends Product{.. JAVA/예제 2017. 8. 22. [JAVA] ch07-16. 객체 지향 16 public class Ex16 { public static void main(String[] args){ Super parent = new Sub3(); Sub3 child = new Sub3(); parent.method(); System.out.println(); child.method(); } } class Sub3 extends Super{ int x= 200; void method(){ System.out.println("x="+x); System.out.println("this.x="+this.x); System.out.println("super.x="+super.x); super.method(); //꼼수 } } JAVA/예제 2017. 8. 22. [JAVA] ch07-15. 객체 지향 15 public class Ex15 { public static void main(String[] args){ Super parent = new Sub2(); Sub2 child = new Sub2(); System.out.println(parent.x); System.out.println(child.x); parent.method(); child.method(); } } class Sub2 extends Super{ } JAVA/예제 2017. 8. 22. [JAVA] ch07-14. 객체 지향 14 public class Ex14 { //ex) AnimalMain.java public static void main(String[] args){ Super parent = new Sub(); //타입을 둘다 가짐, 하지만 타입의 상관없이 자식의 메소드 접근 // 잘안씀. 그런게 있구나 하고 넘어가기 //변수는 부모, 메소드는 자식 Sub child = new Sub(); System.out.println(parent.x); System.out.println(child.x); parent.method(); child.method(); } } class Super{ //부모 int x = 100; void method(){ //멤버 메소드 System.out.println("Super.method()");.. JAVA/예제 2017. 8. 22. [JAVA] ch07-13. 객체 지향 13 public class Ex13 { public static void main(String[] ags){ FireCar firecar = new FireCar(); if(firecar instanceof FireCar) System.out.println("FireCar"); if(firecar instanceof Car) System.out.println("Car"); if(firecar instanceof Object) System.out.println("Object"); } } JAVA/예제 2017. 8. 22. [JAVA] ch07-11. 객체 지향 11 public class Ex11 { public static void main(String[] args){ FireCar firecar = new FireCar(); firecar.drive(); firecar.stop(); firecar.water(); Car car = firecar; car.drive(); car.stop(); //car.water(); } } class Car{ String color; int door; void drive(){ System.out.println("dreive."); } void stop(){ System.out.println("stop."); } } class FireCar extends Car{ void water(){ System.out.println("wat.. JAVA/예제 2017. 8. 22. [JAVA] ch07-10. 싱글톤 기법 (singleton) public class Ex10 { public static void main(String[] args){ //Singleton single = new Singleton(); Singleton single = Singleton.getInstance(); System.out.println(single); single.hello(); single = Singleton.getInstance(); System.out.println(single); } } //stateful 데이터 상태값 (도메인) state 멤버변수에 저장되는 값 //stateless 형태의 객체를 디자인할때. class Singleton{ //stateless 일때만 사용 // 다른사람이 객체생성하지못하게 하기 위하여 private stati.. JAVA/예제 2017. 8. 22. [JAVA] ch07-09. 객체 지향 9 public class Ex09 { public static void main(String[] args){ Time time = new Time(23,35,30); System.out.println(time); //time.hour=1; time.setHour(time.getHour()+1); System.out.println(time); } } class Time{ private int hour; private int minute; private int second; Time(int hour, int minute, int second){ this.hour = hour; this.minute = minute; this.second = second; } public int getHour(){ if(hour.. JAVA/예제 2017. 8. 22. 이전 1 ··· 19 20 21 22 23 24 25 ··· 34 다음 728x90