0720

来源:互联网 发布:天猫直播数据在哪里看 编辑:程序博客网 时间:2024/05/29 13:04

做一个GPSCar类,要求继承Car类,并实现GPS接口;

要求:

1.GPS定义getLocation()方法;

2.Car定义name,speed成员变量 并实现getCar()方法输出车的名字和速度

3.GPSCar类继承Car并实现GPS接口并重写getLocation(),

返回车的坐标(GPSCar.speed,GPSCar.speed).

重写CargetCar()方法,除了输出父类的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(Stringname) {

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 Carimplements 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();

}

 

}

 

0 0
原创粉丝点击