面向对象的关系
我被要求用面向对象编程术语(假设在 php 环境中)描述车辆、汽车、丰田之间的关系。我被难住了。有人可以帮我吗?谢谢...
I was asked to describe the relationship between vehicle, car, toyota in object oriented programming term (let's say in php environment). I was stumped. Can someone help me about it? Thanks...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
我认为丰田指的是该公司。
我会说:
车辆是一个对象,汽车是车辆的一种类型。 Toyota 是 Vehicle 对象的一个特殊实现者,其
brake
方法被重载以返回 false。I think Toyota refers to the company.
I would say:
Vehicle is an object, and a Car is a type of Vehicle. Toyota is a particular implementor of Vehicle objects with the
brake
method overloaded to return false.汽车是车辆,丰田汽车是汽车。
最直接的说法是
car
可以是vehicle
的子类,toyota car
可以是car
的子类>。当然,
Toyota
也可以指代该公司,在这种情况下情况会有所不同。不过,我很确定上面的段落是他们期望听到的。A car is a vehicle and a toyota car is a car.
The most straightforward statement is that
car
could be a subclass ofvehicle
and atoyota car
could be a subclass ofcar
.Of course,
Toyota
could also refer to the company, in which case things would be a little different. I'm pretty sure the paragraph above is what they expected to hear, though.汽车是一种车辆类型 - 因此您可以将
Vehicle
视为Car
的基类。丰田生产汽车。我倾向于向
Vehicle
添加一个manufacturer
成员变量,而不是使其成为继承关系。然后,对于丰田制造的Car
实例,设置car.manufacturer = "Toyota"
。Car is a type of Vehicle - so you could think of
Vehicle
as a base class ofCar
.Toyota manufactures cars. Instead of making this an inheritance relationship, I would be inclined to add a
manufacturer
member variable toVehicle
. Then for instances ofCar
that are made by Toyota, setcar.manufacturer = "Toyota"
.尴尬的是:汽车是车辆的子类,卡车也是。
丰田是一家同时生产汽车和卡车的制造商;因此,丰田应该是车辆制造商属性的几个潜在值之一。
Just to be awkward: car is a subclass of vehicle, as is truck.
Toyota is a manufacturer who make both cars and trucks; so Toyota should be one of several potential values for the manufacturer attribute of vehicle.
你的意思:
Did you mean:
“Vehicle”将是一个基类,“Car”将是一个扩展、继承或派生自“Vehicle”的类(取决于所使用的语言),理想情况下“Toyota”应该是“品牌”的值或“制作”这两个类之一的属性。
"Vehicle" would be a base class, "Car" would be a class that extends, inherits, or is derived from "Vehicle" (depending on the language used), and ideally "Toyota" should be the value of a "brand" or "make" property of one of those two classes.
车辆是一个基类。 Car 是一个派生类。丰田(可能)是一个可以分配给车辆类“品牌”成员的值。理论上,您可以从车辆(或类似的东西)衍生出丰田,但在我看来,这通常是一个糟糕的主意。衍生通常应该反映功能,并且功能很少与品牌相关。
Vehicle is a base class. Car is a derived class. Toyota is (probably) a value that can be assigned to the "brand" member of the Vehicle class. In theory, you could derive Toyota from vehicle (or something on that order), but IMO it's generally a poor idea. Derivation should generally reflect function, and there's little in the way of function that's attached to a brand.
最基本的思考方法是按照您正在处理的内容的逻辑层次结构来思考。这实际上适用于世界上的任何事物。这里每个人所说的都是正确的,但如果你不理解 OOP,它可能无法帮助你理解他们在说什么。所以请允许我详细说明。
车辆:车辆可以是任何类型的交通工具。请记住,它可以是飞机、火车、汽车、自行车、热气球等。车辆是用来将您从一个地点带到另一个地点的东西。因此,车辆的基类告诉您它将是一种运输设备。
汽车:汽车是交通工具的一种形式。因此,它扩展了车辆。汽车可以被描述为具有 4 个轮子、2-n 个车门(取决于汽车)、发动机、方向盘、雨刷、头灯、闪光灯、制动器、加速器等的车辆。因此汽车进一步描述了您是什么车辆指的是。
丰田:丰田是汽车的一种形式。因此,它扩展了汽车。 (如果您正在使用卡车类,它也可以扩展卡车)。丰田的某些元素可能并不适用于所有汽车。因此,为了帮助进一步定义汽车的类型,丰田可以指定油耗、车门数量、颜色、发动机尺寸等。
希望有所帮助。
The most basic way to think about it is in the logical hierarchy of what you are working with. And this would apply to anything in the world practically. What everyone has said here is spot on, but it may not help you understand what they are saying, if you don't understand OOP. SO allow me to elaborate.
Vehicle: A vehicle could be any sort of transportation. Keep in mind, it could be a plane, train, automobile, bike, hot air ballon, etc. A vehicle is something used to get you from one location to another. So the base class being vehicle tells you it's going to be a transportation device.
Car: A car is a form of vehicle. Therefore, it EXTENDS vehicle. A car can be described as a vehicle with 4 wheels, 2-n doors (depending on the car), an engine, a steering wheel, wipers, headlights, blinkers, brakes, accelerator, etc. So car further describes what vehicle you are referring to.
Toyota: A toyota is a form of car. Therefore, it EXTENDS car. (It could also extend truck if you were working with a Truck class). There are certain elements of a toyota that may not apply to every car. So to help further define the type of car, toyota could designate gas mileage, number of doors, color, size of engine, etc.
Hope that helps.
丰田是一辆车。汽车是一种车辆。
因此,Toyota 类将从 Car 继承,而 Car 又将从 Vehicle 继承。
Toyota is a car. Car is a Vehicle.
So Toyota class would inherit from Car which in turn would inherit from Vehicle.
车辆是一个类。
汽车是车辆的子类。
Toyota 可以是 Car 类的(相当通用的)实例,也可以是添加 Toyota 特定功能的子类。
Vehicle is a class.
Car is a sub-class of Vehicle.
Toyota could be a (fairly generic) instance of the Car class, or a sub-class adding Toyota-specific functionality.
汽车是车辆,丰田是汽车。
因此,Toyota 可以是 Car 的子类,而 Car 可以是 Vehicle 的子类。
例如,一辆汽车可能有一个成员 var 来指示车轮数量和一个打开发动机的函数(并非所有车辆都有发动机)。虽然丰田汽车可能具有与汽车相同的变量,但加上一个包含型号名称的字符串和要求踏板保修的方法;-)
A Car is a Vehicle, a Toyota is a Car.
So, Toyota could be a subclass of Car and Car could be a subclass of Vehicle.
For example a Car may have one member var to indicate the number of wheels and a function to turn on the engine (not all vehicles have an engine). While a Toyota may have the same vars as a Car, plus a string containing the model's name and a method to ask for warranty on pedals ;-)
您可以采取不同的方法。
Car
是Vehicle
的子类。Toyota
是Car
的子类。Car
是Vehicle
的子类。Car
有一个名为make
的属性。您实例化 Car 并将其make
设置为“Toyota”。有更多的方法可以做到这一点,但这两种是最明显的符合 OOP 概念的方法。
关键是问自己最通用的术语是什么?是车辆。您可以拥有多种车辆,例如卡车、SUV、自行车、气垫船和汽车。因此,在 OOP 中,更具体的类继承自更通用的类。
You can take different approaches.
Car
is a subclass ofVehicle
.Toyota
is a subclass ofCar
.Car
is a subclass ofVehicle
.Car
has a property calledmake
. You instantiate Car and set itsmake
to "Toyota".There are more ways to do it, but those two are the most obvious that come to mind that fit into the OOP concept.
The key is to ask yourself what is the most general term there? It's vehicle. You can have many kinds of vehicles such as trucks, SUVs, bicycles, hovercrafts and cars. So, in OOP, your more specific class inherits from the more general class.
用面向对象的术语来说
车辆将是基类
汽车将是车辆的子类
,丰田将品牌名称作为属性。
In object oriented term
Vehicle will be the base class
car will be the subclass of vehicle
and Toyota will be brand name as an attribute.
当然,这不是面试官想要听到的答案,但是面向对象编程的目标不是对整个世界进行建模,而只是提供抽象数据类型,对您的领域中的特定概念进行建模。应用程序。
因此,三者之间的关系完全取决于您正在制作什么应用程序。车辆和汽车完全有可能属于同一类别。许多应用程序可能并不关心它是什么类型的车辆。他们也可能不关心它是什么品牌,因此您也可以扔掉丰田汽车。
如果 OOP 没有被那些认为编程的目标是生成整个宇宙的 UML 图的白痴劫持的话,它可能是一个有用的工具。
Of course, this isn't the answer your interviewer wanted to hear, but the goal of object-oriented programming is not to model the entire world, but simply to provide abstract data types which model specific concepts in the domain of your application.
Hence, the relationship between the three depends entirely on what application you're making. It is perfectly possible that vehicle and car should be the same class. A lot of applications may not care what type of vehicle it is. They also might not care what brand it is, allowing you to throw away the Toyota class as well.
OOP could have been a useful tool, if it hadn't been taken hostage by idiots who thought the goal of programming is to produce a UML diagram of the entire universe.
我的看法:
“toyota”不应该源自“car”,因为丰田是一家公司,而不是具体的汽车。 “toyota_car”可以源自“car”。 “toyota”应该派生自“manufacturer”类,而“manufacturer”类又派生自“company”类。 “车辆”类应该有一个指向“制造商”类的指针类型的类成员。对于丰田制造的汽车,类“car”的实例化对象应该具有指向类“toyota”类型的 Singleton 对象的继承类成员。 (C++)
My take:
"toyota" should not be derived from "car", because Toyota is a company, not a specific car. A "toyota_car" could be derived from "car". "toyota" should be derived from the "manufacturer" class, which is derived from the "company" class. The "vehicle" class should have a class member of type pointer to class "manufacturer". In the case of a car manufactured by Toyota, the instantiated object of class "car" should have that inherited class member pointing to the Singleton object of type class "toyota". (C++)