一、实验目的
1、理解面向对象的基本概念,掌握类与对象的定义和使用;
2、掌握构造方法的概念,并学会如何定义和使用构造方法;
3、掌握this关键字和static关键字的作用,并学会如何使用这些关键字;
4、理解封装的基本原理,并会使用封装原理解决问题;
5、理解类的继承特性,掌握类的继承实现方式,并掌握其用法;
6、理解抽象类和接口的基本概念,掌握通过类实现接口的方法;
7、掌握super关键字和final关键字的作用,并学会如何使用这些关键字。
二、预习与准备
1、类与对象的定义语法格式,对象的使用方法;
2、构造方法的定义语法格式,构造方法的重载定义以及构造方法的使用方法;
3、封装的基本原理;
4、this、static关键字的作用和具体用法;
5、类的继承实现方法,方法的重写;
6、接口的定义和使用。
三、实验内容
1、设计一个类Student,该类包括姓名、学号和成绩。设计一个方法,按照成绩从高到低的顺序输出姓名、学号和成绩信息。
参考代码:
class Student {
String name;
int num;
int score;
public Student() {
super();
}
public Student(String name, int num, int score) {
super();
this.name = name;
this.num = num;
this.score = score;
}
@Override
public String toString() {
return "Student [name=" + name + ", num=" + num + ", score=" + score + "]";
}
public static void sort(Student[] stus) {
Student stu;
for(int i=0;i<stus.length-1;i++) {
for(int j=0;j<stus.length-i-1;j++) {
if(stus[j].score<stus[j+1].score) {
stu=stus[j];
stus[j]=stus[j+1];
stus[j+1]=stu;
}
}
}
}
}
public class Test1 {
public static void main(String[] args) {
Student[] stu =new Student[5];
stu[0]=new Student("zhangsan",100,89);
stu[1]=new Student("lisi",101,78);
stu[2]=new Student("mike",102,67);
stu[3]=new Student("tom",103,99);
stu[4]=new Student("bob",104,85);
Student.sort(stu);
for(Student s: stu) {
System.out.println(s.toString());
}
}
}
2、按要求编写一个Java应用程序:
(1)定义一个类,描述一个矩形,包含有长、宽两种属性和计算面积方法。
(2)编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性和计算体积的方法。
(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。
参考代码:
class Rectangle{
double Rectangle_long;
double Rectangle_wide;
public Rectangle() {
super();
}
public double getRectangle_long() {
return Rectangle_long;
}
public void setRectangle_long(double rectangle_long) {
Rectangle_long = rectangle_long;
}
public double getRectangle_wide() {
return Rectangle_wide;
}
public void setRectangle_wide(double rectangle_wide) {
Rectangle_wide = rectangle_wide;
}
public double area() {
return this.Rectangle_long*this.Rectangle_wide;
}
}
class Test extends Rectangle{
double high;
public Test() {
super();
}
public double getHigh() {
return high;
}
public void setHigh(double high) {
this.high = high;
}
public double volume() {
return super.Rectangle_long*super.Rectangle_wide*this.high;
}
}
public class Test2 {
public static void main(String[] args) {
Rectangle rt=new Rectangle();
rt.setRectangle_long(2);
rt.setRectangle_wide(3);
System.out.println("矩形的长为:"+rt.getRectangle_long()+",宽为:"+rt.getRectangle_wide()+",面积为:"+rt.area());
Test t=new Test();
t.setRectangle_long(2);
t.setRectangle_wide(3);
t.setHigh(4);
System.out.println("矩形的长为:"+t.getRectangle_long()+",宽为:"+t.getRectangle_wide()+",高为:"+t.getHigh()+",体积为:"+t.volume());
}
}
3、某汽车租赁公司出租多种车辆,车辆信息包括车牌号、品牌、颜色、里程信息,租金情况如下:
轿车 客车
车型 别克商务舱GL8 宝马550i 别克林荫大道 座位 <=16座 >16座
日租费(元/天) 600 500 300 日租费(元/天) 800 1500
编写程序根据顾客的选择(键盘输入)实现计算5日租赁价。
参考代码:
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean flag;
System.out.println("(true"+"代表轿车,"+"false"+"代表客车)->"+"请选择true或者false"+":");
while (true) {
flag = sc.nextBoolean();
if (flag) {
System.out.println("请选择类型(别克商务舱GL8,宝马550i,别克林荫大道):");
vehicle v = new car(sc.next());
System.out.println("请选择租聘天数:");
System.out.println("费用为:"+v.rent(sc.nextInt())+"元");
}else{
System.out.println("请选择需要几个座位:");
vehicle v = new bus(sc.nextInt());
System.out.println("请选择租聘天数:");
System.out.println("费用为:"+v.rent(sc.nextInt())+"元");
}
}
}
}
abstract class vehicle {
String brand;
String id;
String color;
double mileage;
abstract int rent(int day);
}
class car extends vehicle {
String type;
public car(String type) {
super();
this.type = type;
}
int rent(int day) {
if (type.equals("别克商务舱GL8")) {
return 600 * day;
} else if (type.equals("宝马550i")) {
return 500 * day;
} else {
return 300 * day;
}
}
}
class bus extends vehicle {
int num;
public bus(int num) {
super();
this.num = num;
}
int rent(int day) {
if (num<=16) {
return 800 * day;
}else {
return 1500 * day;
}
}
}
四、实验总结与体会
本次实验在第一题中用到了冒泡排序算法,采用两轮循环,进行依次比较。第二题中用了继承,在计算体积时可以采用父类的面积算法求出底面积,然后乘以高度,求出长方体的体积。第三题同样使用了继承,继承来自父类的租聘天数方法,通过输入的类型不同,选择不同的算法。通过本次实验更加了解了类与对象的用法,构造方法的定义语法格式以及对象的封装,也更加熟练的使用this和static关键字。