package tmp;
import java.awt.Point;
public class GPS_Car extends Car implements GPS{
public GPS_Car(float speedx, float speedy, int expand_time) {
super(speedx, speedy, expand_time);
}
public static void main(String[] args) {
GPS_Car car1=new GPS_Car(1,1,1);
GPS_Car car2=new GPS_Car(20,20,20);
System.out.println("car1的X坐标为"+car1.getpoint().x+" Y坐标为"+car1.getpoint().y);
System.out.println("car2的X坐标为"+car2.getpoint().x+" Y坐标为"+car2.getpoint().y);
}
public Point getpoint() {
Point point=new Point();
point.setLocation(this.speedx*this.time,this.speedy*this.time);
return point;
}
}
interface GPS{
Point getpoint();
}
class Car{
public float speedx;
public float speedy;
public float time;
public Car(float speedx,float speedy,int expand_time){
this.speedx=speedx;
this.speedy=speedy;
time=(float)expand_time;
}
}
输出结果:
car1的X坐标为1 Y坐标为1
car2的X坐标为400 Y坐标为400
知识点:1、接口、继承方法使用;
2、类变量是所有对象共有,其中一个对象将它值改变,其他对象得到的就是改变后的结果;而实例变量则属对象私有,某一个对象将其值改变,不影响其他对象;实例变量是类实例化后,系统为其创建的一个类的实例变量的拷贝,即一个实例拥有一个实例变量。 类变量则是用static声明,系统为每个类变量分配一次存储空间。即所有的实例共享一个类变量。