자바 프로 그래 밍 사상 - 제5 장 초기 화 및 정리

22426 단어 JavaSE학습 총화
컴퓨터 가 발전 함 에 따라 '안전 하지 않다' 는 프로 그래 밍 방식 은 이미 프로 그래 밍 대가 가 높 은 주요 원인 중의 하나 가 되 었 다.1. 구조 기 를 사용 하여 초기 화 를 확보 합 니 다.대상 을 만 드 는 방법 을 지정 하기 위해 파 라 메 터 를 가 진 구조 기도 있다.구조 기 는 대상 이 생 성 될 때 초기 화 되 고 자바 는 사용자 가 대상 을 조작 할 능력 이 있 기 전에 해당 하 는 구조 기 를 자동 으로 호출 합 니 다.『 8194 』 구조 기 명명 규칙: 1) 구조 기 는 클래스 와 같은 이름 을 사용한다.2) '방법 마다 이니셜 소문 자' 의 프로 그래 밍 스타일 은 구조 기 에 적용 되 지 않 는 다.    주의 점: 자바 에서 '초기 화' 와 '생 성' 이 한데 묶 여 있어 분리 할 수 없습니다.2. 방법 은 구조 기 를 다시 싣 는 것 도 방법 이 므 로 구조 기 를 다시 싣 는 것 (파 라 메 트릭 구조 기와 파 라 메 트릭 구조 기 가 있 음) 을 할 수 있 고 구조 기 를 다시 싣 는 것 은 생 성 대상 의 서로 다른 방식 을 확보 할 수 있다.물론 클래스 의 방법 에 대해 서도 무 거 운 짐 을 실 을 수 있다.이것 은 가장 기본 적 인 것 이다.『 8194 』 2.1 재 부팅 방법 을 구분 합 니 다. 『 8194 』 1) 재 부팅 방법 마다 유일한 매개 변수 유형 목록 이 있어 야 합 니 다.    2) 매개 변수 유형 은 같 지만 순서 가 다 르 면 서로 다른 방법 을 구분 할 수 있 지만 일반적으로 이렇게 하지 마 세 요. 코드 를 유지 하기 어렵 기 때 문 입 니 다.    2.2 기본 유형 과 관련 된 재 부팅     1) 들 어 오 는 데이터 형식 (실제 매개 변수 유형) 이 방법 에서 설명 한 형식 매개 변수 유형 보다 작 으 면 실제 데이터 형식 이 제 시 됩 니 다.char 형식 이 조금 다 르 기 때문에 적당 한 char 매개 변 수 를 찾 을 수 없 으 면 char 를 int 로 직접 향상 시 킵 니 다.『 8194 』 2) 들 어 오 는 실제 매개 변수 가 과부하 방법 에서 밝 힌 매개 변수 보다 크 면 유형 전환 을 통 해 협소 화 전환 을 수행 합 니 다.『 8194 』 2.3 반환 값 으로 과부하 방법 을 구분 합 니 다. 이런 방법 은 통 하지 않 으 니 사용 하지 않 는 것 이 좋 습 니 다.3. 기본 구조 기 는 8194 ° 8194 ° 1) 기본 구조 기의 역할 은 '기본 대상' 을 만 드 는 것 입 니 다.클래스 에 구조 기 가 없 으 면 컴 파일 러 가 자동 으로 구조 기 를 만들어 줍 니 다.    2) 구조 기 가 정의 되 어 있 으 면 컴 파일 러 는 기본 구조 기 를 자동 으로 만 드 는 것 을 도와 주지 않 습 니 다.4. this 키워드   해결 문제:     1) 방법 내부 에서 현재 대상 에 대한 인용 을 얻는다.방법 내부 에서 같은 종류의 다른 방법 을 호출 하면 this 를 사용 하지 않 고 직접 호출 하면 된다.현재 방법 중의 this 인용 은 같은 종류의 다른 방법 으로 자동 으로 인 용 됩 니 다.프로그램 예:
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.
*///:~

좋은 웹페이지 즐겨찾기