本次实验考查知识点:
(1)Java 语言中的类继承(extends)
(2)Java 语言中的类封装(private)
(3)Java 语言中的构造方法重写(override)
(4)Java 抽象类与接口
实验题目:
1、定义一个名为 Student 的类,它继承 Person 类(Person 类中包含 String 类型
的 name 和 int 类型的 age),其中定义 sno(表示学号)和 major(表示专业)两
个成员变量和封装这两个变量的方法。编写主程序,检查新建类中的所有变量和
方法。
//Person.java
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
//Student.java
public class Student extends Person{
private String sno;
private String major;
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
}
//测试类 StudentTest.java
public class StudentTest {
public static void main(String[] args) {
Student student = new Student();
student.setName("victor");
student.setAge(20);
student.setSno("6020203112");
student.setMajor("计算机科学与技术");
System.out.println(student.getName()+","+student.getAge()+","+student.getSno()+","+student.getMajor());
}
}
2、设计一个汽车类 Auto,其中包含一个表示速度的 double 型的成员变量 speed 和表示启动的 start()方法、表示加速的 speedUp()方法以及表示停止的 stop()方法。 再设计一个 Auto 类的子类 Bus 表示公共汽车,在 Bus 类中定义一个 int 型的、 表示乘客数量的成员变量 passenger,另外定义两个方法 gotOn()和 gotOff()表示 乘客上车和下车。编写一个应用程序,测试 Bus 类的使用。
//Auto.java
public class Auto {
private double speed;
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public void start()
{
System.out.println("汽车开始启动");
}
public void speedUp(double speed)
{
System.out.println("当前车速:"+(this.speed+speed));
}
public void stop()
{
System.out.println("汽车开始停止");
}
}
//Bus.java
public class Bus extends Auto {
private int passenger;
public int getPassenger() {
return passenger;
}
public void setPassenger(int passenger) {
this.passenger = passenger;
}
public void gotOn(int passenger) {
this.passenger = this.passenger + passenger;
System.out.println("车上乘客数为:" + this.passenger);
}
public void gotOff(int passenger) {
if (passenger <= this.passenger) {
System.out.println("原来乘客数为" + this.passenger + ",此次下车人数为" + passenger + ",车上剩余人数:" + ( this.passenger = this.passenger - passenger));
} else {
System.out.println("下车人数不符合实际情况");
}
}
}
//测试类 BusTest.java
public class BusTest {
public static void main(String[] args) {
Bus bus = new Bus();
bus.setSpeed(15.5);
bus.speedUp(20);
bus.setPassenger(5);
bus.gotOn(3);
bus.gotOn(6);
bus.gotOff(2);
bus.gotOff(13);
}
}
3、定义一个名为 Cuboid 的长方体类,使其继承 Rectangle 类(Rectangle 类中包 含 double 类型的 length 和 width),其中包含一个表示高度的 double 型成员变量 height,定义一个构造方法 Cuboid(double length,double width,double height)和 一个求长方体体积的 volume()方法。编写一个应用程序,在其中求一个长、宽、 高分别为 10、5、2 的长方体的体积。
//Rectangle.java
public class Rectangle {
private double length;
private double width;
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double area() {
return length * width;
}
}
//Cuboid.java
public class Cuboid extends Rectangle{
private double height;
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public Cuboid(double length,double width,double height) {
super(length,width);
this.height = height;
}
public double volume()
{
return area() * getHeight();
}
}
// 测试类 CuboidTest.java
public class CuboidTest {
public static void main(String[] args) {
Cuboid cuboid = new Cuboid(10,5,2);
System.out.println("长、宽、高分别为"+ cuboid.getLength()+"、"+cuboid.getWidth()+"、"+cuboid.getHeight()+"的长方体的体积="+cuboid.volume());
}
}
4、定义一个表示圆的 Circle 类,其中有表示半径的 double 型的属性 radius,计 算圆周长的 perimeter 方法和计算面积的 area 方法。在 Circle 类的基础上,定义 圆柱体 Cylinder 类和球体 Sphere 类,它们分别具备计算表面积的 area 方法和体 积的 volume 方法,编写测试类进行测试。
//Circle.java
public class Circle {
private double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double perimeter()
{
return Math.PI * radius * 2;
}
public double area()
{
return Math.PI * radius * radius;
}
}
//Cylinder.java
public class Cylinder extends Circle{
private double height;
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double volume()
{
return super.area() * height;
}
@Override
public double area() {
return super.area() * 2 + height * perimeter() ;
}
}
//Shpere.java
public class Sphere extends Circle{
public double volume()
{
return 4.0 / 3 * Math.PI * Math.pow(this.getRadius(),3);
}
public double area()
{return super.area() * 4;
}
}
//Test.java
public class Test {
public static void main(String[] args) {
Cylinder cylinder = new Cylinder();
cylinder.setRadius(1);
cylinder.setHeight(2);
System.out.println("volume="+cylinder.volume());
System.out.println("area="+cylinder.area());
Sphere sphere = new Sphere();
sphere.setRadius(2);
System.out.println("volume="+sphere.volume());
System.out.println("area="+sphere.area());
}
}
5、编写一个银行账户类,类的构成包括:
(1)数据成员:用户的账号名称、用户的账户余额;
(2)方法包括:开户(设置账户名称及余额),利用构造方法完成;
(3)查询余额。
//Account.java(额外增加了存款和取款方法)
public class Account {
private String name;//假设账户名称是唯一标识
private double money;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Account(String name, double money) {
this.name = name;
this.money = money;
}
//余额查询方法
public void getBalance() {
System.out.println("余额:" + money);
}
//取款方法
public void withdraw(int money) {
if (this.money >= money) {
this.money = this.money - money;
System.out.println("取钱成功!");
} else {
System.out.println("余额不足!");
}
}
//存款方法
public void deposit(int money) {
this.money = this.money + money;
System.out.println("存款成功!");
}
}
//测试类 AccountTest.java
public class AccountTest {
public static void main(String[] args) {
Account account = new Account("jianghui",20);
account.getBalance();
account.deposit(2500);
account.getBalance();
account.withdraw(800);
account.getBalance();
}
}
6、接口应用:接口继承与接口实现 具体要求:
(1)Biology 生物接口中定义了 breathe()抽象方法;
(2)Animal 动物接口继承了 Biology 接口,增加 eat()和 sleep()两个抽象方法;
(3)Human 人类接口继承了 Animal 接口,增加 think()和 learn()两个抽象方法;
(4)定义一个普通人类 Person 实现 Human 接口,并进行测试。 Billogy 接口、Animal 接口、Human 接口
//Billogy 接口、Animal 接口、Human 接口
public interface Biology {
public abstract void breathe();
}
public interface Animal extends Biology {
public abstract void eat();
public abstract void sleep();
}
public interface Human extends Animal{
public abstract void think();
public abstract void learn();
}
//实现类 Person.java
public class Person1 implements Human{
@Override
public void eat() {
System.out.println("干饭人干饭魂");
}
@Override
public void sleep() {
System.out.println("早睡早起身体好");
}
@Override
public void breathe() {
System.out.println("全世界有最清新氧气");
}
@Override
public void think() {
System.out.println("我思故我在");
}
@Override
public void learn() {
System.out.println("知识改变命运");
}
}
7、动态多态应用:企业员工工资管理 具体要求:某公司的雇员分为以下若干类:
(1)Employee 员工类,包含受保护的属性 name(姓名)、birthDate(出生日期), public double getSalary(Employee e)方法(该方法根据传入不同类别的员工,工资 计算方法则返回其本月的工资);
(2)SalariedEmployee 类继承 Employee 类,代表领取固定工资的员工,包含私 有属性 salary(固定工资);
(3)HourlyEmployee 类继承 Employee 类,代表按小时拿工资的员工,包含私有属性 hourSalary(每小时的工资)、hours(每月工作的小时数)。工资计算方法: 月工资=hourSalary * hours,每月工作超出 160 小时的部分,按照 1.5 倍小时工资 发放。
(4)SalesEmployee类继承Employee类,代表销售人员,包含受保护的属性sale(月 销售额),commissionRate(提成率)。工资计算方法:月工资=sale * commissionRate。
(5)BasePlusSalesEmployee 类继承 SaleEmployee 类,代表有固定底薪的销售人 员,工资由底薪加上销售提成部分组成。其私有属性 basicSalary(底薪)。工资计 算方法:月工资=basicSalary + sale * commissionRate。
(6) 根据 要 求创 建 SalariedEmployee 、HourlyEmployee、 SalesEmployee、 BasePlusSalesEmployee 类的对象各一个,并计算某个月这 4 个对象的工资。
//Employee.java
public class Employee {
protected String name;
protected String birthDate;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBirthDate() {
return birthDate;
}
public void setBirthDate(String birthDate) {
this.birthDate = birthDate;
}
public Employee(String name, String birthDate) {
this.name = name;
this.birthDate = birthDate;
}
public void getSalary(Employee e) {
//员工类型为固定工资型
if (e instanceof SalariedEmployee) {
SalariedEmployee salariedEmployee = (SalariedEmployee) e;
}
System.out.println("姓名:"+salariedEmployee.getName()+",出生日期:"+salariedEmployee.getBirthDate()+",salary=" + salariedEmployee.getSalary());
//员工类型为小时工型
if (e instanceof HourlyEmployee) {
HourlyEmployee hourlyEmployee = (HourlyEmployee) e;
System.out.println("姓名:"+hourlyEmployee.getName()+",出生日期:"+hourlyEmployee.getBirthDate()+",salary=" + hourlyEmployee.getSalary());
}
/*员工类型为销售提成类型(这里之所以要进行两方面判断,主要是 BasePlusSalesEmployee 与 SalesEmployee 之间还存在继承关系,
也就是说一个 BasePlusSalesEmployee 也属于 SalesEmployee 的一个实例,在进行 BasePlusSalesEmployee instanceof SalesEmployee 和
BasePlusSalesEmployee instanceof BasePlusSalesEmployee 判断时,都将是成立的,会导致结果输出两次,因此,在这里加了一个
!(e instanceof BasePlusSalesEmployee)判断,用来说明只是存粹的提成类型员工)
*/
if (e instanceof SalesEmployee && !(e instanceof BasePlusSalesEmployee)) {
SalesEmployee salesEmployee = (SalesEmployee) e;
System.out.println("姓名:"+salesEmployee.getName()+",出生日期:"+salesEmployee.getBirthDate()+",salary=" + salesEmployee.getSalary());
}
//员工类型为底薪+销售提成型
if (e instanceof BasePlusSalesEmployee) {
BasePlusSalesEmployee basePlusSalesEmployee =(BasePlusSalesEmployee) e;
System.out.println("姓名:"+basePlusSalesEmployee.getName()+",出生日期:"+basePlusSalesEmployee.getBirthDate()+",salary=" +basePlusSalesEmployee.getSalary());
}
}
}
//SalariedEmployee.java
public class SalariedEmployee extends Employee{
public SalariedEmployee(String name,String birthDate) {
super(name,birthDate);
}
public double getSalary() {
return 4500; //假设员工固定工资 4500 元/月
}
}
//HourlyEmployee.java
public class HourlyEmployee extends Employee{
private double hourSalary;
private double hours;
public double getHourSalary() {
return hourSalary;
}
public void setHourSalary(double hourSalary) {
this.hourSalary = hourSalary;
}
public double getHours() {
return hours;
}
public void setHours(double hours) {
this.hours = hours;
}
public double getSalary()
{
return getHourSalary() * getHours();
}
public HourlyEmployee(String name,String birthDate,double hourSalary, double
hours) {
super(name,birthDate);
this.hourSalary = hourSalary;
this.hours = hours;
}
}
//SalesEmployee.java
public class SalesEmployee extends Employee{
protected double sale;//销售额
protected double commissionRate;//提成率
public double getSalary()
{
return sale * commissionRate;
}
public SalesEmployee(String name,String birthDate,double sale, double
commissionRate) {
super(name,birthDate);
this.sale = sale;
this.commissionRate = commissionRate;
}
}
//BasePlusSalesEmployee.java
public class BasePlusSalesEmployee extends SalesEmployee{
private double basicSalary;//底薪
public double getSalary()
{
return basicSalary + super.getSalary();
}
public BasePlusSalesEmployee(String name,String birthDate,double sale, double
commissionRate, double basicSalary) {
super(name,birthDate,sale, commissionRate);
this.basicSalary = basicSalary;
}
}
//测试类 EmployeeTest.java
public class EmployeeTest {
public static void main(String[] args) {
Employee employee = new Employee("测试人姓名","测试人生日");
SalariedEmployee salariedEmployee = new SalariedEmployee("小李","1992-05-15");
HourlyEmployee hourlyEmployee = new HourlyEmployee("小王","1990-6-30",120,4.5);
SalesEmployee salesEmployee = new SalesEmployee("小张 ","1993-11-27",100000,0.08);
BasePlusSalesEmployee basePlusSalesEmployee = new BasePlusSalesEmployee("小陈","1988-08-17",100000,0.06,1200);
Employee[] employeeSet = {salariedEmployee,hourlyEmployee,salesEmployee,basePlusSalesEmployee};
for(int i = 0;i < employeeSet.length;i++)
{
employee.getSalary(employeeSet[i]);
}
}
}