//测试类!
public class StudentTest {
public static void main(String[] args) {
//测试1:
Studen s=new Studen();//创建对象!
System.out.println(s);//测试打印对象属性!
System.out.println("\n----------------分割线------------------\n");
//测试2:
Studen s1=new Studen("张三","2017-12-07","1900/10/10","机电",100);
s1.study();
System.out.println(s1);
}
}
//学生类!
class Studen{
private String name;//姓:
private String number;//号:
private String day;//生;
private String spe;//专!
private int age;//年龄!
Studen(){//空参构造!
this("无名氏","2017-12-13","1999/10/27","本课",18);
}//形参构造!
public Studen(String name, String number, String day, String spe, int age) {
super();
this.name = name;
this.number = number;
this.day = day;
this.spe = spe;
this.age = age;
}//修改年龄!
public void setAge(int age) {
this.age = age;
}//修改出生!
public void setDay(String day) {
this.day = day;
}//修改专业!
public void setSpe(String spe) {
this.spe = spe;
}//修改姓名!
public void setName(String name) {
this.name = name;
}//修改学号!
public void setNumber(String number) {
this.number = number;
}//获取学号!
public String getNumber() {
return number;
}//获取出生!
public String getDay() {
return day;
}//获取专业!
public String getSpe() {
return spe;
}//获取年龄!
public int getAge() {
return age;
}//获取姓名!
public String getName() {
return name;
}//学习方法!
public void study() {
System.out.println(name+",在学习:"+spe);
}
public String toString() {
return "姓名:"+name+";年龄:"+age+";专业:"+spe+";学号:"+number;
}
}
手撸出来的,觉得不错就采纳(当然,最好自己也去打,对你有好处)
public class Student {
private String name;
private int stuNum;
private String birthday;//出生日期Date类型也好
private String profession;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getStuNum() {
return stuNum;
}
public void setStuNum(int stuNum) {
this.stuNum = stuNum;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
}
我在main中声明了2个学生,截图如下:
public class Student {
// 姓名,学号,出生日期,专业
private String name;
private int id;
// 这里生日可以选择字符串形式。看你需求
private Date birthday;
private String prof;
public Student() {
}
public Student(String name, int id, Date birthday, String prof) {
this.name = name;
this.id = id;
this.birthday = birthday;
this.prof = prof;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getProf() {
return prof;
}
public void setProf(String prof) {
this.prof = prof;
}
}
直接给出具体代码
public class StudentDto implements Serializable {
private static final long serialVersionUID = -3824346741464362152L;
String student_id;
String name;
String birthday;
String professiona;
public StudentDto(String student_id, String name, String birthday,
String professiona) {
super();
this.student_id = student_id;
this.name = name;
this.birthday = birthday;
this.professiona = professiona;
}
@Override
public String toString() {
return "StudentDto [student_id=" + student_id + ", name=" + name
+ ", birthday=" + birthday + ", professiona=" + professiona
+ "]";
}
public String getStudent_id() {
return student_id;
}
public void setStudent_id(String student_id) {
this.student_id = student_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getProfessiona() {
return professiona;
}
public void setProfessiona(String professiona) {
this.professiona = professiona;
}
}
该方法主要提供了,问题中所需要的具体参数和必要的set,get方法,已经重构tostring和构造方法,防止远程调用增加了序列化数据。
整体其实没有什么难度,希望楼主多看看书,学习阶段不要老copy代码,对你没好处。
创建实体类主要是参数的命名合理,类型合理以及提供一些必要的方法。
package cn.com.avivacofco.test;
public class Student {
private String name;
private String sNo;
private String birth;
private String specialities;
public Student(String name, String sNo, String birth, String specialities) {
this.name = name;
this.sNo = sNo;
this.birth = birth;
this.specialities = specialities;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getsNo() {
return sNo;
}
public void setsNo(String sNo) {
this.sNo = sNo;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
public String getSpecialities() {
return specialities;
}
public void setSpecialities(String specialities) {
this.specialities = specialities;
}
public static void main(String[] args) {
Student stu1 = new Student("Tom","10001","1990-03-12","电子商务");
Student stu2 = new Student("Jack","10002","1990-04-12","物流管理");
}
}