急求此JAVA上机题答案
现有学生类及其子类研究生类,学生类包含私有成员变量name,credit;研究生类还有自己的私有变量postcredit;学生类包括方法getname()和getcrid...
现有学生类及其子类研究生类,学生类包含私有成员变量name ,credit; 研究生类还有自己的私有变量postcredit; 学生类包括方法getname ()和getcridit(),研究生类除继承上述方法外,还有自己的setpostcredit方法,现需创建一个研究生并设置其postcredit,另建立学生数组(研究生作为其一个元素),要求打印输出该学生数组的姓名和学分信息
展开
1个回答
2010-12-23
展开全部
public class Student{
String name ;
String credit ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCredit() {
return credit;
}
public void setCredit(String credit) {
this.credit = credit;
}
}
public class Graduate extends Student {
String postcredit;
public void setPostcredit(String postcredit) {
this.postcredit = postcredit;
}
public String getPostcredit() {
return postcredit;
}
}
public class Test {
public static void main(String[] args) {
Student[] stuarray=new Student[2];
Graduate gstu=new Graduate();
gstu.setName("Graduate_Name");
gstu.setCredit("Graduate_Credit");
gstu.setPostcredit("Graduate_Postcredit");
stuarray[0]=gstu;
stuarray[1]=new Student();
stuarray[1].setCredit("Student_Credit");
stuarray[1].setName("Student_Name");
for(int i=0;i<stuarray.length;i++){
System.out.println(stuarray[i].getName()+"-"+stuarray[i].getCredit());
}
}
}
String name ;
String credit ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCredit() {
return credit;
}
public void setCredit(String credit) {
this.credit = credit;
}
}
public class Graduate extends Student {
String postcredit;
public void setPostcredit(String postcredit) {
this.postcredit = postcredit;
}
public String getPostcredit() {
return postcredit;
}
}
public class Test {
public static void main(String[] args) {
Student[] stuarray=new Student[2];
Graduate gstu=new Graduate();
gstu.setName("Graduate_Name");
gstu.setCredit("Graduate_Credit");
gstu.setPostcredit("Graduate_Postcredit");
stuarray[0]=gstu;
stuarray[1]=new Student();
stuarray[1].setCredit("Student_Credit");
stuarray[1].setName("Student_Name");
for(int i=0;i<stuarray.length;i++){
System.out.println(stuarray[i].getName()+"-"+stuarray[i].getCredit());
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |