본문 바로가기

프로그래밍/JAVA

Bluej로 자바(java)익히기 - 오브젝트, 클래스, 메소드(Object, class ,method)



첫번째 문단은 영어 뒤는 한글 번역.


1. 클래스 이해하기(Understanding class definitions)



<Main concepts to be covered>

A. fields

B. constructors (includg default)

C. parameters

D. assignment statements

E. accessor methods

F. Javadoc comments


<이 장에서 다룰 메인 컨셉>

A. 필드

B. 생성자(디폴트 생성자 포함)

C. 파라미터

D. 값 입력

E. getters메소드

F. 자바 document



<Keyword>

    - Words with a special meaning in the language

      e.g) public, class, private, int

    - Also known as reserved words


<키워드>

    - 프로그래밍 언어에서 특별한 의미를 같는 단어들 

      예) public, class, private, int 등

    - 또한 예약어라고 알려져 있다.



A. Fields

    - Fields store values for an object

    - They are also known as instance variables

    - Fields define the state of an object

    - Use Inspect to view the state

    - Some values change often

    - Some change rarely (or not at all)


    public class TestMachine{

        private int price;

        private int balance;

        private int total;


       //Furher details omitted.

    }


    - private(visibility modifier) int(type) price(variable name)


A.필드

    - 필드는 오브젝트에 대해서 값을 저장한다.

    - 필드는 인스턴스 변수라고도 한다.

    - 필드는 오브젝트의 state(상태?)를 정의한다.

    - 필드 상태를 보기위해 인스펙트를 사용

    - 몇몇의 값들은 종종 변한다.

    - 몇몇은 아주 가끔이나 변하거나 전혀 변하지 않는다.


    public class TestMachine{

        private int price;       //인스턴스 변수들..

        private int balance;

        private int total;


       //Furher details omitted.

    }


    - private(접근 제한자) int(타입) price(변수이름)



B. Constructors


  public TestMachine(int testCost){

        price = testCost;

        balance = 0;

        total = 0;

  }


    - Initialize an object

    - Have the same name as their class

    - Close association with the fields.

    - Store initial values into the fields

    - External parameter values for this.


B. 생성자

    - 오브젝트를 초기화 시킨다

    - 클래스 이름과 똑같은 이름을 가진다.

    - 필드와 연관성이 가깝다.

    - 최초 값을을 변수에 저장한다.

    - 외부 파라미터 변수를 받을 수 있다.





C. Assignment

    - Values are stored into fields(and other variables) via assignemt statements

      -> variable = expression;  (price = cost;)

    - A variable stores a single value, so any previous value is lost

    - The variable receiving the value is on the left


C. 할당

    - 값들은 할당 statements를 통해 변수에 저장된다.

      -> 변수 = 표현;  (price = cost;)

    - 변수는 값이 저장되면 이전값은 사라진다.(대체)

    - 변수는 표현식에서 왼쪽에 위치한다.


D. Choosing variable names

    - There is a lot of freedom over choice of names. Use it wisely!

    - Choose expressive names to make code easier to understand:

        -> price, amount, name, age, etc.

    - Avoid single-letter or cryptic names:

    - w, t5, xyz123


D. 변수이름 짓기

    - 변수이름 짓는 것은 자유지만 알맞게 만드는것이 좋다.

    - 코드를 더 이해하기 쉽게 만드는것이 좋다.

        -> priceamountnameage, etc.

    - 영문 한 레터나 아리송한 이름은 피해라

    - wt5xyz123


E. Default Constructor

    - When a constructor is not provided, Java will automatically generate one with no parameters.

    - This default constructor will initialize the fields to default values.

    - If we provide a constructor with parameters, then Java will not generate a default.

    - For future use we will include a default constructor for each class.


E. 기본 생성자

    - 클래스 내에서 기본 생성자를 작성 하지 않으면 자바가 파라미터 없는 기본 생성자를 생성한다.

    - 기본 생성자는 필드 값을 기본 변수로 초기화 시킨다.

    - 파라미터가 있는 생성자를 작성하면 자바는 기본 생성자를 만들지 않는다.


    public TestMachine(int testCost){

        price = testCost;

        balance = 0;

        total = 0;

    }


F. methods

    - Methods implement the behavior of objects.

    - Methods have a consistent structure comprised of a header and a body.

    - Accessor methods provide information about an object.


F. 메소드

    - 메소드는 오브젝트의 행동을 실행한다.

    - 메소드는 헤더와 바디로 구성되어 있는 일관성있는 구조를 가지고 있다.

    - 엑세스 메소드는 오브젝트에 대한 정보를 제공한다.


        G. Methods structure

            - The header provides the method’s signature:

                ->public int getPrice()

 

            - The header tells us:


                -> the name of the method


                -> what parameters it takes


                -> whether it returns a result


                -> its visibility to objects of other classes


            - The body encloses the method’s statements.


G. 메소드 구조

    - 헤더는 메소드 시그니쳐를 제공한다.

        -> public ing getPrice()

    - 헤더는

        -> 메소드의 이름

        -> 무슨 파라미터가 있는지

        -> 리턴을 하는지 안하는지

        -> 다른 클래스의 오브젝트에서 접근이 가능한지 아닌지


H. Accessor (get) methods

    - An accessor method always has a return type that is not void.


    - An accessor method returns a value (result) of the type given in the header.


    - The method will contain a return statement to return the value.


    - NB: Returning is not printing!


H. get 메소드(엑세서 메소드)

    - 엑세서 메소드는 항상 리턴타입을 가지고 있다.

    - 엑세서 메소드는 헤더에 명시된 리턴타입의 값을 리턴한다.

    - 이 메소드는 값을 리턴하기 위해서 리턴 스테이트 먼트를 가지고 있다.



I. Javadoc comments

    - Every class has a comment at the top that explains what the class is.

    - Every method has a comment that includes information about parameters (if any) and return value (if any).

    - Javadoc comments and tags e.g. @author, @version, @return, @param are used to generate documentation for the class.



I. 자바 다큐먼트 코멘트

    - 모든 클래스는 제일 위에 이 클래스가 무엇인지 설명하는 코멘트를 가지고 있다.

    - 모든 메소드는 파리미터와 리턴 값에 대한 정보를 포함한 코멘트를 가지고 있다.

    - 자바독 코멘트와 태그들 @author, @version, @return, @param 들은 다큐멘트를 생성하기 위해 사용 되어 진다.