我正在为我的编程类做一个家庭作业,涉及实现接口.这里的问题是我真的只是不了解接口或它们用于什么(教授对解释它不是很好).
分配是制作一个“车辆”超类,而不是三个子类,比如“卡车”或“吉普”,每个都有自己的几个特征. “Vehicle”类必须实现类似的界面,我想我已经想到了(我用compareTo()方法编写比较车辆上的门数),另一个类也必须实现“混合”类(I不知道这意味着什么).然后,我们必须实现toString(),equals(Object o)和compareTo(Object o)方法.
我想我有compareTo(),但是使用equals()我不知道.我们要做的最后一件事是编写一个Main进行测试,这涉及制作一个Vehicle对象数组,打印出来,对它们进行排序,然后重新打印它们.它还应该遍历数组并打印混合动力车的价格溢价,并使用等于(对象o)方法来比较2辆车.
这是我的“Vehicle”超类的代码
package vehicle;
abstract public class Vehicle implements Comparable {
private String color;
private int numberOfDoors;
// Constructor
/**
* Creates a vehicle with a color and number of doors
* @param aColor The color of the vehicle
* @param aNumberOfDoors The number of doors
*/
public Vehicle(String aColor,int aNumberOfDoors) {
this.color = aColor;
this.numberOfDoors = aNumberOfDoors;
}
// Getters
/**
* Gets the color of the vehicle
* @return The color of the vehicle
*/
public String getColor() {return(this.color);}
/**
* Gets the number of doors the vehicle has
* @return The number of doors the vehicle has
*/
public int getNumberOfDoors() {return(this.numberOfDoors);}
// Setters
/**
* Sets the color of the vehicle
* @param colorSet The color of the vehicle
*/
public void setColor(String colorSet) {this.color = colorSet;}
/**
* Sets the number of doors for the vehicle
* @param numberOfDooRSSet The number of doors to be set to the vehicle
*/
public void setNumberOfDoors(int numberOfDooRSSet) {this.numberOfDoors = numberOfDooRSSet;}
@Override
public int compareTo(Object o) {
if (o instanceof Vehicle) {
Vehicle v = (Vehicle)o;
return this.numberOfDoors - v.getNumberOfDoors();
}
else {
return 0;
}
}
/**
* Returns a short string describing the vehicle
* @return a description of the vehicle
*/
@Override
public String toString() {
String answer = "The car's color is "+this.color
+". The number of doors is"+this.numberOfDoors;
return answer;
}
}
我还将发布我的一个子类
package vehicle;
abstract public class Convertible extends Vehicle {
private int topSpeed;
// Constructor
/**
* Creates a convertible with a color,number of doors,and top speed
* @param aColor The color of the convertible
* @param aNumberOfDoors The number of doors of the convertible
* @param aTopSpeed The top speed of the convertible
*/
public Convertible (String aColor,int aNumberOfDoors,int aTopSpeed) {
super(aColor,aNumberOfDoors);
this.topSpeed = aTopSpeed;
}
// Getters
/**
* Gets the top speed of the convertible
* @return The top speed of the convertible
*/
public int getTopSpeed() {return(this.topSpeed);}
// Setters
/**
* Sets the top speed of the convertible
* @param topSpeedSet The top speed to set to the convertible
*/
public void setTopSpeed(int topSpeedSet) {this.topSpeed = topSpeedSet;}
/**
* Returns a short description of the convertible
* @return a short description of the convertible
*/
@Override
public String toString() {
String answer = "The car's color is "+super.getColor()
+",the number of doors is "+super.getNumberOfDoors()
+",and the top speed is "+this.topSpeed+" mph.";
return answer;
}
}
我绝对不会要求任何人为我做这项工作,但是如果有人能够更多地了解接口如何工作以及这项功课真正要求的那么好.
非常感谢所有帮助
谢谢!
什么是界面?
当我第一次开始编程时,我也发现界面的概念令人困惑,因此我喜欢将其视为标准的“规则手册”.实现此规则手册的每个类都有一个必须遵循的“规则”列表.例如,请考虑以下界面:
interface Bounceable{
public void setBounce(int bounce);
public int getBounce();
}
上面的规则书声明了一个反弹的界面.它指出任何可以反弹的东西都必须设置反弹并且也会反弹.任何实现此接口的类都必须遵循规则手册.
为什么这本规则书会有用?
那么,如果你想编写一个游乐场,孩子们玩各种有弹性的东西,那该怎么办?您可以制作以下类型的弹性物品..
public class FootBall implements Bounceable{
private int bounce;
public void setBounce(int bounce){
this.bounce = bounce;
}
public int getBounce(){
return this.bounce;
}
}
public class BaseBall implements Bounceable{
private int bounce;
public void setBounce(int bounce){
this.bounce = bounce;
}
public int getBounce(){
return this.bounce;
}
}
上述类定义了一种弹力球.然后,您将创建您的playground类,并可以围绕抽象Bounceable接口定义方法.例如,如果篮球圈是你班上的一种方法怎么办?如果它可以接受任何有弹性的东西作为一个论点怎么办?这意味着只要它实现了可弹性就可以传递任何类型的球.如果你没有接口或类似的功能,你可以看到你的代码有多乱,你实现的球越多.
编辑:我已经包含了一个小实用的例子..
一个实际的例子就是……
public void slamDunk(Bounceable bouncyThing){
System.out.println("You scored three points!");
}
以下两个对slamDunk的调用都是有效的……
slamDunk(new BaseBall());
slamDunk(new FootBall());