2

Game クラスがあるとします。

@interface Game : NSObject

CutthroatGame サブクラスで:

@interface CutthroatGame : Game

ViewController .h ファイルに次のようなプロパティがあるとします。

@property (strong) Game *game;

ViewController .m ファイルで次のようにプロパティを安全にオーバーライドできますか。

if (_playerCount == 3) {
    _game = [[CutthroatGame alloc] init];
else {
    _game = [[Game alloc] init];   
}

編集:これが機能するはずの場合、以下のエラーにどのように対処すればよいですか?

CutthroatGame は、次のような多くの追加プロパティを定義します。

@property (strong) Player *opponent2

ViewController の _game プロパティを使用してそれらにアクセスしようとすると、次のエラーが発生します。

 _game.opponent2 = [players objectAtIndex:0]; -- ERROR: Property 'opponent2' was not found on object of type 'Game *'
4

2 に答える 2

1

絶対!それがリスコフの置換原理です。CutthroatGameから適切にサブクラス化すれば、そのサブクラスでGame問題なく置換できます。Game

于 2013-06-28T00:28:38.080 に答える