은근한

140311 객체 생성 본문

카테고리 없음

140311 객체 생성

EsJoo 2014. 3. 14. 11:40

1. 과제

클래스 Car로 

myCar,yourCar의 객체를 생성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Car {
    // 필드 정의
    String color;        // 색상
    int speed;            // 현재 속도
    int gear;            // 현재 기어
    void print(){
        System.out.println("("+color+","+speed+","+gear+")");
     }
}
 
class CarTest {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Car myCar = new Car();    // 객체 생성
        myCar.color = "red";    // 객체의 필드 변경
        myCar.speed = 0;        // 객체의 필드 변경
        myCar.gear = 1;            // 객체의 필드 변경
        myCar.print();            // 객체의 메소드 호출        
        
        Car yourCar = new Car();// 객체 생성
        yourCar.color = "blue";    // 객체의 필드 변경
        yourCar.speed = 60;        // 객체의 필드 변경
        yourCar.gear = 3;        // 객체의 필드 변경
        yourCar.print();        // 객체의 메소드 호출
     }
}





과제2

클래스 객체 그림