Objective-C面向对象编程的基本应用

    《Objective-C 程序设计(第4版)》这本书的试读章节有两章,分别是 类、对象和方法,数据类型和表达式。无可厚非,Objective-C是一门面向对象的语言,如果使用面向对象的编程思想对使用他,并应用到具体的开发中,我觉得是iOS程序员的基本功,《Objective-C 程序设计(第4版)》,我看了两章,特别是第一张,作者的讲解的方式,都是从面向对象的方面去讲解这门语言,而不是单单怎么用进行描述。我也根据面向对象的思想写了一个小demo来讲解怎么在iOS开发中使用他。
     我创建了一个名叫WJHProudct的类,他有productId,productName,productImageName这三个属性,分别是商品id,商品名和商品图片名。并实现了两个方法,一个是description方法,还有一个是dealloc,在我看到了两个章节里面并没有介绍到这两个方法,可能在其他章节上会介绍,后者是用来释放资源,前者是用来描述这个类产生的对象的,如果没有实现这个方法,那么当我们使用NSLog打印WJHProudct的对象时,会看到这样的信息,<WJHProduct: 0x685d010>,也就是类名+对象地址,当我们想要知道这个对象具体是怎样时,这样的数据显然没什么用处,如果实现了,则会看到product:id:id:9,name:第9款,image:image,也就是你在Description的返回值,这个方法类似Java 的String方法。当我们在打印一个元素类型为WJHProudct的数组时,也会出现上面的效果,实现这个方法或许会花点时间,但是在调试的时候,他起到的作用,帮助是很大的。
     下面是两个类的代码。
WJHProduct.h

#import <Foundation/Foundation.h>

@interface WJHProduct : NSObject

@property (retain, nonatomic) NSString *productId;
@property (retain, nonatomic) NSString *productName;
@property (retain, nonatomic) NSString *productImageName;

@end


WJHProduct.m

#import "WJHProduct.h"

@implementation WJHProduct

@synthesize productId = _productId;
@synthesize productName = _productName;
@synthesize productImageName = _productImageName;

- (NSString *)description{
  NSString *tempDescription = [NSString stringWithFormat:@"id:%@,name:%@,image:%@",_productId,_productName,_productImageName];
  return tempDescription;
}

- (void)dealloc{
  [_productId release];
  [_productName release];
  [_productImageName release];
  [super dealloc];
}

@end



    而在真实的开发中,如果应用到这个类呢?我想要创建一个商品列表,用来展示我的商品,每件商品都是WJHProduct的一个对象,并且每件商品我想要显示的样式是这样的,那么就想要我定义这样一个Cell来显示,


 所以我创建了一个WJHProductCell类,他具有一个product的属性,这样我只要将WJHProduct对象发送给他,就可以,他负责显示出来,我并不需要关心他们内部是怎么实现了,这样也就是封装,很多人都是喜欢在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法里面对每个Cell进行赋值,但是我觉得他既然要显示这样的样式,
那么就需要- (void)setProduct:(WJHProduct *)product的技能,那么我只要知道,我亲爱的UITableViewCell需要一个WJHProduct的对象,然后我给他,至于怎么显示就是他的事了,下面是UITableViewCell的代码
WJHProductCell.h

#import <UIKit/UIKit.h>

#import "WJHProduct.h"

@interface WJHProductCell : UITableViewCell {
  
  
  IBOutlet UIImageView *_productImageView;
  IBOutlet UILabel *_productIdLabel;
  IBOutlet UILabel *_productNameLabel;
}

@property (retain, nonatomic) WJHProduct *product;

@end


WJHProductCell.m

#import "WJHProductCell.h"

@implementation WJHProductCell

@synthesize product = _product;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setProduct:(WJHProduct *)product{
  if (_product) {
    [_product release];
    _product = nil;
  }
  _product = [product retain];
  
  _productImageView.image = [UIImage imageNamed:_product.productImageName];
  _productIdLabel.text = _product.productId;
  _productNameLabel.text = _product.productName;
}

- (void)dealloc {
    [_product release];
  
    [_productImageView release];
    [_productIdLabel release];
    [_productNameLabel release];
    [super dealloc];
}
@end

    这篇文章只写关于iOS的面向对象开发,不想介绍iOS开发的其他内容,想知道的可以看《Objective-C 程序设计(第4版)》,我想作者会介绍得很清楚的,我只是简单描述下我的想法,后面是具体的效果还有整个Demo的代码,我的开发环境的Mac OS X 10.8.2 Xcode4.5.


猜你喜欢

转载自lsdev.iteye.com/blog/1717806
今日推荐