实 验 报 告
( 2017 / 2018学年 第2学期)
|
课程名称 |
JAVA语言程序设计 |
|||||
|
实验名称 |
Java集成开发环境的安装与使用、 Java变量、表达式与控制结构 |
|||||
|
实验时间 |
2018 |
年 |
4 |
月 |
11 |
日 |
|
指导单位 |
计算机学院软件教学中心 |
|||||
|
指导教师 |
许棣华 |
|||||
|
学生姓名 |
王利国 |
班级学号 |
B160209 |
|
学院(系) |
电子与光学工程学院,微电子学院 |
专 业 |
微电子科学与工程 |
|
实验名称 |
方法、数组和类 |
指导教师 |
许棣华 |
|
|||||||
|
实验类型 |
上机 |
实验学时 |
2 |
实验时间 |
2017.4.11 |
||||||
三、实验内容
1. 在前面实验二已定义的学生类Student的基础上,以Student类为父类,为学生类派生出一个子类为大学生类(CollegeStudent)。
CollegeStudent 类在学生类上增加一个专业(profession)数据属性;方法上增加获得专业和设置专业两个方法。并对超类中的toString( )方法进行重写,使得CollegeStudent类中的toString( )方法除了显示学生类的信息外,还要显示它的专业属性。
编写测试程序的主类。在主类中创建一个Student对象和CollegeStudent对象,并显示或修改这两个对象的属性值。
package lg.test; //测试类 public class Demo31 { public static void main(String[] args) { Student one = new Student( "16020912", "王宁宁","男" , 19 ); CollegeStudent two = new CollegeStudent( "16020913", "王利国","男" , 19 ,"微电子科学与工程" ); System.out.println("未进行修改的时候的属性值"); System.out.println(one.toString()); System.out.println(two.toString()); System.out.println("修改后的属性值"); one.setAge( 20 ); two.setProfession( "微电子" ); System.out.println(one.toString()); System.out.println(two.toString()); } } class Student { private String studentID; private String name; private String sex; private int age; private static int count; public static int getCount() { return count; } Student(String studentID, String name, String sex, int age) { this.studentID = studentID; this.name = name; this.sex = sex; this.age = age; } @Override public String toString() { return "Student{" + "studentID='" + studentID + '\'' + ", name='" + name + '\'' + ", sex='" + sex + '\'' + ", age=" + age + '}'; } //studen的set和get方法 public String getStudentID() { return studentID; } public void setStudentID(String studentID) { this.studentID = studentID; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public static void setCount(int count) { Student.count = count; } } //新建立的CoolegeStudent对象 class CollegeStudent extends Student{ private String profession; CollegeStudent(String studentID, String name, String sex, int age, String profession) { super( studentID, name, sex, age ); this.profession = profession; } //属性的get & set方法 public String getProfession() { return profession; } public void setProfession(String profession) { this.profession = profession; } @Override public String toString() { return "CollegeStudent{" + "profession='" + profession + '\'' + "studentID='" + super.getStudentID() + '\'' + ", name='" + super.getName() + '\'' + ", sex='" + super.getSex() + '\'' + ", age=" + super.getAge() + '}'; } }