### : 목차 구분 기호
--- : 목차 내에 항목 구분 기호
목차
1. 이론 및 정보
2. 설정 및 그 밖에
3. 소스코드
4. 과제
###################################
1. 이론 및 정보
-----------------------------------
--- : 목차 내에 항목 구분 기호
목차
1. 이론 및 정보
2. 설정 및 그 밖에
3. 소스코드
4. 과제
###################################
1. 이론 및 정보
-----------------------------------
클래스
변수 -> 배열 -> 클래스
2. Access Modifier(접근 지정자)
3. 메서드의 인자전달 방식 (인자가 있는 방식의 파생)
4. 재귀호출(recursive call)
5. static
1) 클래스, 메서드, 변수에 사용가능
2) 인스턴스와는 별도로 만들어지는 메모리
다른 모든 인스턴스들이 공동으로 사용하는 메모리
3) 단 1개만 만들어지는 메모리
4) 클래스 변수(클래스에서 유일하게 하나 만들어져서?,반드시 클래스안에서 만들어야함!!)
5) static block
static int i;
...
static int j;
...
static int k;
static{
i = 10;
j = 20;
k = 30;
}
//static 변수를 초기화 하기 위해서
6. 중첩 클래스(내부 클래스)
1) 클래스 내부에 다른 클래스를 포함하는 것.
2) 내부 클래스
- General 클래스(비정적 클래스) - 많이 쓰임, 외부 클래스가 먼저 만들어져야함
- static 클래스(정적 클래스) - 거의 안쓰임
3) 클래스들끼리 자주 접근해야 하는 경우에 유리
4) 전용클래스로 사용할 경우에 유리
내부클래스를 파일명으로 보면 outterclass$innerclass.class로 되어 있음
//외부 클래스를 쓸일이 없으면 아래와 같이 생성가능
InnerClass ic1 = (new OutterClass()).new InnerClass();
InnerClass ic1 = (new OutterClass()).new InnerClass();
//같은 클래스가 아닌 다른 클래서에서 작업할때에는 아래와 같이 소속을
//적어주어야 선언이 됨 , 왼쪽 부분, 좌변
OutterClass.InnerClass ic2 = new OutterClass().new InnerClass();
//적어주어야 선언이 됨 , 왼쪽 부분, 좌변
OutterClass.InnerClass ic2 = new OutterClass().new InnerClass();
외부 클래스를 마치 자기것 마냥 쓸 수 있음
-----------------------------------
Design Pattern
- Singleton Pattern : 생성자, static, private
단 하나의 인스턴스만을 생성하게 해주는 패턴
쓸때 없는 인스턴스를 만들어내지 않기 위해서?
1) 인스턴스를 생성할 수 없게 만든다.
2) 자체적으로 인스턴스를 단 1개만 생성하게 한다.
3) 생성된 인스턴스를 외부에서 사용할 수 있게 한다.
-----------------------------------
상속성
1. 상속 제외
1) 생성자
2) private(상속해줘도 쓰질 못해)
2. 부모의 생성자는 호출 가능하다.
super() - 부모의 생성자 주소를 가지고 있음,전달받은 데이터를 부모클래스에서 보관하기 위해서
super.변수 or super.메서드()
부모의 생성자를 호출할 경우 반드시 가장 처음에 사용해야한다.
super를 쓰지 않으면 기본적으로 자식 클래스는 this는 생략된체 변수가 쓰임.
특히 변수명이 같으면 부모의 변수인지 자식의 변수인지 잘 설정해줘야함
생성자에서 매개변수, 자식클래스 변수, 부모클래스 변수 명이 같으면
변수명을 쓸때 매개변수가 우선순위가 제일 높음!!!!@@@@@@@@
부모클래스 , 슈퍼 클래스, 베이스 클래스 같은 의미
자식클래스, 서브 클래스, 파생 클래스
-----------------------------------
###################################
2. 설정 및 그 밖에
-----------------------------------
2. 설정 및 그 밖에
-----------------------------------
day08 만듬
-----------------------------------
call by ref, call by value
caller, callee
###################################
3. 소스코드
-----------------------------------
3. 소스코드
-----------------------------------
3-1
/day08/src/CallByTest2.java
class RefTest{ int a; RefTest(int a) { this.a = a; } } public class CallByTest2 { //swap1, swap3 메서드들은 메개변수는 Call By Reference 같은데 //내부 swap하는 과정은 Call By Value 인 듯 함 //swap, swap2 메서드는 메개변수도 Call By Value이고 //내부 swap하는 과정도 Call By Value 인 듯 함 void swap(int num1,int num2){ int temp = num1; num1 = num2; num2 = temp; System.out.println("num1 : "+num1+", num2 : "+num2); } void swap1(int[] a, int[] b){ int temp = a[0]; a[0] = b[0]; b[0] = temp; } void swap2(RefTest a, RefTest b){ RefTest temp = a; a = b; b = temp; } void swap3(RefTest a, RefTest b) { int temp = a.a; a.a = b.a; b.a = temp; } public static void main(String[] args) { // TODO 반드시 참조에 의한 전달을 사용해야 하는 경우 int num1 = 10, num2 = 5; CallByTest2 obj = new CallByTest2(); obj.swap(num1, num2); System.out.println(num1+","+num2); // 주소를 만들어내야함 어떻게 주소를 만들수 있을까? // 같은 타입? 배열? System.out.println("####################"); System.out.println("swap1"); int[]a = {10}; int[]b = {5}; System.out.println(a[0]+","+b[0]); obj.swap1(a,b); System.out.println(a[0]+","+b[0]); System.out.println("####################"); System.out.println("swap2"); RefTest b1 = new RefTest(10); RefTest b2 = new RefTest(5); System.out.println(b1.a+","+b2.a); obj.swap2(b1,b2); System.out.println(b1.a+","+b2.a); System.out.println("####################"); System.out.println("swap3"); RefTest b3 = new RefTest(10); RefTest b4 = new RefTest(5); System.out.println(b3.a+","+b4.a); obj.swap3(b3,b4); System.out.println(b3.a+","+b4.a); } } | cs |
이해가 안되던 부분 아래 그림으로 설명!!
swap2 부분!
-----------------------------------
3-5
/day08/src/OutterClass.java
/day08/src/OutterClass.java
// TODO 중첩 클래스 예제 //static 내부 /* public class OutterClass { private int out_i; void outter() { InnerClass ic = new InnerClass(); ic.in_i = 20; System.out.println(ic.in_i); } static class InnerClass { private int in_i; void inner() { OutterClass oc = new OutterClass(); oc.out_i = 10; System.out.println(oc.out_i); } } public static void main(String[] args) { InnerClass ic = new InnerClass(); ic.inner(); OutterClass oc = new OutterClass(); oc.outter(); } } */ //일반 내부 클래스는 외부 클래스에 종속적임, //그래서 외부 클래스가 먼저 만들어져야함!! public class OutterClass { private int out_i; class InnerClass { private int in_i; void inner() { //외부 클래스는 내것 마냥 쓸 수 있음 out_i = 10; System.out.println(out_i); } } public static void main(String[] args) { OutterClass oc = new OutterClass(); InnerClass ic = oc.new InnerClass(); //외부 클래스를 쓸일이 없으면 아래와 같이 생성가능 InnerClass ic1 = (new OutterClass()).new InnerClass(); //같은 클래스가 아닌 다른 클래서에서 작업할때에는 아래와 같이 소속을 //적어주어야 선언이 됨 , 왼쪽 부분, 좌변 OutterClass.InnerClass ic2 = new OutterClass().new InnerClass(); ic.inner(); ic.in_i = 20; System.out.println(ic.in_i); } } | cs |
-----------------------------------
3-6
/day08/src/SingletonTest.java
class SingletonDemo { int i; private static SingletonDemo sd = new SingletonDemo(); private SingletonDemo() { } public static SingletonDemo newInstance() { return sd; } } public class SingletonTest { public static void main(String[] args) { // TODO Singleton Pattern 연습 SingletonDemo s1 = SingletonDemo.newInstance(); SingletonDemo s2 = SingletonDemo.newInstance(); if (s1 == s2) System.out.println("주소 같음"); else System.out.println("주소 다름"); } } | cs |
-----------------------------------
3-7
/day08/src/CMS.java
public class CMS { private int no; private String name; private char level; String tel; CMS() { } CMS(int no, String name, char level, String tel) { this.no = no; this.name = name; this.level = level; this.tel = tel; } void display() { System.out.println(no + " : " + name + " : " + level + " : " + tel); } } | cs |
-----------------------------------
3-8
/day08/src/InheritanceTest.java
public class InheritanceTest { public static void main(String[] args) { // TODO 상속에 대한 첫번째 예제 CMS kim = new CMS(1, "김유진", 'C', "111-1111"); kim.display(); CMSExt lee = new CMSExt(2, "이승기", 'B', "222-2222", "서울시 서초구"); lee.print(); } } | cs |
-----------------------------------
3-9
/day08/src/CMSExt.java
public class CMSExt extends CMS { String address; String tel; CMSExt() { } CMSExt(int no, String name, char level, String tel, String address) { super(no, name, level, tel); tel = "000-0000"; // 이럴때는 매게변수 this.tel = "111-1111"; //CMS가 만든 tel super.tel = "333-3333"; //부모 tel this.address = address; tel = "333-3333"; // 이럴때는 매게변수 } void print() { display(); System.out.print(address); } } | cs |
-----------------------------------
###################################
4. 과제
-----------------------------------
없음
-----------------------------------
숫자 받아서 다이아몬드
public class Problem { public static void main(String[] args) throws java.io.IOException { int input = 0; System.out.println("input number : "); input = System.in.read() - 48; System.in.skip(2); int var = 0; // var는 input - 1 의 크기로 시작해서 라인이 변할때마다 숫자가 변함 for (int i = 0; i < input; i++) {// 라인을 처음부터 끝까지 도는 for문 for (int j = 0; j < input + (input - 1); j++) {// 한 라인을 채우는 for문 if (j < input - 1 - var) { System.out.print(" "); } else if (j > input - 1 + var) { System.out.print(" "); } else System.out.print("*"); } System.out.println(); var++; } for (int i = 0; i < input - 1; i++) {// 라인을 처음부터 끝까지 도는 for문 for (int j = 0; j < input + (input - 1); j++) {// 한 라인을 채우는 for문 if (j < input - var + 1) { System.out.print(" "); } else if (j > input + var - 3) { System.out.print(" "); } else System.out.print("*"); } System.out.println(); var--; } } } | cs |
'OpenFrameWork' 카테고리의 다른 글
오픈프레임워크_Day11 (0) | 2015.04.20 |
---|---|
오픈프레임워크_Day10 (0) | 2015.04.20 |
오픈프레임워크_Day08 (0) | 2015.04.20 |
오픈프레임워크_Day07 (0) | 2015.04.20 |
오픈프레임워크_Day06 (0) | 2015.04.20 |