클래스 Classes
Object-oriented Programming Concepts 단원에서, 객체-지향 개념을 소개할 때 하위클래스가 경주 자전거, 산악 자전거 및 탠덤 자전거인, 자전거 클래스를 예제로 사용하였습니다.
아래에 자전거 클래스로 구현할 수 있는 샘플 코드가 있는데, 이 코드로 클래스 선언의 개요를 설명합니다.
이 단원의 후속 섹션에서 단계 별로 클래스 선언을 백업하고 설명할 것입니다.
당분간, 세부 사항에 대한 걱정은 안하셔도 됩니다.
The introduction to object-oriented concepts in the lesson titled Object-oriented Programming Concepts used a bicycle class as an example, with racing bikes, mountain bikes, and tandem bikes as subclasses. Here is sample code for a possible implementation of a Bicycle
class, to give you an overview of a class declaration. Subsequent sections of this lesson will back up and explain class declarations step by step. For the moment, don't concern yourself with the details.
public class Bicycle { // Bicycle 클래스는 3개의 fields를 갖고 있습니다
public int cadence; public int gear; public int speed; // Bicycle 클래스는 1개의 생성자(constructor)를 갖고 있습니다
public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } // Bicycle 클래스는 4개의 methods를 갖고 있습니다
public void setCadence(int newValue) { cadence = newValue; } public void setGear(int newValue) { gear = newValue; } public void applyBrake(int decrement) { speed -= decrement; } public void speedUp(int increment) { speed += increment; } }
Bicycle의 하위클래스인 MountainBike
의 클래스 선언은 아래와 같습니다:
A class declaration for a MountainBike
class that is a subclass of Bicycle
might look like this:
public class MountainBike extends Bicycle { // 하위클래스 MountainBike는 1개의 field를 갖고 있습니다
public int seatHeight; // 하위클래스 MountainBike는 1개의 constructor를 갖고 있습니다
public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) { super(startCadence, startSpeed, startGear); seatHeight = startHeight; } // 하위클래스 MountainBike는 1개의 method를 갖고 있습니다
public void setHeight(int newValue) { seatHeight = newValue; } }
seatHeight를 정하는 메소드(mountain bikes는 terrain demands에 따라 올리거나 내릴 수 있는 좌석을 갖고 있음)를 추가합니다.MountainBike는
Bicycle
의 모든 필드와 메소드를 상속받고, 필드 seatHeight
와
MountainBike
inherits all the fields and methods of Bicycle
and adds the field seatHeight
and a method to set it (mountain bikes have seats that can be moved up and down as the terrain demands).
'Java 배우기' 카테고리의 다른 글
Declaring Member Variables 멤버변수 선언하기 (0) | 2016.01.05 |
---|---|
Declaring Classes 클래스 선언하기 (0) | 2016.01.05 |
Lesson: Classes and Objects 단원: 클래스와 객체 (0) | 2016.01.05 |
Questions and Exercises: Control Flow Statements (0) | 2016.01.05 |
Summary of Control Flow Statements (0) | 2016.01.05 |
댓글