用java程序语言编程
定义一个学生类Student,要求其包含name(String类型,保存姓名)、birthday(上一节定义过的MyDate类型,用以保存生日)以及成员方法set、get...
定义一个学生类Student,要求其包含name(String类型,保存姓名)、birthday(上一节定义过的MyDate类型,用以保存生日)以及成员方法set、get等、print(打印对象信息)。在main方法,要求方法中通过“Student s1=new Student(); ”新建姓名为"default"、生日为1999年1月1日的对象,再通过类似“Student s2=new Student("张三",1989,7,7); ”的方法新建与你自己信息相同的学生对象,分别打印s1同s2的信息;添加新的构造函数,以便能够通过“Student s3=new Student("张三",new MyDate()); Student s4=new Student("张三",new MyDate(2009,4,1)); ”语句创建的两个新对象,并分别打印其信息;
最好是已经运行无误的,并且把运行的结果也一起给出,非常感谢 展开
最好是已经运行无误的,并且把运行的结果也一起给出,非常感谢 展开
展开全部
public class Zhidao {
public static void main(String args[]) {
Student s1 = new Student();
Student s2 = new Student("张三",1989,7,7);
Student s3=new Student("张三",new MyDate());
Student s4=new Student("张三",new MyDate(2009,4,1));
s1.print();
s2.print();
s3.print();
s4.print();
}
}
class MyDate {
private int year;
private int month;
private int day;
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public MyDate(){
this.year = 1999;
this.month = 1;
this.day =1;
}
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
}
class Student {
private String name;
private MyDate birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
public Student() {
this.name = "default";
this.birthday = new MyDate();
}
public Student(String name,int year,int month,int day) {
this.name = name;
this.birthday = new MyDate(year,month,day);
}
public Student(String name, MyDate birthday) {
super();
this.name = name;
this.birthday = birthday;
}
public void print() {
System.out.println("name=" + name + "\nbirthday=" + birthday.getYear() + "."+
birthday.getMonth() + "." + birthday.getDay() );
}
}
结果:
name=default
birthday=1999.1.1
name=张三
birthday=1989.7.7
name=张三
birthday=1999.1.1
name=张三
birthday=2009.4.1
public static void main(String args[]) {
Student s1 = new Student();
Student s2 = new Student("张三",1989,7,7);
Student s3=new Student("张三",new MyDate());
Student s4=new Student("张三",new MyDate(2009,4,1));
s1.print();
s2.print();
s3.print();
s4.print();
}
}
class MyDate {
private int year;
private int month;
private int day;
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public MyDate(){
this.year = 1999;
this.month = 1;
this.day =1;
}
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
}
class Student {
private String name;
private MyDate birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
public Student() {
this.name = "default";
this.birthday = new MyDate();
}
public Student(String name,int year,int month,int day) {
this.name = name;
this.birthday = new MyDate(year,month,day);
}
public Student(String name, MyDate birthday) {
super();
this.name = name;
this.birthday = birthday;
}
public void print() {
System.out.println("name=" + name + "\nbirthday=" + birthday.getYear() + "."+
birthday.getMonth() + "." + birthday.getDay() );
}
}
结果:
name=default
birthday=1999.1.1
name=张三
birthday=1989.7.7
name=张三
birthday=1999.1.1
name=张三
birthday=2009.4.1
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询