我构建了一个具有此方法的程序,我还想计算我创建的对象数.但是在运行程序之后.它说有0个对象被创建.谁能知道为什么这不正确?应该说有4个对象正在创建.这是我的代码:
/**
This program implements code for a Circle class,
which has a radius field, set and
get methods, and a getArea method.
Author: Michael Wu.
*/
public class Circle
{
private double radius;
private static int numCircles;
public Circle(double radius)
{
this.radius = radius;
}
//SetRadius method,sets radius.
public void setRadius(double radius)
{
this.radius = radius;
}
//GetRadius method; returns radius.
public double getRadius()
{
return radius;
}
//Constructor increments numbers of circles.
public Circle()
{
numCircles++;
}
//Copy …Run Code Online (Sandbox Code Playgroud) 我尝试使用一个简单的java EnumMap来存储道路类型到默认速度的映射.但是我立即遇到了以下问题:
我创建了一个简单的枚举如下:
public enum RoadCategory {
AUTOBAHN("Autobahn", 0),
BUNDESSTR("Bundesstrasse", 1),
OTHER("other", -1);
private String name;
private int code;
private RoadCategory(String name, int code){
this.name = name;
this.code = code;
}
}
Run Code Online (Sandbox Code Playgroud)
接下来,我创建了一个小类,试图将此枚举用作枚举映射的键:
import java.util.EnumMap;
import java.util.Map;
public class VehicleConfig {
public static void main(String[] args) throws Exception {
VehicleConfig v = new VehicleConfig();
v.addSpeedMapping(RoadCategory.AUTOBAHN, 80.0);
}
private Map<RoadCategory,Double> speedMap;
public VehicleConfig(){
this.speedMap = new EnumMap<RoadCategory, Double>(RoadCategory.class);
}
public double addSpeedMapping(RoadCategory category, double speed) throws Exception{
if(speedMap == null) …Run Code Online (Sandbox Code Playgroud)