728x90 JAVA126 [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. [JAVA] ch07-08. 객체 지향 8 public class Ex08 { public static void main(String[] args){ Paper paper = new Paper(10, "HEART"); //paper.NUMBER = 5; System.out.println(paper.NUMBER); System.out.println(paper.KIND); } } //public 메서드호출 //private 멤버변수 class Paper{ final int NUMBER; final String KIND; static int width = 100; static int height = 250; Paper(){ this(1,"HEART"); } Paper(int number, String kind){ this.NUMBER = number;.. JAVA/예제 2017. 8. 22. [JAVA] ch07-06. 객체 지향 6 //import 패키지.클래스 // import 패키지.*;은 가독성이 떨어짐 //import java.lang.*; 생략된 import문 컴파일러가 자동으로 추가 시켜줌 public class Ex06 { public static void main(String[] args){ Point3D point = new Point3D(); System.out.printf("(%d, %d, %d)",point.x,point.y,point.z); } } class Point3D extends Point{ //Object는 최상위클래스 int z = 30; Point3D(){ this(100,200,300); } Point3D(int x, int y, int z){ super(x,y); this.z = z; } } .. JAVA/예제 2017. 8. 22. [JAVA] ch07-05. 객체 지향 5 public class Ex05 { public static void main(String[] args){ Descendant descendant = new Descendant(); descendant.method(); } } class Descendant extends Parent{ int x = 20; void method(){ System.out.println("x= "+x); System.out.println("this.x= "+this.x); System.out.println("super.x= "+super.x); } } JAVA/예제 2017. 8. 22. [JAVA] ch07-04. 객체 지향 4 public class Ex04 { public static void main(String[] args){ Child child = new Child(); child.method(); } } class Parent{ int x =10; } class Child extends Parent{ void method(){ System.out.println("x= "+x); System.out.println("this.x= "+this.x); System.out.println("super.x= "+super.x); } } JAVA/예제 2017. 8. 22. [JAVA] ch07-03. 객체 지향 3 public class Ex03 { public static void main(String[] args){ TVCR tvcr = new TVCR(); tvcr.power(); tvcr.play(); tvcr.stop(); System.out.println(tvcr); } } class TVCR extends Tv{ VCR vcr = new VCR(); int counter = vcr.counter; void play(){vcr.play();} void stop(){vcr.stop();} void rew(){vcr.rew();} void ff(){vcr.ff();} } class Tv{ boolean power; int channel; void power(){power = !power;} void chan.. JAVA/예제 2017. 8. 22. 이전 1 2 3 4 5 6 ··· 11 다음 728x90