1

我想在 MatLAB 中创建一个卡片组类。这是我第一次使用 MatLAB 体验 OOP,而且我对 MatLAB 还很陌生。我正在考虑开设 2 节课:

  1. 卡片(属性:类型 [1-4],值 [1-13])
  2. 卡组(包含卡组中卡片的列表/向量,稍后我将定义此类的方法;洗牌、绘图卡等)

卡.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

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

我还希望能够让我的套牌类与我的卡片实例进行通信(以确保我调用正确的卡片:[card(1,1), card(1,2), card(1,3), . .., card(4, 13)],例如。

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

4

2 回答 2

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
于 2012-11-17T00:41:29.883 回答
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
于 2012-11-17T01:06:39.393 回答