인스턴스 변수와 정적 변수(Static Variables)
// 정적변수를 이용하여 생성된 Circle객체의 개수를 해아리는 프로그램
import java.io.*;
public class CircleArea
{
public static void main (String[] args) throws IOException
{
BufferedReader stdin = new BufferedReader(new InputStreamReader (System.in));
int rad, xPos, yPos; // 지역변수
String aString; // 객체 참조 변수
Circle aCircle;
while(true) // NULL과 같지않냐?
{
System.out.print ("\nEnter the radius <type enter-key to quit>: ");
aString = stdin.readLine();
if(aString.equals (""))
{
break;
}
rad = Integer.parseInt (aString);
System.out.print ("Enter the x coordinate of origin: ");
xPos = Integer.parseInt (stdin.readLine());
System.out.print ("Enter the y coordinate of origin: ");
yPos = Integer.parseInt (stdin.readLine());
aCircle = new Circle (xPos, yPos, rad);
aCircle.create();
}
System.out.println ("This is the end of program.");
}
}
class Circle
{
static int count = 0; // 정적 변수
static final double PI = 3.14; // 상수 (final : #define 과 같은 역할)
int radius; // 원의 반지름 {
int xCenter; // 중심의 x좌표 인스턴스 변수.
int yCenter; // 중심의 y좌표 }
Circle (int x, int y, int rad)
{
xCenter = x;
yCenter = y;
radius = rad;
count++;
}
void create()
{
System.out.println (count + " Object created.");
}
double area()
{
return radius * radius * PI;
} // method area
double circumference()
{
return 2 * radius * radius * PI;
}
}
{
return radius * radius * PI;
} // method area
double circumference()
{
return 2 * radius * radius * PI;
}
}
객체참조변수 ☞ 곧 태어날 아기의 이름 : 클래스에 이름을 부여. (Circle aCircle;)
객 체 ☞ 아기가 태어나 이름을 갖게 된것. 홍길동 : 객체참조변수에 공간을 확보. 인스턴스 객체라고도함
(aCircle = new Circle (xPos, yPos, rad);)
인스턴스 변수 ☞ 클래스의 지역변수 (같은 클래스에 속하는 모든 객체가 서로 다른 메모리 공간을 차지함)
정적 변수 ☞ 클래스의 static변수 (같은 클래스에 속하는 모든 객체가 공유하며 객체를 생성하지 않아도 이미 존재한다.)
aString.equals : 문자열을 물어보는 메소드(aString.equals("") : NULL이냐고 물어보는 것);
메소드 오버로딩, 복사 생성자
// 세 사각형 면적의 합을 구하는 프로그램으로 메소드 오버로딩을 사용한다.
public class TotalArea
{
public static void main (String[] args)
{
Rectangle rectA, rectB, rectC;
rectA = new Rectangle();
System.out.println("--------------------------------------");
rectB = new Rectangle (10, 20);
System.out.println("--------------------------------------");
rectC = new Rectangle (15);
System.out.println("--------------------------------------");
Rectangle rectD = new Rectangle (rectA); // 객체가 복사됨
System.out.println("--------------------------------------");
Rectangle rectZ = rectA; // 객체가 생성되지 않음.
System.out.println("--------------------------------------");
double area;
area = rectA.area() + rectB.area() + rectC.area();
System.out.println ("The total area of three rectangle is " + area);
}
}
class Rectangle
{
double length;
double width;
Rectangle(double l, double w)
{
length = l;
width = w;
System.out.println("인자 2개인 생성자 호출");
}
Rectangle()
{
length = 10;
width = 20;
System.out.println("인자 없는 생성자 호출");
}
Rectangle(int size)
{
length = size;
width = size;
System.out.println("인자 1개인 생성자 호출");
}
Rectangle(Rectangle oldRect)
{
length = oldRect.length;
width = oldRect.width;
System.out.println("복사 생성자 호출");
}
double area()
{
return length * width;
} // method area
} // class Rectangle
정적 메소드
☞ 메소드에 static이 붙음. 주의 할점
class A
{
int b;
int c;
static void test()
{
b=3;
}
}
a.test();
위와 같은 코드는 오류가 난다. 변수 b에 static을 붙여줘야 정상 작동한다.
메소드 오버로딩 ☞ 같은 이름의 메소드가 여러게 단. 인자가 달라야한다.(인자의 순서나 인자의 숫자로 구분)
이를 객체지향의 다형성이라고 부른다.
복사 생성자 ☞ 기존의 객체와 동일한 독립된 객체를 생성(오버로딩된 생산자의 정의)
이 생성자는 이미 존재하는 객체를 인수로 받아서, 기존 객체의 인스턴스 변수의 값을 새로운 객체의 인스턴스 변수에 부여함으로 기존 객체와 같은 새로운 객체를 생성할 수 있다.
'Java > 강의' 카테고리의 다른 글
String 클래스와 배열 (0) | 2011.02.11 |
---|---|
여러 종류의 객체 (0) | 2011.02.11 |
인수 전달 기법 (0) | 2011.02.09 |
생성자(Class Constructors) (0) | 2011.02.08 |
정수 데이타의 입출력!! (0) | 2011.02.07 |