MatLAB中的类数组

时间:2012-11-17 00:06:45

标签: oop class matlab

我想在MatLAB中创建一个卡片组类。这是我第一次使用MatLAB体验OOP,而且我对MatLAB很新。我想要有两个班级:

  1. 卡(属性:类型[1-4],值[1-13])
  2. deck(包含卡片中卡片的列表/向量,稍后我将定义此类的方法; shuffle,drawCard等)。
  3. card.m:

    classdef card
       properties
          type;    % number: 1-4
          value;   % number: 1-13
       end
       methods
          function obj = card(type, value)
             % some code to check [type, value] should be inserted here
             obj.type = type;
             obj.value = value;
          end
       end
    end
    

    这是我班级的原始结构,功能将在稍后出现。现在 - 我如何定义甲板类?

    我还希望能让我的套牌舱与我的卡片实例通信(以确保我拨打正确的卡片:[卡片(1,1),卡片(1,2),卡片卡片(1,3) ),...,card(4,13)],例如。

    有什么建议吗? 提前谢谢!

2 个答案:

答案 0 :(得分:1)

如果要直接进入套牌,则需要实现subsref函数来处理索引对象的索引,即:

d = deck();
d(33)

或添加带索引参数的函数:

d = deck();
d.getCard(33)

最后一个似乎更合适,也更容易实现:

classdef deck
    properties
        cardstack=card;
    end
    methods(Static)
        function obj = deck()
            obj.cardstack(1:52) = card(); % array creation, will fail if card has no parameterless constructor
            for ii=1:4
                for jj=1:13
                    obj.cardstack((ii-1)*13+jj) = card(ii,jj);
                end
            end
        end
    end
    methods
        function crd = getCard(obj, idx)
            if idx>=1 && idx<=52
                crd = obj.cardstack(idx);
            else
                error('Deck:getCard','Index out of range');
            end
        end
    end
end

或只是使用:

d.cardstack(33)

这是相同的,没有明确的错误检查。

还要向card添加无参数构造函数,以便轻松创建card数组:

function obj = card(type, value)
    if nargin==0
        % eg:
        type=NaN;
        value=NaN;
    end
    % some code to check [type, value] should be inserted here
    obj.type = type;
    obj.value = value;
end

答案 1 :(得分:0)

也许您不需要将套牌作为一个类,因为您可以使用Card个对象进行大部分操作。 这是一个例子。

classdef Card < handle
   properties
      type    % number: 1-4
      value   % number: 1-13
   end
   methods
      function obj = Card(type, value)
         % some code to check [type, value] should be inserted here
         obj.type = type;
         obj.value = value;
      end
      function c = get_card(obj, t, v)
          c = obj([obj.type] == t & [obj.value] == v);
      end
      function c_arr = get_cards_array(obj, t, v)
          c_arr = obj(ismember([obj.type], t) & ismember([obj.value], v));
      end      
      function print(obj)
          for k = 1 : numel(obj)
              fprintf('Card type = %g; value = %g\n', obj(k).type, obj(k).value);
          end
      end
   end
end

用法:

%% build array
deck = Card.empty();
for type = 1 : 4
    for value = 1 : 13
        deck(end + 1, 1) = Card(type, value);
    end
end
%% if needed, you can reshape it into 4x13
deck = reshape(deck, 4, 13);
%% you can also use Card's methods from array:
>> deck.print
Card type = 1; value = 1
Card type = 1; value = 2
Card type = 1; value = 3
...
%% get certain card
c = deck([deck.type] == 3 & [deck.value] == 10);
c.print
%% shuffle 
idx = randperm(numel(deck));
deck = deck(idx);

更新:将get_card()添加到Card的方法中(参见实现):

>> c = deck.get_card(3, 10)    
c = 

  Card handle

  Properties:
     type: 3
    value: 10

  Methods, Events, Superclasses

两个评论:

1)如果这是您第一次使用MatLAB的OOP,那么理解classdef Card < handle的含义可能非常有用。

2)这是关于对象数组初始化的问题:Matlab's arrayfun for uniform output of class objects

更新2:添加了get_cards_array()方法。用法:

%% get array of cards
t = [1, 2];
v = [5, 6, 12];
c = deck.get_cards_array(t, v);
>> c.print
Card type = 1; value = 5
Card type = 1; value = 6
Card type = 1; value = 12
Card type = 2; value = 5
Card type = 2; value = 6
Card type = 2; value = 12