자바 프로 그래 밍 사상 - 제5 장 초기 화 및 정리
public class Leaf {
int i = 0;
Leaf increment() {
i++;
return this;
}
void print() {
System.out.println("i = " + i);
}
public static void main(String[] args) {
Leaf x = new Leaf();
x.increment().increment().increment().print();
}
} /* Output:
i = 3
*///:~
2) 현재 대상 을 다른 방법 으로 전달한다.프로그램 예:
class Person {
public void eat(Apple apple) {
Apple peeled = apple.getPeeled();
System.out.println("Yummy");
}
}
class Peeler {
static Apple peel(Apple apple) {
// ... remove peel
return apple; // Peeled
}
}
class Apple {
Apple getPeeled() { return Peeler.peel(this); }
}
public class PassingThis {
public static void main(String[] args) {
new Person().eat(new Apple());
}
} /* Output:
Yummy
*///:~
『 8194 』 3) 구조 기 에서 다른 구조 기 를 호출 합 니 다. 주의: this 자체 가 현재 대상 에 대한 인용 을 표시 합 니 다.구조 기 에서 this 에 매개 변수 목록 을 추가 하면 이 매개 변수 목록 에 맞 는 구조 기 에 대한 명확 한 호출 이 발생 합 니 다.이렇게 해서 다른 구조 기 를 호출 하 는 데 직접적인 경로 가 생 겼 다.프로그램 예:
public class Flower {
int petalCount = 0;
String s = "initial value";
Flower(int petals) {
petalCount = petals;
System.out.println("Constructor w/ int arg only, petalCount= "
+ petalCount);
}
Flower(String ss) {
System.out.println("Constructor w/ String arg only, s = " + ss);
s = ss;
}
Flower(String s, int petals) {
this(petals);
//! this(s); // Can't call two!
this.s = s; // Another use of "this"
System.out.println("String & int args");
}
Flower() {
this("hi", 47);
System.out.println("default constructor (no args)");
}
void PetalCount() {
//! this(11); // Not inside non-constructor!
System.out.println("petalCount = " + petalCount + " s = "+ s);
}
public static void main(String[] args) {
Flower x = new Flower();
x.PetalCount();
}
} /* Output:
Constructor w/ int arg only, petalCount= 47
String & int args
default constructor (no args)
petalCount = 47 s = hi
*///:~
구조 기 Flower (String s, int petals) 는 this 로 구조 기 를 호출 할 수 있 지만 두 개 를 호출 할 수 없다 고 밝 혔 다. 또한 구조 기 를 시작 부분 까지 선택해 야 한다. 그렇지 않 으 면 컴 파일 이 잘못 될 수 있다. 4) 구조 방법 을 통 해 외부 에서 들 어 오 는 매개 변 수 를 클래스 의 구성원 변수 에 할당 하려 면 구조 방법의 형식 매개 변수 이름 은 클래스 의 구성원 변수 이름과 같다.
class Person
...{
String name;
public Person(String name)
...{
this.name = name;
}
}
5. 청소: 종료 처리 와 쓰레기 수 거 일반 상황 에서 자바 의 쓰레기 수 거 기 는 무용 대상 이 차지 하 는 메모리 공간 을 회수 하 는 것 을 책임 집 니 다. 그러나 특수 한 경우 도 있 습 니 다. 대상 을 가정 합 니 다 (new 로 만 든 것 이 아 닙 니 다)특수 한 메모리 영역 을 가 져 옵 니 다. 쓰레기 수 거 기 는 new 에서 분 배 된 메모리 만 방출 할 수 있 기 때 문 입 니 다. 이러한 상황 에 대비 하여 자바 는 클래스 에서 finalize () 라 는 방법 을 정의 할 수 있 습 니 다. 작업 원 리 는 "가정" 입 니 다.이 렇 습 니 다. 쓰레기 수 거 기 는 방출 대상 이 차지 하 는 저장 공간 을 준비 하면 먼저 이 방법 을 사용 하고 다음 쓰레기 수 거 동작 이 발생 할 때 만 진정 으로 회수 대상 이 차지 하 는 메모리 입 니 다. 따라서 이 방법 을 사용 하려 면 쓰레기 수 거 시간 에 특별한 청 소 를 할 수 있 습 니 다.C + + 의 석조 함수 가 아 닙 니 다.의 함수 용도: 1) 대상 을 만 드 는 방식 이외 의 방식 으로 대상 에 게 메모 리 를 할당 합 니 다. 예 를 들 어 로 컬 방법 (자바 에서 비 자바 코드 를 사용 하 는 방식).
class Book {
boolean checkedOut = false;
Book(boolean checkOut) {
checkedOut = checkOut;
}
void checkIn() {
checkedOut = false;
}
protected void finalize() {
if(checkedOut)
System.out.println("Error: checked out");
// Normally, you'll also do this:
// super.finalize(); // Call the base-class version
}
}
public class TerminationCondition {
public static void main(String[] args) {
Book novel = new Book(true);
// Proper cleanup:
novel.checkIn();
// Drop the reference, forget to clean up:
new Book(true);
// Force garbage collection & finalization:
System.gc();//
}
} /* Output:
Error: checked out
*///:~
본 사례 의 종결 조건 은 모든 북 대상 이 쓰레기 로 회수 되 기 전에 check in 에 서명 해 야 하 는데 main () 에서 한 권 의 책 이 서명 되 지 않 았 고 finalize () 를 통 해종료 조건 을 검증 하고 결함 을 발견 합 니 다. 6. 멤버 가 기본 형식 정 의 를 초기 화 하 는 동시에 초기 화 합 니 다. 초기 화 하지 않 으 면 자바 규칙 에 따라 기본 으로 초기 화 합 니 다. 기본 형식 이 아 닌 대상 도 같은 방식 으로 초기 화 할 수 있 습 니 다.
public class InitialValues2 {
boolean bool = true;
char ch = 'x';
byte b = 47;
short s = 0xff;
int i = 999;
long lng = 1;
float f = 3.14f;
double d = 3.14159;
} ///:~
//
class Depth {}
public class Measurement {
Depth d = new Depth();
// ...
} ///:~
2) 어떤 방법 으로 초기 값 을 제공 합 니 다. 매개 변수 가 있 는 방법 으로 초기 값 을 초기 화 할 수도 있 지만, 이 매개 변 수 는 초기 화 되 어야 합 니 다. 프로그램 예:
1.
public class MethodInit {
int i = f();
int f() { return 11; }
} ///:~
2.
public class MethodInit2 {
int i = f();
int j = g(i);
int f() { return 11; }
int g(int n) { return n * 10; }
} ///:~
7. 구조 기 초기 화: 자동 초기 화 를 막 을 수 없습니다. 그 는 구조 기 가 호출 되 기 전에 발생 합 니 다.호출 되 기 전에 초기 화 되 었 습 니 다.
class Window {
Window(int marker) { System.out.println("Window(" + marker + ")"); }
}
class House {
Window w1 = new Window(1); // Before constructor
House() {
// Show that we're in the constructor:
System.out.println("House()");
w3 = new Window(33); // Reinitialize w3
}
Window w2 = new Window(2); // After constructor
void f() { System.out.println("f()"); }
Window w3 = new Window(3); // At end
}
public class OrderOfInitialization {
public static void main(String[] args) {
House h = new House();
h.f(); // Shows that construction is done
}
} /* Output:
Window(1)
Window(2)
Window(3)
House()
Window(33)
f()
*///:~
2. 정적 데이터 의 초기 화: 초기 화 순 서 는 정적 대상 을 초기 화한 다음 비정 적 대상 을 초기 화 합 니 다. 정적 대상 은 전체 과정 에서 한 번 만 초기 화 합 니 다. 프로그램 예:
class Bowl {
Bowl(int marker) {
System.out.println("Bowl(" + marker + ")");
}
void f1(int marker) {
System.out.println("f1(" + marker + ")");
}
}
class Table {
static Bowl bowl1 = new Bowl(1);
Table() {
System.out.println("Table()");
bowl2.f1(1);
}
void f2(int marker) {
System.out.println("f2(" + marker + ")");
}
static Bowl bowl2 = new Bowl(2);
}
class Cupboard {
Bowl bowl3 = new Bowl(3);
static Bowl bowl4 = new Bowl(4);
Cupboard() {
System.out.println("Cupboard()");
bowl4.f1(2);
}
void f3(int marker) {
System.out.println("f3(" + marker + ")");
}
static Bowl bowl5 = new Bowl(5);
}
public class StaticInitialization {
public static void main(String[] args) {
System.out.println("Creating new Cupboard() in main");
new Cupboard();
System.out.println("Creating new Cupboard() in main");
new Cupboard();
table.f2(1);
cupboard.f3(1);
}
static Table table = new Table();
static Cupboard cupboard = new Cupboard();
} /* Output:
Bowl(1)
Bowl(2)
Table()
f1(1)
Bowl(4)
Bowl(5)
Bowl(3)
Cupboard()
f1(2)
Creating new Cupboard() in main
Bowl(3)
Cupboard()
f1(2)
Creating new Cupboard() in main
Bowl(3)
Cupboard()
f1(2)
f2(1)
f3(1)
*///:~
3. 표 시 된 정적 초기 화 프로그램 예:
class Cups {
static Cup cup1;
static Cup cup2;
static {
cup1 = new Cup(1);
cup2 = new Cup(2);
}
4. 비정 상 인 스 턴 스 초기 화 프로그램 예:
public class Mugs {
Mug mug1;
Mug mug2;
{
mug1 = new Mug(1);
mug2 = new Mug(2);
System.out.println("mug1 & mug2 initialized");
}
8. 배열 초기 화 는 다음 과 같 습 니 다. 식별 자 하나 로 포 장 된 대상 시퀀스 나 기본 데이터 시퀀스 를 초기 화 할 수 있 습 니 다. 이렇게 초기 화 할 수 있 습 니 다. < 8194 > int [] a = {1, 2, 3, 4, 5}; [8194, 8194, int int int [] a = {1, 2, 3, 4, 5}; [] a = new int [] a = new int [rand. nextIntInt (20)]; 8194, Integer [20] a = {new Integer (1), new Integer [2), new Integer [2], new Integer [2] [2], new Integer [2] [2], new int int [a = new int. nextint [ran. nextIntint (20)]], [20], [8194,], 3,}; 8194, 8194, 가 변 매개 변수 목록: 매개 변수 개수 와 유형 이 알 수 없 는 상황 을 처리 하 는 데 사 용 됩 니 다.
public class NewVarArgs {
static void printArray(Object... args) {
for(Object obj : args)
System.out.print(obj + " ");
System.out.println();
}
public static void main(String[] args) {
// Can take individual elements:
printArray(new Integer(47), new Float(3.14),
new Double(11.11));
printArray(47, 3.14F, 11.11);
printArray("one", "two", "three");
printArray(new A(), new A(), new A());
// Or an array:
printArray((Object[])new Integer[]{ 1, 2, 3, 4 });
printArray(); // Empty list is OK
}
} /* Output: (75% match)
47 3.14 11.11
47 3.14 11.11
one two three
A@1bab50a A@c3c749 A@150bd4d
1 2 3 4
*///:~
예 (enum): switch 구문 에 특히 적합 합 니 다.
public class Burrito {
Spiciness degree;
public Burrito(Spiciness degree) { this.degree = degree;}
public void describe() {
System.out.print("This burrito is ");
switch(degree) {
case NOT: System.out.println("not spicy at all.");
break;
case MILD:
case MEDIUM: System.out.println("a little hot.");
break;
case HOT:
case FLAMING:
default: System.out.println("maybe too hot.");
}
}
public static void main(String[] args) {
Burrito
plain = new Burrito(Spiciness.NOT),
greenChile = new Burrito(Spiciness.MEDIUM),
jalapeno = new Burrito(Spiciness.HOT);
plain.describe();
greenChile.describe();
jalapeno.describe();
}
} /* Output:
This burrito is not spicy at all.
This burrito is a little hot.
This burrito is maybe too hot.
*///:~
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.