Skip to content

Commit

Permalink
Update 2024-10-07-정보처리기사
Browse files Browse the repository at this point in the history
  • Loading branch information
gghwan authored Oct 7, 2024
1 parent c9ab4ab commit 9b2cf31
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions _posts/2024-10-07-정보처리기사
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,60 @@ values = [
# 번호를 매겨서 실제 값과 타입을 출력하는 코드
for index, value in enumerate(values, start=1):
print(f"{index}. {value} - {type(value)}")

자바 코드

정적변수, 인스턴수 변수, 생성자, 인스턴스 메서드, 정적 메서드, 정적 메서드에서 정적 변수 처리
public class Main {
public static void main(String[] args) {
Car car1 = new Car("Tesla");
Car car2 = new Car("Hyundai");

car1.accelerate(50);
car2.accelerate(30);

System.out.println(" : " + Car.getTotalCars()); 총 차량 수

Car.resetTotalCars();
System.out.println(" : " + Car.getTotalCars()); 초기화 후 총 차량 수
}
}
class Car {
// 인스턴스 변수
private String model;
private int speed;

// 정적 변수
private static int totalCars = 0;

// 생성자
public Car(String model) {
this.model = model;
this.speed = 0;
totalCars++; // 정적 변수 증가
}

《 》 Java , , 의 클래스 객체 변수와 메서드 복습
// 인스턴스 메서드
public void accelerate(int increment) {
speed += increment;
System.out.println(model + " : " + speed + "km/h"); 속도 증가
}

// 정적 메서드
public static int getTotalCars() {
return totalCars;
}

// 정적 메서드에서 정적 변수 처리
public static void resetTotalCars() {
totalCars = 0;
System.out.println(" "); 총 차량 수 초기화
}
}
출력값:
Tesla : 50km/h 속도 증가
Hyundai : 30km/h 속도 증가
총 차량 수: 2
총 차량 수 초기화
초기화 후 총 차량 수: 0

0 comments on commit 9b2cf31

Please sign in to comment.