日期:2014-05-20 浏览次数:20792 次
class RaceCar { private static int count = 0; private int carNo; private String driverName; public RaceCar(int carNo, String driverName) { this.carNo = carNo; this.driverName = driverName; count++; } public String toString() { return String.format("carNo:%d, driverName:%s", carNo, driverName); } public static int getCount() {return count;} public static void main(String[] args) { RaceCar[] rc = new RaceCar((int)(Math.random()*100 + 1)); for (int i=0; i<rc.length; i++) { rc[i] = new RaceCar(i+1, "driver-"+(i+1)); } for (RaceCar c : rc) { System.out.printf("赛车信息:%s\n", c.toString()); } System.out.printf("赛车总数:%d\n", getCount()); } }
------解决方案--------------------
新人来看 来学习的 不回答
------解决方案--------------------
public class RaceCar {
private int carNo;
private String driverName;
private static int count=0;
public static int getCount(){
return count++;
}
public RaceCar(int carNo, String driverName,int count) {
super();
this.carNo = carNo;
this.driverName = driverName;
this.count = this.getCount();
}
@Override
public String toString() {
return "赛车编号:"+this.carNo+",车手名:"+this.driverName+",当前车数"+this.count;
}
public static void main(String[] args) {
RaceCar r1 = new RaceCar(132, "zhangsan",RaceCar.getCount());
RaceCar r2 = new RaceCar(456, "zhangsan",RaceCar.getCount());
RaceCar r3 = new RaceCar(457, "zhangsan",RaceCar.getCount());
RaceCar [] r ={r1,r2,r3};
for(int i=0;i<r.length;i++){
System.out.println(r[i]);
}
}
}
------解决方案--------------------
public class RaceCar{
private Integer carNo;
private String driverName;
private static int count=0;
public RaceCar(Integer carNo,String driverName){
this.carNo=carNo;
this.driverName=driverName;
count++;