본문 바로가기
Java 배우기

Declaring Member Variables 멤버변수 선언하기

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

멤버변수 선언하기 Declaring Member Variables


몇 가지 종류의 변수가 있습니다: There are several kinds of variables:

  • 클래스 안의 멤버변수 - 이들을 필드(fields)라고 합니다.
    Member variables in a class—these are called fields.
  • 메소드 또는 코드블록 안의 변수 - 이들을 로컬변수(local variables)라고 합니다.
    Variables in a method or block of code—these are called local variables.
  • 메소드 선언 안의 변수 - 이들을 패러미터(parameters) 라고 합니다.
    Variables in method declarations—these are called parameters.

클래스 Bicycle은 그 필드를 정의하기 위하여 다음의 코드 라인들을 사용합니다:
The Bicycle class uses the following lines of code to define its fields:

public int cadence;
public int gear;
public int speed;

필드 선언들은 3개의 구성요소가 순서대로 이루어져 있습니다:
Field declarations are composed of three components, in order:

  1. public 또는 private과 같은 0개 이상의 제한자들
    Zero or more modifiers, such as public or private.
  2. 필드의 타입 The field's type.
  3. 필드의 이름 The field's name.

Bicycle 의 필드 이름은 cadencegear 및 speed 이며, 데이터타입은 모두 정수(int)입니다.
키워드 public은 이들 필드들이, 클래스에 접근 가능한 객체가 접근할 수 있는, public 멤버들임을 알려줍니다.
The fields of Bicycle are named cadencegear, and speed and are all of data type integer (int). The public keyword identifies these fields as public members, accessible by any object that can access the class.


접근 제한자 Access Modifiers


사용된 첫 번째(가장 왼쪽) 제한자를 이용하여, 어떤 다른 클래스들이 멤버 필드에 접근하는 지를 제어할 수 있습니다.
우선,
public 과 private 만을 고려하십시오. 
다른 접근 제한자는 나중에 논의됩니다.

The first (left-most) modifier used lets you control what other classes have access to a member field. For the moment, consider only public and private. Other access modifiers will be discussed later.

  • public modifier—모든 클래스들이 접근할 수 있는 필드 the field is accessible from all classes.
  • private modifier—그 자신 클래스 내에서만 접근할 수 있는 필드 the field is accessible only within its own class.

encapsulation의 취지에서, 흔히 fields를  private으로 만듭니다. 

이것은 Bicycle 클래스에서만 필드에 직접 접근할 수 있음을 의미합니다.
하지만 이들 필드 값에 접근해야 할 경우도 있습니다. 
필드 값을 얻을 수 있는 public 메소드를 추가하여 간접적으로
 필드에 접근할 수 있게 합니다:
In the spirit of encapsulation, it is common to make fields private. This means that they can only be directly accessed from the Bicycle class. We still need access to these values, however. This can be done indirectly by adding public methods that obtain the field values for us:

public class Bicycle {
        
    private int cadence;
    private int gear;
    private int speed;
        
    public Bicycle(int startCadence, int startSpeed, int startGear) {
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;
    }
        
    public int getCadence() {
        return cadence;
    }
        
    public void setCadence(int newValue) {
        cadence = newValue;
    }
        
    public int getGear() {
        return gear;
    }
        
    public void setGear(int newValue) {
        gear = newValue;
    }
        
    public int getSpeed() {
        return speed;
    }
        
    public void applyBrake(int decrement) {
        speed -= decrement;
    }
        
    public void speedUp(int increment) {
        speed += increment;
    }
}


타입 Types


모든 변수는 타입을 가져야 합니다.

intfloatboolean 등 원시 타입을 사용할 수 있습니다.
또는 문자열, 배열 또는 객체와 같은 참조 타입(reference types)도 사용할 수 있습니다.
All variables must have a type. You can use primitive types such as intfloatboolean, etc. Or you can use reference types, such as strings, arrays, or objects.


변수 이름 Variable Names


모든 변수들은 그들이 필드 이든, 로컬 변수 이든 또는 패러미터 이든 관계없이, 단원 Language Basics, Variables—Naming에서 다루어진 것과 같은 작명 규칙 및 협약을 따라야 합니다.  

이 단원에서, 다음을 제외하고, 동일한 작명 규칙 및 협약이 메소드 및 클래스 이름에 사용됨을 알아야 합니다.
All variables, whether they are fields, local variables, or parameters, follow the same naming rules and conventions that were covered in the Language Basics lesson, Variables—Naming.

In this lesson, be aware that the same naming rules and conventions are used for method and class names, except that

  • 클래스 이름의 첫 째 글자는 대문자이어야 하며, the first letter of a class name should be capitalized, and
  • 메소드 이름 안의 첫 째(또는 유일한) 단어는 동사이어야 함.    the first (or only) word in a method name should be a verb.



반응형

댓글