차량을 만들고 비교하는 "Racing Simulator"를 만들려고합니다. 여기에는 차량이 정의 된 클래스와 속도가 비교되는 주 클래스가 포함됩니다. Vehicle의 두 인스턴스를 만들고 두 인스턴스에서 getSpeed 메서드를 사용하면 속도가 동일합니다. 왜 그런가?Java 클래스의 인스턴스 생성과 관련된 문제
주성분 :
는public class Main {
static Vehicle bike, jeep;
//static Race race;
public static void main(String args[]) {
bike = new Vehicle(4000, 20, 30.5, "bike");
jeep = new Vehicle(3000, 12, 9.8, "Jeep");
//race = new Race(bike, jeep, 0);
System.out.println("Bike: " + bike.getTopSpeed() + " Jeep: " + jeep.getTopSpeed());
}
}
차량 :
public class Vehicle {
static int _weight, _topSpeed;
static double _zeroToSixty;
static String _vehicleName;
public Vehicle(int weight, int topSpeed, double zeroToSixty, String vehicleName) {
_weight = weight;
_topSpeed = topSpeed;
_zeroToSixty = zeroToSixty;
_vehicleName = vehicleName;
}
public static void setVehicleName(String name) {
_vehicleName = name;
}
public static void setWeight(int weight) {
_weight = weight;
}
public static void setTopSpeed(int topSpeed) {
_weight = topSpeed;
}
public static void setZeroToSixty(double zeroToSixty) {
_zeroToSixty = zeroToSixty;
}
public static String getVehicleName() {
return _vehicleName;
}
public static int getWeight() {
return _weight;
}
public static int getTopSpeed() {
return _topSpeed;
}
public static double getZeroToSixty() {
return _zeroToSixty;
}
}
본체의 출력은 :
"자전거 12 지프 12"
는
'_topSpeed'는'정적 '이므로 문제가됩니다. 변수 선언에서'static' 수정자를 제거하십시오. ClassLoader 당 –