728x90 JAVA/예제115 [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. [JAVA] ch07-02. 객체 지향 2 public class Ex02 { public static void main(String[] args){ Deck deck = new Deck(); Card card = deck.pick(0); System.out.println(card); deck.shuffle(); card = deck.pick(0); System.out.println(card); } } class Card{ static final int KIND_MAX = 4; //final을 변수에 쓰게되면 상수화. static final int NUM_MAX = 13; static final int SPADE = 4; static final int DIAMOND = 3; static final int HEART = 2; static final.. JAVA/예제 2017. 8. 22. [JAVA] ch07-01. 객체 지향 public class Ex01{// 모든클래스는 상속되어있다. public class Ex01 extends Object 생략됨 public static void main(String[] args){ Ex01 e = new Ex01(); e.paint(); } void paint(){ Point[] p = {new Point(100,100), new Point(140,50), new Point(200,100)}; Triangle t = new Triangle(p); Triangle tp = new Triangle(new Point(100,100), new Point(140,50), new Point(200,100)); Circle c = new Circle(new Point(150,150), 50); .. JAVA/예제 2017. 8. 22. [JAVA] ch06-10. 배열 (Array) 10 public class Ex10 { public static void main(String[] args){ char[] abc = {'A','B','C','D'}; char[] number = {'0','1','2','3','4','5','6','7','8','9'}; System.out.println(new String(abc)); System.out.println(new String(number)); char[] result = new char[abc.length+number.length]; System.arraycopy(abc, 0, result, 0, abc.length);//abc의 0번째부터 abc length만큼 result 0번에다가 붙여넣기 System.arraycopy(number, 0.. JAVA/예제 2017. 8. 22. [JAVA] ch06-09. 배열 (Array) 9 public class Ex09 { public static void main(String[] args){ int[] source = {1,2,3,4,5}; int[] target = new int[10]; for(int i=0; i JAVA/예제 2017. 8. 22. [JAVA] ch06-08. 배열 (Array) 8 public class Ex08 { //다차원 배열 아파트를 연상하라.성적표 public static void main(String[] args){ int[][] score = { // 2차원 배열 {100, 100, 100}, {100, 20, 20}, {30, 30, 30}, {40, 40, 40}, {50, 50, 50}, }; int korTot=0, engTot=0, mathTot=0; System.out.println("num kor eng math total avg"); System.out.println("=========================="); for(int i=0; i JAVA/예제 2017. 8. 22. 이전 1 2 3 4 5 6 ··· 10 다음 728x90