包含其他类作为属性的类 [英] Classes containing other classes as properties

查看:73
本文介绍了包含其他类作为属性的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MATLAB中,我有不同的类A& B.我想分配从类A& B作为A& B.我的代码看起来像这样

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

还有这个

classdef B < handle

   properties
      container
   end

   methods
      function object = B()
      end
   end
end

然后我要分配来自类A&的两个对象. Bcontainer属性的两个类A& B,就像

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

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.我是OOP的新手.

P.S. I am new to OOP.

编辑以获得更好的解释:

Edit for better explanation:

Class A& B根本不同,不应相互连接/继承.我有这样的想法:对象city包含对象street和对象house,对象house连接到对象street.因此,streethouse应该从city继承,但是streethouse应该彼此了解.但是,我应该如何实现都继承自city但彼此之间不共享/继承其他任何东西的许多对象(例如标志,汽车,人,猫,狗等)之间的关系?

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?

例如,我想在carstreet对象之间建立连接,因此,如果查看特定的street对象,我想分配特定的对象car1car2car3street对象.

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.

推荐答案

根据您的问题,您实际上希望模拟对象之间的一对多关系.

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

一种可能的方法是将句柄存储到关系的一个"侧的每个内部"对象中的每个对象中,然后使用类似findobj的方法获取共享对象的列表相同的一个"对象引用.

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.

以您的CarStreet示例为例.

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

然后是您的Street

classdef Street < handle
    properties
        name
    end

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

然后您可以像使用它

% 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);

缺点是,您必须维护StreetCar对象的列表才能传递给findobj,并且此搜索可能会成为大量对象的性能瓶颈.

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.

这篇关于包含其他类作为属性的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆