본문 바로가기
Java 배우기

Creating a Package 패키지 만들기

by 노화방지 Anti-aging Hairstyle 2016. 1. 24.
반응형

패키지 만들기 Creating a Package


패키지를 만들려면, 패키지의 이름을 선택한 다음 (이름짓기 규칙은 다음 섹션에서 설명), 패키지에 넣고자 하는 타입 (클래스, 인터페이스, 열거enumerations  및 주석 타입)이 포함된 모든 소스 파일의 상단에 해당 이름을 가진 패키지 문을 넣습니다. ,
패키지 문 (예, 패키지 그래픽)은  소스 파일의 첫 줄이어야 합니다. 
각 소스 파일에는 1개의 패키지 문만 있을 수 있으며, 그것은 파일의 모든 타입에 적용됩니다.
To create a package, you choose a name for the package (naming conventions are discussed in the next section) and put a package statement with that name at the top of every source file that contains the types (classes, interfaces, enumerations, and annotation types) that you want to include in the package.

The package statement (for example, package graphics;) must be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file.


Note: If you put multiple types in a single source file, only one can be public, and it must have the same name as the source file. For example, you can define public class Circle in the file Circle.java, define public interface Draggable in the file Draggable.java, define public enum Day in the file Day.java, and so forth.

You can include non-public types in the same file as a public type (this is strongly discouraged, unless the non-public types are small and closely related to the public type), but only the public type will be accessible from outside of the package. All the top-level, non-public types will be package private.

If you put the graphics interface and classes listed in the preceding section in a package called graphics, you would need six source files, like this:

//in the Draggable.java file
package graphics;
public interface Draggable {
    . . .
}

//in the Graphic.java file
package graphics;
public abstract class Graphic {
    . . .
}

//in the Circle.java file
package graphics;
public class Circle extends Graphic
    implements Draggable {
    . . .
}

//in the Rectangle.java file
package graphics;
public class Rectangle extends Graphic
    implements Draggable {
    . . .
}

//in the Point.java file
package graphics;
public class Point extends Graphic
    implements Draggable {
    . . .
}

//in the Line.java file
package graphics;
public class Line extends Graphic
    implements Draggable {
    . . .
}

If you do not use a package statement, your type ends up in an unnamed package. Generally speaking, an unnamed package is only for small or temporary applications or when you are just beginning the development process. Otherwise, classes and interfaces belong in named packages.


반응형

댓글