MainClass 내용 올바르게 수정하기

인철·2023년 10월 13일
0

algorithm

목록 보기
19/91

레퍼런스변수, 배열문제

class Main {
  public static void main(String[] args) {
    int 자동차 = new 자동차;
     //자동차는 정수가 아니기 때문에 int가 못온다
		 // 객체에 ()가 빠졌다
    자동차.달립니다();
		//동사적표현이 틀렸다
  }
}

class 자동차 {
  void 달리다() {
    System.out.println("자동차가 달립니다.");
  }
}

정답
class Main {
  public static void main(String[] args) {
    자동차 자동차a = new 자동차();
    
    자동차a.달리다();
    
  }
}

리모콘을 변수에 저장하지 않고 바로 사용
 문제 : 아래 코드가 실행되도록 해주세요.

class Main {
  public static void main(String[] args) {
    new 자동차().달리다();
  }
}

//정답
// 문제 : 아래 코드가 실행되도록 해주세요.

class Main {
  public static void main(String[] args) {
    // new 자동차() => 리모콘
    new 자동차().달리다();
  }
}

class 자동차 { //자동차클래스를 만들어준다
  void 달리다() { //동사적표현을 만들어준다
    System.out.println("달립니다.");
  }
}
profile
같은글이있어도양해부탁드려요(킁킁)

0개의 댓글