Java 编译
编写一个学生类,类里有一个标示姓名的属性和一个静态的标示总学生个数的属性,写一个构造方法,使每创建一个学生对象时学生个数自加,写一个输入结果的方法,程序运行的结果如下...
编写一个学生类,类里有一个标示姓名的属性和一个静态的标示总学生个数的属性,写一个构造方法,使每创建一个学生对象时学生个数自加,写一个输入结果的方法,程序运行的结果如下
展开
1个回答
展开全部
public class Student {
private String name;
private static int count = 0;
public Student(){
count ++;
}
public void setName(String name){
this.name = name;
}
public void print(){
System.out.println(name + "是第"+count+"个学生!");
}
public static void main(String[] args) {
Student stu1 = new Student();
stu1.setName("zhangsan");
stu1.print();
Student stu2 = new Student();
stu2.setName("lisi");
stu2.print();
Student stu3 = new Student();
stu3.setName("wangwu");
stu3.print();
}
}
private String name;
private static int count = 0;
public Student(){
count ++;
}
public void setName(String name){
this.name = name;
}
public void print(){
System.out.println(name + "是第"+count+"个学生!");
}
public static void main(String[] args) {
Student stu1 = new Student();
stu1.setName("zhangsan");
stu1.print();
Student stu2 = new Student();
stu2.setName("lisi");
stu2.print();
Student stu3 = new Student();
stu3.setName("wangwu");
stu3.print();
}
}
追问
class Student{
private String name;
private static int num;
public Student(String name){
this.name = name;
num ++;
System.out.println(this.name + "是第" + num + "个学生");
}
public static void main(String [] args){
new Student("zhangsan");
new Student("lisi");
new Student("wangwu");
}
}
你的太长了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询