java实验报告5 类与对象

5.1 实验目的
掌握 Java 类的定义和使用,以及对象的声明和使用。
理解构造函数的概念和使用方法。
掌握类及其成员的访问控制符的使用。
5.2 实验要求

按要求编写一个 Java 应用程序:第一,定义一个表示学生的类 Student。这个类的属性有“学号”、“班号”、“姓名”、“性别”、“年龄”,方法有“获得学号”、“获得班号”、“获得性别”、“获得姓名”、“获得年龄”、“获得年龄”;第二,为类 Student 增加一个方法 public String toString( ),该方法把 Student 类的对象的所有属性信息组合成一个字符串以便输出显示。

5.3 实验指导

在 Student 类的构造方法中,参数中的局部变量的名字与成员变量的名字相同,于是 Student类的成员变量被隐藏,此时若想使用被隐藏的成员变量,必须使用关键字 this。

5.4 实验实现代码
class Student{
    
    
	 private long studentID;
	 private int classID;
	 private String name;
	 private String sex;
	 private int age;
	 public Student(long studentID, int classID, String name,
	 String sex, int age){
    
    
		 this.studentID = studentID;
		 this.classID = classID;
		 this.name = name;
		 this.sex = sex;
		 this.age = age;
	 }
	 public long getStudentID(){
    
    
		 return studentID;
	 }
	 public int getClassID(){
    
    
		 return classID;
	 }
	 public String getName(){
    
    
		 return name;
	 }
	 public String getSex(){
    
    
		 return sex;
	 }
	 public int getAge(){
    
    
		 return age;
	 }
	 public String toString(){
    
    
		 return "学号:"+getStudentID()+
		 "\n班号:"+getClassID()+
		 "\n姓名:"+getName()+
		 "\n性别:"+getSex()+
		 "\n年龄:"+getAge();
	  }
	 }
public class StudentExtendedDemo{
    
    
		public static void main(String[] args){
    
    
		Student s1=new Student(90221,2,"Tom","male",20);
		System.out.println(s1.toString());
	 }
}
5.5 拓展Student类

拓展上述实验中的 Student 类,添加以下方法:“设置学号”、“设置班号”、“设置性
别”、“设置姓名”、“设置年龄”、“设置年龄”,以及没有参数的构造方法。

实现代码
class Student{
    
    
	 private long studentID;
	 private int classID;
	 private String name;
	 private String sex;
	 private int age;
	 public Student(long studentID, int classID, String name,
	 String sex, int age){
    
    
		 this.studentID = studentID;
		 this.classID = classID;
		 this.name = name;
		 this.sex = sex;
		 this.age = age;
	 }
	 public long getStudentID(){
    
    
		 return studentID;
	 }
	 public int getClassID(){
    
    
		 return classID;
	 }
	 public String getName(){
    
    
		 return name;
	 }
	 public String getSex(){
    
    
		 return sex;
	 }
	 public int getAge(){
    
    
		 return age;
	 }
	 public void setStudentID(long studentID){
    
    
		 this.studentID = studentID;
	 }
	 public void setClassID(int classID){
    
    
		 this.classID = classID;
	 }
	 public void setName(String name){
    
    
		 this.name = name;
	 }
	 public void setSex(String sex){
    
    
		 this.sex = sex;
	 }
	 public void setAge(int age){
    
    
		 this.age = age;
	 }
	 public Student(){
    
    
	 }
	 public String toString(){
    
    
		 return "学号:"+getStudentID()+
		 "\n班号:"+getClassID()+
		 "\n姓名:"+getName()+
		 "\n性别:"+getSex()+
		 "\n年龄:"+getAge();
	  }
	 }
public class StudentExtendedDemo{
    
    
		public static void main(String[] args){
    
    
		Student s1=new Student(90221,2,"Tom","male",20);
		Student s2=new Student();
		s2.setStudentID(90222);
		s2.setClassID(3);
		s2.setName("Lisa");
		s2.setSex("female");
		s2.setAge(18);
		System.out.println(s1.toString());
		System.out.println(s2.toString());
	 }
}

猜你喜欢

转载自blog.csdn.net/buibuilili/article/details/108887636