- 小汽车工厂按照模块化方式生产汽车,低配汽车仅仅具有基本功能,舒适度低,中配汽车在低配基础上提供了满足部分个性化需求的功能,例如蓝牙等,而高配在中配的基础上增加了GPS定位和车联网等功能。
定义小汽车类Car,要求如下:(1)成员变量包括型号,速度;(2)定义一个空构造方法,一个能够初始化所有成员变量的构造方法;(3)必要的setter和getter方法;(4)功能方法包括:重写toString()方法返回有意义的汽车信息。
定义如下接口:(1)蓝牙接口Bluetooth具有启动关闭蓝牙功能;(2) GPS接口提供GPS定位功能;(3)车联网IOV接口提供车联网功能。
定义中配汽车MiddleCar实现蓝牙接口,高配汽车LuxuryCar实现蓝牙、GPS和IOV接口。
定义测试类,完成如下:(1)创建低配汽车对象,并输出该汽车的基本信息;(2)创建中配汽车对象,输出基本信息,并调用蓝牙功能;(3)创建高配汽车对象,利用GPS进行定位,建立与其它汽车的的IOV连接。 - 实验步骤
3.1 模拟小汽车模块化生产
【解题思路】:
采用适配器模式,该模式涉及三个角色:
(1) 源角色:需要被增减功能的类或者接口,本实验中的Car类;
(2) 目标角色:需要得到的接口,本实验中的Bluetooth、GPS和IOV接口;
(3) 适配器角色:在源角色的基础上实现了目标角色得到新的类,本实验中的MiddleCar和LuxuryCar类。
package 测试;
import java.awt.Point;
import java.util.Random;
class Car {
//1. 定义成员变量
private String brand;
private double speed;
//2. 定义构造方法
public Car() {}
public Car(String brand, double speed) {
this.brand = brand;
this.speed = speed;
}
//3. 必要的setter和getter方法
public double getSpeed() {
return this.speed;
}
public String getBrand() {
return this.brand;
}
//4. 功能方法
public String toString() {
StringBuilder sb=new StringBuilder();
sb.append("品牌:").append(this.brand);
sb.append(",速度:").append(this.speed);
return sb.toString();
}
}
//定义蓝牙接口
interface Bluetooth {
public void startBluetooth();//启动蓝牙
public void closeBluetooth();//关闭蓝牙
}
//定义GPS接口
interface GPS {
public Point getLocation();//获得位置
}
//定义IOV接口
interface IOV {
public void getConnetion(Car car);//与其它汽车建立IOV关系
}
//定义中配汽车
class MiddleCar extends Car implements Bluetooth{
//1. 定义成员变量
private boolean bluetoothState=false;//蓝牙状态
//2. 定义构造方法
public MiddleCar() { }
public MiddleCar(String brand, double speed) {
super(brand, speed);
}
//3. 定义setter和getter方法
public boolean getBluetoothState() {
return this.bluetoothState;
}
//4. 定义功能方法
@Override
public void startBluetooth() {//启动蓝牙
this.bluetoothState=true;
}
@Override
public void closeBluetooth() {//关闭蓝牙
this.bluetoothState=false;
}
public String toString() {
StringBuilder sb=new StringBuilder();
sb.append(super.toString());
sb.append(",蓝牙状态:").append(this.bluetoothState);
return sb.toString();
}
}
//定义高配汽车
class LuxuryCar extends MiddleCar implements GPS,IOV{
//1. 定义成员变量
private Point location;//位置
//2. 定义构造方法
public LuxuryCar() {
super();
}
public LuxuryCar(String brand, double speed) {
super(brand, speed);
}
//3. 定义setter和getter方法
//4. 定义功能方法
@Override
//instanceof 是 Java 的保留关键字。它的作用是测试它左边的对象是否是它右边的类的实例,返回 boolean 的数据类型。
public void getConnetion(Car car) {
if(car instanceof IOV) {//如果该汽车是IOV的子类实例,表明具有IOV功能
System.out.println(this.getBrand()+"建立与"+car.getBrand()+"的连接!");
}
else {
System.out.println(car.getBrand()+"没有物联网接口!");
System.out.println(this.getBrand()+"不能建立与"+car.getBrand()+"的连接!");
}
}
@Override
public Point getLocation() {
Point p=new Point();
double x=new Random().nextInt(50);
double y=new Random().nextInt(50);
//setLocation(double x, double y)将此点的位置设为指定的双精度坐标。
p.setLocation(this.getSpeed()+x, this.getSpeed()+y);
return p;
}
public String toString() {
StringBuilder sb=new StringBuilder();
sb.append(super.toString());
// getX() 以 double 精度返回此 Point2D 的 X 坐标。y也是这样
sb.append(",位置:").append(this.getLocation().getX());
sb.append(","+this.getLocation().getY());
return sb.toString();
}
}
//定义测试类
public class test {
public static void main(String[] args) {
System.out.println("------- 测试低配汽车 -------------");
Car gen=new Car("红旗L5",60);
//不用写to.string(),效果是相同的.当你传入的对象 “obj” 不为空 null,返回 该对象的 toString()的值
System.out.println(gen);
System.out.println("------- 测试中配汽车 -------------");
MiddleCar mc=new MiddleCar("红旗L5",80);
mc.closeBluetooth();
System.out.println(mc);
mc.startBluetooth();
System.out.println(mc);
System.out.println("------- 测试高配汽车 -------------");
LuxuryCar lc1=new LuxuryCar("红旗H5",120);
lc1.startBluetooth();
System.out.println(lc1);
lc1.getConnetion(mc);//红旗H5与红旗L5不能建立连接
LuxuryCar lc2=new LuxuryCar("宝马X5",100);
lc1.getConnetion(lc2);//红旗H5与宝马X5能建立连接
}
}
关于Java为什么使用到接口:
一个类只能继承一个父类,但是可以继承N个接口。
java之所以引入接口,就是为了弥补不能多继承的缺点,指明相关或者不相关类的多个对象的共同行为,跟抽象类很相似,可以说接口是更加抽象的抽象类。
- 接口不能被实例化。
- 接口中所有方法不能有主体。
- 一个类可以实现多个接口。
- 接口可以有变量,但一定是public static final 修饰,
- 接口中的方法只能是public修饰。
- 一个接口不能继承其他类,但是可以继承别的接口。