짱짱해커가 되고 싶은 나

Ch.4 Objects and classes (2) 본문

programming/java

Ch.4 Objects and classes (2)

동로시 2019. 10. 9. 01:36

4.6 Object Construction

Overloading : many methods have the same name but different parameters. (return type은 상관 없음)

the signature of the method : parameter type을 포함한 함수의 이름.

 

Overloading solution : argument type을 보고 적절한 version 선택.

 

data field를 explict하게 초기화 하는 방법

1. declaration을 통해 value 할당

2. initialization box를 통해 value setting

- class declaration에는 arbirary code block이 있는데 이들은 class가 construct 될 때 실행된다.

- 가장 먼저 실행되고 constructor가 실행된다. (before the body of constructor)

3. constructor를 통해 value setting

 

§ Note 1: static initialization occurs when the class is first loaded.

§ Note 2: Like instance fields, static fields will be 0,false, or null unless

the programmer explicitly set them to another values.

 

explict하게 초기화 하지 않을 경우 compiler가 자동으로 할당 : Default field

- numeric = 0

- boolean = false

- object reference = null

 

constructor를 아무것도 작성하지 않았다면 compiler가 자동으로 no-argument constructor 생성.

 

this : used as an implicit parameter of a method.

- 같은 클래스 내에서 한 constructor가 다른 constructor를 호출할 수 있게 해줌.

- caller constructor의 first statement여야 함.

- 하나의 common construction code로 factor out/write를 가능하게 함.

 

java는 garbage collector가 있어서 알아서 object를 파괴한다.

하지만 가끔 resource로 memory를 file처럼 사용하는 경우가 있는데, 이 때 오랜 기간 필요하지 않을 경우 resource를 reclaimed 해야한다. 이때 사용되는 함수가 finalize()이며 garbage coollector가 객체를 파괴하기 전에 실행되어 파일 리스를 close()한다.

 

4.7 Packages

: 관련 있는 클래스를 하나의 패키지로 묶는다.

- 클래스 이름 충돌 방지

- 다른 코드로부터 분리

컴파일러 관점에서 nested package 간에는 어떤 relationship도 존재하지 않음.

 

같은 package 내의 모든 클래스는 사용 가능, public class는 다른 package 내에서도 가능.

*을 사용한 import는 오직 하나의 package만 가능. 따라서 2개 이상이 필요할 경우 사용 불가능.

§ Note 1: compiler searches the packages .

§ Note 2: bytecodes in class files always use full package names to refer to other classes.

 

import는 static field, static method만 사용 가능하다. (class X)

 

4.8 Setting the Class Path

class는 file system의 subdirectory에 존재. class path는 package name과 match 되어야 함.

Class file은 JAR file에 저장되며 JAR file에는 많은 class file과 완성된 format의 subfolder가 존재.

§ Class path= list of directories and JAR files in which class files or packages are located

 

4.9 Documentation Comments

JDK는 javadoc라는 tool을 갖고 있음.

 

4.10 Class Design Hints

1) Always keep data private to satisfy encapsulation principle

2) Always initialize data explicitly both local variables and instance fields by assigning

our own default values during declaration or in side constructors.

3) Don't use too many basic types in a class: replace related basic types with a class

4) Not all fields need field accessors and matadors

5) Break up classes that have too many responsibilities

6) Make the names of your classes and methods reflect their responsibilities.

 

Note 1 : we covered fundamentals of objects and classes in java.

Note 2 : To be truly object-oriented, Java support inheritance and polymorphism

 

 

Comments