我正在尝试基于类层次结构的挑战。我必须尝试评估并正确构建包含基于运输工具的层次结构。以下是课程选项:
在结构方面,我必须决定哪些类是抽象的,哪些类扩展哪些类和哪些类是接口。
这是我到目前为止所提出的:
运输
public abstract class Transport
{
//Super class. All vehicles are a form of transportation.
}
自行车
public abstract class Bicycle extends RoadVehicle
{
//Bicycle is a road vehicle. Does not implement Refuelable as it does not use fuel. Is abstract because Cycle is its sub-class.
}
汽车
public class Car extends RoadVehicle implements Refuelable
{
//Extends RoadVehicle which extends Transport and also implements Refuelable as it uses fuel.
}
循环
public class Cycle extends Bicycle
{
//Cycle is an act which is carried out when using a Bicycle so it extends Bicycle.
}
JetPlane
public class JetPlane extends Transport implements Refuelable
{
//JetPlane is not a road vehicle but directly extends Transport because it is used as a form of transport, also implements Refuelable.
}
RoadVehicle
public abstract class RoadVehicle extends Transport
{
//All classs that are road vehicles extends this clss. This seperates the road vehicles from the non-road vehicles.
}
充燃料
public interface Refuelable
{
//An interface that declares which tyypes of transport use fuel and which do not.
}
列车
public class Train extends Transport implements Refuelable
{
//Trains are not a road veicle but do extend Transport and are refuelable.
}
三轮车
public class Tricycle extends Transport
{
//Tricycle is a form of transport for children, it is not refuelable and does not operate on the road.
}
这是对的还是你看到有什么错误?我这样做是为了测试我对继承的知识,所以任何建议或指示都值得赞赏,谢谢。
答案 0 :(得分:1)
鉴于我们无法告诉每个实体应该采用的方法,我将从以下内容开始:
interface Transport
interface Refuelable
interface RoadVehicle extends Transport
interface Cycle extends RoadVehicle
class Bicycle implements Cycle
class Tricycle implements Cycle
class Car implements RoadVehicle, Refuelable
class JetPlane implements Transport, Refuelable
class Train implements Transport