定义一个static变量count,用来存放student类总共创建的对象数。_百度知...
发布网友
发布时间:2024-03-30 21:41
我来回答
共4个回答
热心网友
时间:2024-09-30 04:08
package com;
public class Student {
private String no;
private String name;
private byte sex;
private int age;
private static int count;
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public byte getSex() {
return sex;
}
public void setSex(byte sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(String no, String name, byte sex, int age) {
super();
this.no = no;
this.name = name;
this.sex = sex;
this.age = age;
count++;
}
public Student() {
super();
count++;
}
public void print(){
System.out.println("Student对象一共创建了:"+count+"次");
}
}
//测试方法
public static void main(String[] args) {
Student[] stus = new Student[5];
for (int i = 0; i < stus.length; i++) {
stus[i] = new Student("NO_"+i,"学生_"+i,(byte)1,22);
stus[i].print();
}
}
热心网友
时间:2024-09-30 04:06
源码如下所示,请采纳
public class Student {
private static int count = 0;
private String name;
public Student() {
count++;
}
public void print() {
System.out.println("当前Count = : " + count);
}
public static void main(String[] args) {
Student student;
for (int i = 0; i < 100; i++) {
student = new Student();
student.print();
}
}
}
热心网友
时间:2024-09-30 04:10
学生类:
class Student{
//学号、姓名、性别、年龄
private String stuNum;//学号
private String stuName;
private String sex;
private int age;
public String getStuNum() {
return stuNum;
}
public void setStuNum(String stuNum) {
this.stuNum = stuNum;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
测试类:
public class TestStr {
public static int count=0;
public static void main(String[] args) {
Student[] students=new Student[10];
for(int i=0;i<students.length;i++){
students[i]=new Student();
count++;
System.out.println("学生对象个数为:"+count);
}
}
}
热心网友
时间:2024-09-30 04:04
你这分给得少了点