做一个GPSCar类,要求继承Car类,并实现GPS接口;
要求:
1.GPS定义getLocation()方法;
2.Car定义name,speed成员变量 并实现 getCar()方法输出车的名字和速度
3.GPSCar类继承Car并实现GPS接口并重写getLocation(),
返回车的坐标(GPSCar.speed,GPSCar.speed).
重写Car的getCar()方法,除了输出父类的getCar()而且输出getLocation()
1、GPS接口
package com.xuyilong.gpscar;
public interface GPS { public void getLocation(); } |
2、Car类
package com.xuyilong.gpscar;
public class Car { private String name; private int speed; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSpeed() { return speed; } public void setSpeed(int speed) { this.speed = speed; } public void getCar(){ System.out.println("车的名字是:"+this.getName()+", 车的速度是:"+this.getSpeed()); } } |
3、GPSCar类
package com.xuyilong.gpscar;
public class GPSCar extends Car implements GPS{
@Override public void getCar() { super.getCar(); this.getLocation(); } @Override public void getLocation() { System.out.println("车的坐标:("+this.getSpeed()+", "+this.getSpeed()+")"); }
} |
4、TestGPSCar类
package com.xuyilong.gpscar;
import java.util.Scanner;
public class TestGPSCar {
public static void main(String[] args) { Car gpscar= new GPSCar(); Scanner sc=new Scanner(System.in); System.out.println("输入车名称:"); gpscar.setName(sc.nextLine()); System.out.println("输入车速度:"); gpscar.setSpeed(sc.nextInt()); sc.close(); gpscar.getCar(); }
} |