Object Oriented Communication

I was asked to describe the relationship between car, car, toyota in object-oriented programming (say, in php environment). I was at a dead end. Can someone help me? Thanks...

+4
source share
15 answers

A car is a car, and a toyota car is a car.

The simplest conclusion is that car may be a subclass of vehicle , and toyota car may be a subclass of car .

Of course, Toyota may also relate to the company, in which case everything will be a little different. I am sure that the paragraph above is what they expected to hear.

+5
source

I think Toyota belongs to the company.

I would say:

A car is an object, and a car is a type of vehicle. Toyota is a specific developer of Vehicle objects with an overloaded brake method to return false.

+22
source

A car is a type of vehicle, so you can think of Vehicle as a base class of Car .

Toyota makes cars. Instead of making this an inheritance relation, I would be inclined to add the manufacturer member variable to Vehicle . Then, for the Car instances made by Toyota, set car.manufacturer = "Toyota" .

+4
source

Just being embarrassed: a car is a subclass of a car, as well as a truck. Toyota is a manufacturer that produces both cars and trucks; therefore, Toyota should be one of several potential values ​​for the vehicle manufacturer attribute.

 class Vehicle{ protected $_model; protected $_manufacturer; public __construct($manufacturer) { $this->_manufacturer = $manufacturer; } } class Car extends Vehicle{ public __construct($model,$manufacturer) { parent::__construct($manufacturer); } $this->_model = $model; } class Truck extends Vehicle{ public __construct($model,$manufacturer) { parent::__construct($manufacturer); } $this->_model = $model; } $a = new Car('Avensis','Toyota'); $b = new Truck('Land Cruiser','Toyota'); 
+3
source

You meant:

 class Vehicle{ } class Car extends Vehicle{ } class Toyota extends Car{ } 
+2
source

“Car” will be the base class, “Car” will be the class that extends, inherits or derives from the “Vehicle” (depending on the language used), and ideally “Toyota” should be the value “brand” or “make” a property of one of these two classes.

+2
source

A car is a base class. A car is a derived class. Toyota (probably) is a value that can be assigned a “brand” from the Vehicle class. In theory, you could get Toyota from a vehicle (or something in that order), but IMO is generally a bad idea. Derivation should, as a rule, reflect a function, and a function tied to a brand can do little.

+2
source

The easiest way to think about this is in the logical hierarchy of what you are working with. And this applies to almost the whole world. What everyone has been talking about here is happening, but it may not help you understand what they are saying if you do not understand OOP. So let me clarify.

Vehicle: the vehicle may be any mode of transport. Keep in mind this could be a plane, train, car, bike, balloon, etc. A car is what is used to deliver you from one place to another. So the base class being the carrier tells you that it will be a transport device.

Car: A car is a form of car. Therefore, he is expanding the car. A car can be described as a car with 4 wheels, 2-inch doors (depending on the car), engine, steering wheel, wipers, headlights, emergency lights, brakes, accelerator, etc. Thus, the car also describes which vehicle you are referring to.

Toyota: Toyota is a form of car. Therefore, he is expanding the car. (It can also expand the truck if you work with the Truck class). There are some toyota elements that may not apply to every car. To help determine the type of vehicle, toyota can indicate gas mileage, number of doors, color, engine size, etc.

Hope this helps.

+2
source

Toyota is a car. A car is a car. Thus, the Toyota class would inherit from a car, which, in turn, would inherit from Vehicle.

+1
source

A car is a class. A car is a subclass of a car. Toyota may be a (fairly general) instance of the Car class or a subclass that adds Toyota-specific features.

+1
source

A car is a car, Toyota is a car.

So Toyota can be a subclass of Car and Car, which can be a subclass of Vehicle.

 class Vehicle { } class Car extends Vehicle { } class Toyota extends Car { } 

For example, a car may have one var element indicating the number of wheels and the engine start function (not all cars have an engine). While Toyota may have the same varieties as the car, as well as a string containing the model name and a way to request a pedal warranty; -)

+1
source

You can use different approaches.

  • Car is a subclass of Vehicle . Toyota is a subclass of Car .

  • Car is a subclass of Vehicle . Car has a property called make . You create a car and set its make to Toyota.

There are more ways to do this, but these two are the most obvious ones that come to mind that fit into the concept of OOP.

The key is to ask yourself, what is the most common term? This is a car. You can have many types of vehicles, such as trucks, SUVs, bicycles, ship ships and cars. So in OOP, your more specific class inherits from a more general class.

+1
source

In object-oriented terms, a car will be a base class a car will be a subclass of a vehicle

and Toyota will be a brand as an attribute.

+1
source

My trick: “toyota” should not be taken out of the “car,” because Toyota is a company, not a specific car. "Toyota_car" can be obtained from a "car". "toyota" should be derived from the "manufacturer" class, which is derived from the "company" class. The car class must have a member of the type pointer class for the manufacturer class. In the case of a vehicle manufactured by Toyota, an instance of an object of class car must have an inherited class that points to a Singleton object of type toyota. (C ++)

+1
source

Of course, this is not the answer that your interlocutor wanted to hear, but the goal of object-oriented programming is not to simulate the whole world, but simply to provide abstract data types that model specific concepts in the field of your application.

Therefore, the relationship between the three depends entirely on what kind of application you are making. It is possible that the car and the car should be of the same class. Many applications may not care what type of vehicle it has. They may also be indifferent to which brand, which allows you to drop the Toyota class.

OOP could be a useful tool if it were not taken hostage by idiots who thought the goal of programming was to create a UML diagram of the entire universe.

+1
source

All Articles