• IOS7:学习笔记2


    一、Deck类

    .h文件

     1 #import <Foundation/Foundation.h>
     2 #import "Card.h"
     3 
     4 @interface Deck : NSObject
     5 
     6 //atTop是方法名的一部分
     7 //addCard有两个参数,参数穿插在方法名间
     8 -(void)addCard:(Card *)card atTop:(BOOL)atTop;
     9 
    10 //addCard方法的重载,用重载的方式实现可选参数
    11 -(void)addCard:(Card *)card;
    12 
    13 -(Card *)drawRandomCard;
    14 
    15 
    16 @end

    .m文件

     1 #import "Deck.h"
     2 
     3 @interface Deck()
     4 @property (strong,nonatomic) NSMutableArray *cards; //of Card
     5 @end
     6 
     7 @implementation Deck
     8 
     9 -(NSMutableArray *)cards
    10 {
    11     if (!_cards)
    12     {
    13         _cards=[[NSMutableArray alloc] init];
    14     }
    15     return _cards;
    16 }
    17 
    18 -(void)addCard:(Card *)card atTop:(BOOL)atTop
    19 {
    20     if (atTop) {
    21         [self.cards insertObject:card atIndex:0];
    22     }
    23     else
    24     {
    25         [self.cards addObject:card];
    26     }
    27 }
    28 
    29 -(void)addCard:(Card *)card
    30 {
    31     [self.cards addObject:card];
    32 }
    33 
    34 -(Card *)drawRandomCard
    35 {
    36     Card *randomCard=nil;
    37     
    38     if ([self.cards count]) {
    39         unsigned index=arc4random()%[self.cards count];
    40         randomCard=self.cards[index];
    41         [self.cards removeObjectAtIndex:index];
    42     }
    43     return randomCard;
    44 }
    45 
    46 @end

    PlayingCar类:

    .h文件:

     1 #import "Card.h"
     2 
     3 @interface PlayingCard : Card
     4 
     5 @property (strong,nonatomic) NSString *suit;
     6 @property (nonatomic) NSUInteger rank;
     7 
     8 //+号代表该方法是一个类方法,意味着将它发送给类,而不是对象的实例
     9 //类方法常被用于两种情况:
    10 //1.创建事物
    11 //2.工具方法
    12 +(NSArray *)validSuits;
    13 +(NSArray *)maxRank;
    14 
    15 
    16 @end

    .m文件:

    #import "PlayingCard.h"
    @implementation PlayingCard
    
    - (NSString *)contents
    {
        //这种方法不好,不被使用
        //return [NSString stringWithFormat:@"%d%@",self.rank,self.suit];
        NSArray *rankStrings=[PlayingCard rankStrings];
        return [rankStrings[self.rank] stringByAppendingString:self.suit];
    }
    
    //因为提供了setter和getter,要使用@synthesize
    @synthesize suit=_suit;//because we provide setter and getter
    
    
    
    -(void)setSuit:(NSString *)suit
    {
        if ([[PlayingCard validSuits] containsObject:suit]) {
            _suit = suit;
        }
    }
    
    -(NSString *)suit
    {
        return _suit?_suit:@"?";
    }
    
    +(NSArray *)validSuits
    {
        return @[@"♥︎",@"♦︎",@"♠︎",@"♣︎"];
    }
    
    +(NSArray *)rankStrings
    {
        return @[@"?",@"A",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"J",@"Q",@"K"];
    }
    
    @end
  • 相关阅读:
    ASP.NET 无提示关闭窗口
    C# Winform程序获取外网IP地址
    使用System.Drawing.Printing 画报表。
    C# Winform调用IP地址、手机归属和身份证查询接口
    C# 获取文中文字符首字母
    C# WinForm给Button或其它控件添加快捷键响应
    C# 获取农历日期
    TSQL之插入的内容是查询出的值
    C# 获取中文星期的两种方法
    C#批量操作控件
  • 原文地址:https://www.cnblogs.com/hzz521/p/5095010.html
Copyright © 2020-2023  润新知