2007/01/03 | Student
类别(STUDY) | 评论(0) | 阅读(23) | 发表于 16:53

·实验说明:创建了Student类,数据域中声明了类型为一个先前创建的Name类的私有变量。main方法中使用两种创建Student对象的方法。

·代码
public class Student {
   private Name fullName;
   private String id;//学号
  
   public Student(){
    fullName=new Name();
    id="";
   }//end default constructor
  
   public Student(Name studentName,String studentId){
    fullName=studentName;
    id=studentId;
   }//end constructor
  
   public void setStudent(Name studentName,String studentId){
    setName(studentName);
    setId(studentId);
   }//end setStudent
  
   public void setName(Name studentName){
    fullName=studentName;
   }//end setName
  
   public Name getName(){
    return fullName;
   }//end getName
  
   public void setId(String studentId){
    id=studentId;
   }//end setId
  
   public String getId(){
    return id;
   }//end getId
  
   public String toString(){
    return id+" "+fullName.toString();
   }
  
   public static void main(String args[]){
    Student Jill=new Student();
    Jill.setStudent(new Name("Jill","Jones"), "8001");
    System.out.println(Jill.toString());
    Student Jill2=new Student(new Name("Jill","Jones"),"8001");
    System.out.println(Jill2.toString());
   }
}

·运行结果

8001 Jill Jones
8001 Jill Jones

0

评论Comments

日志分类
首页[90]
GUITAR[43]
JRUM[4]
PIANO[23]
HEART_ACHES[5]
STUDY[7]
FRAGMENT[8]