본문 바로가기
Java 배우기

Objects 객체

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

객체 Objects


자바 프로그램은 많은 객체를 생성하는데, 객체들을 메소드들을 invoking함으로써 상호작용합니다.
이들 객체간 상호작용을 통하여, 프로그램은 GUI 구현, 애니메이션 가동 또는 네트웍 상에서의 정보 송수과 같은 다양한 과제들을 수행할 수 있습니다.
객체가 생성된 것에 대한 작업을 일단 완료하면, 그 리소스들을 다른 객체들이 재사용합니다.

아래에 CreateObjectDemo라는 이름의 작은 프로그램이 있는데 이것은 1개의 Point 객체 및 2개의 Rectangle 객체 등 3개의 객체를 생성합니다.
이 프로그램을 컴파일하려면 모두 3개의 소스 파일이 필요합니다.

A typical Java program creates many objects, which as you know, interact by invoking methods. Through these object interactions, a program can carry out various tasks, such as implementing a GUI, running an animation, or sending and receiving information over a network. Once an object has completed the work for which it was created, its resources are recycled for use by other objects.

Here's a small program, called CreateObjectDemo, that creates three objects: one Point object and two Rectangle objects. You will need all three source files to compile this program.


public class CreateObjectDemo { public static void main(String[] args) { // 1개의 객체와 2개의 rectangle 객체를 선언하고 만듭니다. Point originOne = new Point(23, 94); Rectangle rectOne = new Rectangle(originOne, 100, 200); Rectangle rectTwo = new Rectangle(50, 100); // rectOne의 width, height, area를 표시 System.out.println("Width of rectOne: " + rectOne.width); System.out.println("Height of rectOne: " + rectOne.height); System.out.println("Area of rectOne: " + rectOne.getArea()); // set rectTwo's position rectTwo.origin = originOne; // display rectTwo's position System.out.println("X Position of rectTwo: " + rectTwo.origin.x); System.out.println("Y Position of rectTwo: " + rectTwo.origin.y); // move rectTwo and display its new position rectTwo.move(40, 72); System.out.println("X Position of rectTwo: " + rectTwo.origin.x); System.out.println("Y Position of rectTwo: " + rectTwo.origin.y); } }

이 프로그램은 다양한 객체에 대한 정보를 만들고, 조작하고, 디스플레이 합니다. 그 산출물:
This program creates, manipulates, and displays information about various objects. Here's the output:

Width of rectOne: 100
Height of rectOne: 200
Area of rectOne: 20000
X Position of rectTwo: 23
Y Position of rectTwo: 94
X Position of rectTwo: 40
Y Position of rectTwo: 72

The following three sections use the above example to describe the life cycle of an object within a program. From them, you will learn how to write code that creates and uses objects in your own programs. You will also learn how the system cleans up after an object when its life has ended.


반응형

댓글