实验目的:
- 掌握成员变量的初始化顺序
- 掌握成员的各种访问权限
- 掌握类的继承
- 掌握方法重写
实验内容:
- 创建一个包,在该包中设计一个名为Person的类和他的2个子类Student和Employee。
- Person类有姓名(声明时设置为“”)、地址(声明表达式时设置为北师大,初始化模块设置为金凤路)、电话号码和电子邮件地址。
- 学生类有班级状态(大一、大二、大三、大四),将班级状态定义为静态常量。
- 雇员类包括办公室(声明表达式时设置为励耘楼,初始化模块设置为木铎楼)、工资和受聘日期(声明时设置为当前日期)。给每个类定义toString方法,每个属性成员的访问器,所有属性都定义为私有的。给每个类添加相应的不带参数构造器和带参数构造函数
- 编写测试程序,创建包含2个学生Student数组和和包含2个雇员Employee,并按自己定义的格式显示相关信息
- 提示:按照指定日期初始化Date的格式:new Date(2016-1900,3,,设置的日期为2016年4月5日,显示可用SimpleDateFormat格式化(具体操作自己上网查)
- 显示结果参照下图:
- 输入下面的程序,观察程序的输出结果,并在子程序后用注释语句说明为什么?
1)swap1和 swap2函数是否可以写成非静态,为什么?在程序后面用注释语句回答此问题
2) 将radius数据成员修改为private,程序会发生什么问题,请修改程序
package bnuz.edu.cn.person;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
class Person {
private String name="";
private String address="北师大";
private String phoneNumber;
private String email;
{
address="金凤路";
}
public Person(){
System.out.println("Person不带参数构造函数");
}
public Person(String name, String address, String phoneNumber, String email) {
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
this.email = email;
System.out.println("Person带参数构造函数");
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getEmail() {
return email;
}
@Override
public String toString() {
return "姓名:"+name+"\n地址:"+address+"\n电话号码:"+phoneNumber+"\n邮件地址:"+email+"\n";
}
}
class Student extends Person {
public static final int FRESHMAN = 1;
public static final int SOPHOMORE = 2;
public static final int JUNIOR = 3;
public static final int SENIOR = 4;
private int status;
public Student(){
System.out.println("Student不带参数构造函数");
}
public Student(String name, String address, String phoneNumber, String email,int status) {
super(name, address, phoneNumber, email);
this.status = status;
System.out.println("Student带参数构造函数");
}
@Override
public String toString() {
return "学生:\n "+super.toString()+"年级"+status+"\n";
}
}
class Employee extends Person {
private String office= "励耘楼";
private int salary;
private Date dateHired=new Date();
{
office="木铎楼";
}
public Employee(){
System.out.println("Employee不带参数构造函数");
}
public Employee(String name, String address, String phoneNumber, String email, String office, int salary, Date dateHired) {
super(name, address, phoneNumber, email);
this.office = office;
this.salary = salary;
this.dateHired = dateHired;
System.out.println("Student带参数构造函数");
}
@Override
public String toString() {
String d=new SimpleDateFormat("yyyy-MM-dd").format(dateHired);
return "雇员:\n"+super.toString()+"办公地址:"+office+"\n工资:"+salary+"\n入职时间:"+d;
}
}
package bnuz.edu.cn.person;
import java.util.Date;
public class PersonTest {
public static void main(String[] args) {
Student [] s=new Student[2];
s[0]=new Student();
System.out.println(s[0].toString());
s[1]=new Student("张平","粤华","13924001234","3276@qq.com",Student.SENIOR);
System.out.println(s[1].toString());
Employee []e=new Employee[2];
e[0]=new Employee();
System.out.println(e[0].toString());
e[1]=new Employee("严华","金凤路北师大","13924703000","78964@qq.com","励耘楼",5000,new Date(2016-1900,3,5));
System.out.println(e[1].toString());
}
}
public class Test {
public static void main(String []args){
Circle c1=new Circle(1);
Circle c2=new Circle(2);
swap1(c1,c2);
System.out.println("After swap1: c1="+c1.radius+" c2="+c2.radius);
swap2(c1,c2);
System.out.println("After swap2: c1="+c1.radius+" c2="+c2.radius);
}
public static void swap1(Circle x,Circle y){
Circle temp=x;
x=y;
y=temp;
}
public static void swap2(Circle x,Circle y){
double temp=x.radius;
x.radius=y.radius;
y.radius=temp;
}
}
class Circle{
private double radius;
Circle(double r){
radius=r;
}
}
public class Test {
public static void main(String []args){
Circle c1=new Circle(1);
Circle c2=new Circle(2);
swap1(c1,c2);
System.out.println("After swap1: c1="+c1.getRadius()+" c2="+c2.getRadius());
swap2(c1,c2);
System.out.println("After swap2: c1="+c1.getRadius()+" c2="+c2.getRadius());
}
public static void swap1(Circle x,Circle y){
Circle temp=x;
x=y;
y=temp;
}//该函数只是交换的2个实参对象拷贝指向的内容进行了交换,c1,c2的值没有发生变化
public static void swap2(Circle x,Circle y){
double temp=x.getRadius();
x.setRadius(y.getRadius());
y.setRadius(temp);
}//该函数的形参是实参的引用拷贝,和实参指向同样的内容,所以修改形参指向的内容,也就是修改实参指向的内容
}
class Circle{
private double radius;
Circle(double r){
radius=r;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
//两个swap函数不能是非静态,因为静态函数main不能调用非静态函数