Java类和对象实验

  1.  学生类

学生类的创建与使用:

  1. 将Student类创建在myStudent包中

  2. 创建一个Student类,包括的属性有学号、班级、姓名、性别、出生年月等,且都是private类型。

  3. 声明三个构造函数,初始化对象的所有域。

  4. 声明分别获得与修改各属性的public方法(set、get方法)。

  5. 声明toString()方法,把该类的所有域信息组合成一个字符串。

  6. 在类中声明统计班级人数的私有域count,以及得到班级总人数的public方法。(在构造方法中进行Student个数增加)

  7. 在包myStudent外,创建测试主类StudentTest。在主类中,创建两个Student类的对象,输出对象的所有域信息;修改对象的姓名并输出;比较两个对象的年龄大小,输出年龄较大的对象。

    Student.Java的内容:

    package MyStudent;//Student类创建在myStudent包中
    
    import java.util.Arrays;
    
    //创建一个Student类,包括的属性有学号、班级、姓名、性别、出生年月等,且都是private类型
    
    public class Student {
    
       private int num;
    
       private int clas;
    
       private String name;
    
       private String gender;
    
       private int[] birthday;
    
       private static int count;//统计班级人数的私有域count
    
       ///声明三个构造函数,初始化对象的所有域
    
       public Student(){
    
          this.num=0;
    
          this.clas=0;
    
          this.name=null;
    
          this.gender=null;
    
          this.birthday=null;
    
          Student.count++;
    
       }
    
       public Student(int nu,int cla,String nam,String gen,int[] bir){
    
          this.num=nu;
    
          this.clas=cla;
    
          this.name=nam;
    
          this.gender=gen;
    
          this.birthday=bir;
    
          Student.count++;
    
       }
    
       public Student(int nu,String nam){
    
          this(nu,0,nam,null,null);
    
          Student.count++;
    
    
    
       }
    
       //声明分别获得与修改各属性的public方法(set、get方法
    
       public void setNum(int nu) {
    
          this.num=nu;
    
       }
    
       public void setClas(int cla) {
    
          this.clas=cla;
    
       }
    
       public void setName(String nam) {
    
          this.name=nam;
    
       }
    
       public void setGender(String Gend) {
    
          this.gender=Gend;
    
       }
    
       public void setBirthday(int[] bir) {
    
          this.birthday=bir;
    
       }
    
       public int  getNum() {
    
          return this.num;
    
       }
    
       public int  getClas() {
    
          return this.clas;
    
       }
    
       public String  getName() {
    
          return this.name;
    
       }
    
       public String  getGender() {
    
          return this.gender;
    
       }
    
       public int[]  getBirthday() {
    
          return this.birthday;
    
       }
    
       //声明toString()方法,把该类的所有域信息组合成一个字符串。
    
       public String toString() {
    
          String s;
    
          String s1=String.valueOf(this.num);
    
          String s2=String.valueOf(this.clas);
    
          s="Student:"+"\n"+"num:"+s1+"\n"+"clas:"+s2+"\n"+"name:"+this.name+"\n"+"gender:"+this.gender+"\n"+"birthday"+Arrays.toString(birthday)+"\n";
    
          return s;
    
       }
    
       //班级总人数的public方法
    
       public int sum() {
    
          return Student.count;
    
       }
    
    }

    StudentTest.java的内容:

    package MyStudent;//import java.util.Scanner;
    
    public class StudentTest {
    
       static void display(Student st) {//Scanner in=new Scanner(System.in);
    
          System.out.printf("%s",st.toString());
    
       }
    
       public static Student older(Student s1,Student s2) {
    
          int b1[]=s1.getBirthday();
    
          int b2[]=s2.getBirthday();
    
          for(int i=0;i<3;i++) {
    
             if(b1[i]>b2[i]) {
    
                return s2;
    
                //break;
    
             }else if(b1[i]<b2[i]){
    
                return s1;//st1.birthday[i]>st2.birthday[i]
    
             }
    
          }
    
          return s1;
    
       }
    
       public static void main(String[]args) {
    
          //创建两个Student类的对象
    
          Student st1=new Student(113,1,"li","man",new int[]{2003,2,15});
    
          Student st2=new Student(114,2,"wang","woman",new int[]{2004,6,8});
    
          //输出对象的所有域信息
    
          display(st1);
    
          display(st2);
    
          //修改对象的姓名并输出
    
          st1.setName("libai");
    
          st2.setName("wangwei");
    
          System.out.println(st1.getName());
    
          System.out.println(st2.getName());
    
          //比较两个对象的年龄大小,输出年龄较大的对象。
    
          Student sstt=older(st1,st2);
    
          System.out.println(sstt.toString());
    
       }
    
    }

    输出结果:

  • 14
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值