JAVA_25.05.28

장성욱·2025년 5월 28일

JAVA

목록 보기
6/23

클래스

클래스 : 제품을 만드는 설계도
객체 : 설계도로 만들어진 제품

new가 붙으면 객체가 만들어짐

class Main {
  public static void main(String[] args) {
    build car = new build() ;	// 객체 1 
     car.name = "K5";
     car.brand = "KIA";
     car.color = "white";
     car.max_speed = 180;
     car.accident = false;

     car.run();		// 함수 1

     build car2 = new build();	 // 객체 2
     car2.name = "SONATA";
     car2.brand = "HYUNDAI";
     car2.color = "black";
     car2.max_speed = 220;
     car2.accident = false;

     car2.run(); 	//함수 2


    System.out.println("== 자동차1 정보 ==");
    System.out.println("차종 : " + car.name);
    System.out.println("브랜드 : " + car.brand);
    System.out.println("색상 :" + car.color);
    System.out.println("최고속도 : " + car.max_speed +"km");
    System.out.println("사고여부 : " + car.accident);


    System.out.println("== 자동차2 정보 ==");
    System.out.println("차종 : " + car2.name);
    System.out.println("브랜드 : " + car2.brand);
    System.out.println("색상 :" + car2.color);
    System.out.println("최고속도 : " + car2.max_speed +"km");
    System.out.println("사고여부 : " + car2.accident);
  }
}
class build { 		// 클래스 생성
  String name;
  String brand;
  String color;
  int max_speed;
  boolean accident;

  void run(){		// 함수 생성
    System.out.println(name + "(이)가 달립니다.");
  }


}
class Main {
  public static void main(String[] args) {
    build car1 = new build();	// 객체 1 생성
    car1.name = "K5";
    car1.max_speed = 230;

    car1.run();

    build car2 = new build();	 // 객체 2 생성
    car2.name = "K7";
    car2.max_speed = 210;

    car2.run();

  }
}

class build {		// 클래스 생성
  int max_speed;
  String name;

  void run() {		// 함수 생성
    System.out.println(name + "이(가) 최고속력 " + max_speed + "km로 달립니다.");
    // System.out.printlnt(this.name + "이(가) 최고속력 " 
    + this.max_speed + "km로 달립니다.");	this.는 생략이 가능
    
  }
}

객체를 사용하지 않고 플레이어2 만들기

cl   player1 = new game();
player1.age = 20;
player1.name = "홍길동";
player1.job = "의적";

int player2_age = 21;
String player2_name = "홍길순";
String player2_job = "의적";
    
  }
      }

class game {
  int age;
  String name;
  String job;
}ass Main {
  public static void main(String[] args) {
    game player1;

변수를 선언해서 만들면 되지만, 효율적이지 않음
자바는 객체 지향 언어임

profile
https://frost-puck-b0f.notion.site/B-2610fdaef71d80c49d1bccdcb575dcb5

0개의 댓글