Discuss / Java / 测试不成功 不知道问题出在哪里

测试不成功 不知道问题出在哪里

Topic source

呱呱

#1 Created at ... [Delete] [Delete and Lock User]

public static void main(String[] args) {

        // TODO: 给Person类增加一个静态字段count和静态方法getCount,统计实例的个数

        Person p1 = new Person("小明");

        System.out.println(Person.getCount()); // 1

        Person p2 = new Person("小红");

        System.out.println(Person.getCount()); // 2

        Person p3 = new Person("小军");

        System.out.println(Person.getCount()); // 3

    }

}

public class Person {

    public static int Count;

    public static Person(){

        Count ++;

    }

    public static int getCount(){

        return Count;

    }// TODO

    String name;

    public Person(String name) {

        this.name = name;

    }

}

勿忘我

#2 Created at ... [Delete] [Delete and Lock User]

“count++”写错地方了。。构造器哪有用“static”修饰的


  • 1

Reply