简体   繁体   English

Java类和子类逻辑

[英]Java classes and subclasses logic

I'm learning about Java classes and this example came on my mind: 我正在学习Java类,并且想到了这个示例:

Let's say I'm developing an app that can create cars. 假设我正在开发一个可以创建汽车的应用程序。 Each car is made up of some components (engine, tires, ...) and each component gives the car a boost in some areas (for example the engine boosts the acceleration). 每辆汽车都由一些部件(发动机,轮胎等)组成,并且每个部件在某些区域为汽车提供了助力(例如,发动机提高了加速度)。

To develop this I thought I can build a class "Cars" which has some variables that hold which components that car is equipped with. 为了发展这一点,我认为我可以建立一个“汽车”类,其中包含一些变量,这些变量可以控制汽车配备的组件。 Then I create a base class "Components" which holds the characteristic common to all components (their weight, size, ...) and some generic functions. 然后,我创建一个“组件”基类,该基类具有所有组件(它们的重量,大小等)和一些通用功能所共有的特征。 Then I create some subclasses of the "Components" class which define a specific component (for example I create the class "Engines" or "Tires"). 然后,创建“组件”类的一些子类,这些子类定义特定的组件(例如,我创建“引擎”或“轮胎”类)。 Of course each engine can be of different brands (BMW, Audi, ...) and each brand has different characteristics (let's say Audi engines accelerate faster than BMW ones). 当然,每个引擎可以具有不同的品牌(宝马,奥迪...),每个品牌具有不同的特征(比方说,奥迪引擎的加速要快于宝马的引擎)。

Now the question is, do I have to create different subclasses for each brand engine? 现在的问题是,我是否必须为每个品牌引擎创建不同的子类? Of course I can, but is this the correct way to build these kinds of "structures"? 我当然可以,但这是构建这类“结构”的正确方法吗?

And for example, if, after I build all the classes, I notice that I have to add another characteristic to all engine (for example maximum speed) I have to change all the subclasses of each specific brand engine, but this doesn't sound very "efficient". 例如,如果在构建所有类之后,我注意到我必须向所有引擎添加另一个特征(例如最大速度),那么我必须更改每个特定品牌引擎的所有子类,但这听起来并不合理。非常“高效”。

The "rule of thumb" is: you need separate classes if there is different behavior . “经验法则”是:如果行为不同,则需要单独的类。

eg.: An engine has a different behavior than a Tire , but a Audi engine has the same behavior as a BMW engine , just the property "amount of acceleration" differs, which is a configuration , not behavior. 例如:引擎的行为不同于Tire ,但Audi引擎的行为与BMW引擎相同,只是属性 “加速量”不同,这是一种配置 ,而不是行为。

If the difference can be expressed with different values of one same data attribute, and does not require different code paths to process those values, then you don't need separate subclasses for them. 如果差值可以用一个相同的数据属性的不同值来表示, 并不需要不同的代码路径来处理这些值,那么你就需要为他们独立的子类。

For instance, if you are calculating 0-60, you would have a generic formula, to which you plugin a particular engine's acceleration parameters. 例如,如果您要计算0-60,则将有一个通用公式,您可以在其中插入特定引擎的加速参数。 There is no need for separate AudiEngine and BMWEngine classes. 不需要单独的AudiEngineBMWEngine类。

OTOH, if an Audi engine has the additional feature of making a car invisible, then you do need a special AudiEngine class, with a special enableInvisibility() method on it. OTOH,如果Audi引擎具有使汽车不可见的附加功能,那么您确实需要一个特殊的AudiEngine类,上面带有特殊的enableInvisibility()方法。

Rule of thumb: if you find yourself writing if/else blocks that check on object types and do different things accordingly, you need subclasses. 经验法则:如果发现自己编写了if/else块来检查对象类型并相应地执行不同的操作,则需要子类。

I think you can use polymorphism and inheritance. 我认为您可以使用多态和继承。 For example, you create a class of slinics - each has the same variables (brand, power, etc.). 例如,您创建了一类诊所-每个诊所都有相同的变量(品牌,能力等)。 Then, when calling the class constructors, you can initialize variables (assign them "BMW", 130 etc.) It all depends on how your application works. 然后,在调用类构造函数时,可以初始化变量(将变量分配为“ BMW”,130等)。这完全取决于应用程序的工作方式。 If this is not the answer you want, specify the question and I will try to help. 如果这不是您想要的答案,请指定问题,我将尽力提供帮助。

you're on the right track but you're running into decisions that all designers face and there is no clear answer. 您走在正确的道路上,但是您遇到了所有设计师都面临的决策,并且没有明确的答案。

but to expand you could specifically define behaviours .. for example, you could start with your base class car . 但是要扩展,您可以具体定义behaviours ..例如,您可以从基类car then you could define an interface such as: 那么您可以定义一个接口,例如:

interface driveable
{
     void drive(engine type_of_engine, tire type_of_tire);
}

interface moddable
{
    void tune(exhuast type_of_exhaust);
}

then with car : 然后car

class car : driveable
{
    void drive(...)
}

class bmw : car {...}
class audi : car, moddable {...}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM