Classes containing other classes as properties

99封情书 提交于 2019-12-25 08:20:14

问题


in MATLAB I have different classes A & B. I want to assign objects created from class A & B as properties in both A & B . My code looks like this

classdef A < handle

   properties
      container
   end

   methods
      function object = A()
      end
   end
end

and this

classdef B < handle

   properties
      container
   end

   methods
      function object = B()
      end
   end
end

Then I am assigning both objects from class A & B to the container-property from both classes A & B, like

object_from_class_A.container = object_from_class_A

and

object_from_class_A.container = object_from_class_B

This means, I use the same variable/property for storing objects from different classes. Is this a bad design choise? How should I avoid this? I am just trying to assign objects to each other, because I am trying to build relationships between different objects.

P.S. I am new to OOP.

Edit for better explanation:

Class A & B are fundamentally different and should not be connected/inherited from each other. I have something in mind like this: Object city contains object street and object house, object house is connected to object street. So, street and house should inherit from city, but street and house should know each other. But how should I realize the relation between many objects (like sign, car, people, cat, dog, etc) that all inherit from city but dont share/inherit anything else between each other?

For example I want to establish a connection beween a car an an street object, so if I look into a specific street-object, I want to assign the specific objects car1, car2, car3 to the street object.


回答1:


Based on your question, you essentially want to emulate a one-to-many relationship between objects.

One possible way of doing this would be to store the handle to the "one" side of the relationship within each of the "many" side's objects and then use something like findobj to get the a list of objects that share the same "one" object reference.

In the case of your Car and Street example.

classdef Car < handle
    properties
        name
        street      % A handle to the street you're on
    end

    methods
        function self = Car(name, street)
            self.name = name;
            self.street = street;
        end
    end
end

And then your Street class

classdef Street < handle
    properties
        name
    end

    methods
        function self = Street(name)
            self.name = name;
        end
    end
end

And then you could use it like

% Create some streets
streets = [Street('Street1'), ...
           Street('Street2')];

% Create some cars
cars = [Car('one', streets(1)), ...
        Car('two', streets(2)), ...
        Car('three', streets(1))];

% Now get cars that are on Street1
cars_on_street_1 = findobj(cars, 'street', streets(1));

This has the added advantage in that it is trivial to change streets (since you only have to update the car object)

cars(1).street = streets(2);

The downside is that you have to maintain a list of Street and Car objects to pass to findobj and this search could potentially get to be a performance bottleneck for a very large number of objects.

Now if you're not going to be changing what street a car is on etc. then you could always create a factory to construct the class relationships (adding references in both objects) and then just use the data that you've created.




回答2:


If I am understanding your question correctly here is my suggestion.

Inheritance enable a new class to take on properties and methods of another class. Class B in your case wouldn't need the additional function definition if you have it inherit from class A.



来源:https://stackoverflow.com/questions/41556731/classes-containing-other-classes-as-properties

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!