[변수] 프로그래밍 언어의 scope에 관해서
- 최초 등록일
- 2005.07.21
- 최종 저작일
- 2004.04
- 6페이지/ 한컴오피스
- 가격 2,000원
소개글
프로그래밍 언어의 scope에 관해서
목차
※java에서 변수의 범위(scope)를 결정하는 방법
※c/c++에서 변수의 범위(scope)를 결정하는 방법
본문내용
※java에서 변수의 범위(scope)를 결정하는 방법
자바에서는 세 가지 타입의 변수가 존재하는 데, 각각 인스턴스 변수, 클래스 변수, 지역 변수(또는 로컬 변수)라고 부릅니다.
class Jabberwock{
String color;
String sex;
boolean hungry;
void feedJabberwock(){
if(hungry == true){
System.out.println("Yum -- a parent!");
hungry = false;
}else
System.out.println("No, thanks -- already ate.");
}
void showAttributes(){
System.out.println("This is a " + sex + " " + color + " jabberwock.");
if(hungry == true)
System.out.println("The jabberwock is hungry.");
else
System.out.println("The jabberwock is full.");
}
public static void main(String arguments[])
Jabberwock j = new Jabberwock();
j.color = "orange";
j.sex = "male";
j.hungry = true;
System.out.println("Calling showAttributes...");
j.showAttributes();
System.out.println("----");
System.out.println("Feeding the Jabberwock...");
j.feedAttributes();
System.out.println("----");
System.out.println("Calling showAttributes...");
j.showAttributes();
System.out.println("----");
System.out.println("Feeding the Jabberwock...");
j.feedAttributes();
}
}
클래스에서는 속성의 뱐수로 정의하는데 속성을 그 클래스의 객체에 대한 전역 변수와 비슷하게 간주할 수 있습니다. 각 객체는 그 변수에 대한 다른 값을 가질 수 있으며, 인스턴스 변수(instance variable)라고 부릅니다. 인스턴스 변수(instance variable)란 한 특정한 객체의 속성을 정의하는 정보입니다. 객체의 클래스는 어떠한 종류의 속성이 정해질 것인가를 정의하며, 각각의 인스턴스는 그 속성에 대한 값들을 가집니다. 인스턴스 변수는 객체 변수(object variable)라고 부릅니다. 각 클래스의 속성은 대응되는 값만을 가집니다.
참고 자료
없음