iOS:Objective-C学习入门随手记

2016年05月12日 iOS 4条评论 阅读7489次

方法前的 + 代表 静态方法。无需实例化该类即可调用该方法

方法前的 - 代表 动态方法。必须实例化该类才可调用该方法


object-c:bean类及其初始化


面向对象思想中很重要的一点就是万物即对象。
既然是对象则一般就会有属性方法等。
因此,我们有必要为常用到的对象创建一个bean的类。

在object-c中,创建一个类很简单。下面是以一个市场可能具有的相关属性作为对象的bean类。

@interface MarketBean : NSObject {
    NSString *name;
    NSString *type;
    NSString *location;
}
- (id)initWithName:(NSString *)name Type:(NSString *)type Loction:(NSString *) loc;
+ (id)marketWithName:(NSString *)name Type:(NSString *)type Loction:(NSString *) loc;
@end

@implementation MarketBean

-(id)initWithName:(NSString *)name Type:(NSString *)type Loction:(NSString *) loc {

    if (self = [super init]) {
        _name = name;
        _type = type;
        _location = location;
    }
    return self;
}
@end



Object-C的便利构造器

还有一种被称为便利构造器的构造方法是更常用的更简单的。
在初始化方法的基础上进行了封装,即封装了对象的创建过程。便利构造器是“+”号方法,返回 本类型 的实例,方法名以类名开头。根据需求不同,可以没有参数、或多个参数。
内部实现,封装了alloc和初始化方法。

+(id) marketWithName:(NSString *)name Type:(NSString *)type Location:(NSString *)loc {
    id obj = [[MarketBean alloc] initWithName:name Type:type Location:loc];
    [obj autorelease];
    return obj;
}


注意[obj autorelease]一句,我在xcode7(iOS9)下用的时候,有编译错误的提示。
ARC

分享本文至:

WRITTEN BY

avatar
本文标签:iOSobjective-c
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

4 条评论

  1. avatar jbsbruxelles

    <a href="http://www.curveshampton.com/nike-dunk-sky-high-wedge-gr%C3%B8nn-footf">nike dunk sky high wedge gr酶nn</a> <a href="http://www.jcvindreau.com/kvinders-air-jordan-retro-2-gr%C3%B8n-hvid-sneakerd">kvinders air jordan retro 2 gr酶n hvid</a> <a href="http://www.weinhefe.com/air-max-thea-blanco-mint-candy-schuhef">air max thea blanco mint candy</a> <a href="http://www.fboagropec.com/jordan-5-azul-rosado-p%C3%BArpura-foote">jordan 5 azul rosado p煤rpura</a> <a href="http://www.kikoyboy.com/nike-zoom-vomero-femminile-rosso-arancia-skoe">nike zoom vomero femminile rosso arancia</a> <a href="http://www.giraprendi.com/kam-chancellor-limited-jersey-nflh">kam chancellor limited jersey</a>
    jbsbruxelles http://www.jbsbruxelles.com/

  2. avatar agenpermainan

    <a href="http://www.randygageteam.com/rot-converse-pumps-schuhek">rot converse pumps</a> <a href="http://www.newlouisbag.com/chicago-bears-gsh-on-jersey-nflh">chicago bears gsh on jersey</a> <a href="http://www.thebrandiac.com/nike-internationalist-dove-gr%C3%A5-hvit-obuvi">nike internationalist dove gr氓 hvit</a> <a href="http://www.isisprojectsa.com/nike-air-max-90-women-white-green-shoesj">nike air max 90 women white green</a> <a href="http://www.linnwoodpark.com/mens-nike-air-force-1-purple-gold-shoesl">mens nike air force 1 purple gold</a> <a href="http://www.perfecthostia.com/ralph-lauren-shoulder-bag-for-men-poloh">ralph lauren shoulder bag for men</a>
    agenpermainan http://www.agenpermainan.com/

  3. avatar expochacra

    <a href="http://www.zrdcwz.com/prada-shoulder-jet-set-br4697-handbags-in-coffee-pradar">prada shoulder jet set br4697 handbags in coffee</a> <a href="http://www.adamkhadaroo.com/kyrie-3-low-nox-shoesa">kyrie 3 low nox</a> <a href="http://www.kelebekotel.com/nike-kd-9-university-r%C3%B8d-flag-kurpesa">nike kd 9 university r酶d flag</a> <a href="http://www.massagelizard.com/silver-red-womens-asics-gel-quantum-360-shoes-sportsd">silver red womens asics gel quantum 360 shoes</a> <a href="http://www.neodipol.com/purple-silver-womens-nike-shox-turbo-21-shoes-shoesc">purple silver womens nike shox turbo 21 shoes</a> <a href="http://www.mbcphila.com/mujeres-asics-gel-kinsei-4-naranja-plata-schuhef">mujeres asics gel kinsei 4 naranja plata</a>
    [url=http://www.expochacra.com/]expochacra[/url]

  4. avatar icxeber

    <a href="http://www.idomicil.com/north-face-waterproof-jacket-sale-northfaceh">north face waterproof jacket sale</a> <a href="http://www.revtelluride.com/nike-air-max-tavas-rot-herren-schuhek">nike air max tavas rot herren</a> <a href="http://www.footslockerca.com/air-jordan-6-br%C3%B8d-skoi">air jordan 6 br酶d</a> <a href="http://www.alyysjf.com/nike-air-force-1-gris-suede-07-femmes-chaussuresh">nike air force 1 gris suede 07 femmes</a> <a href="http://www.pertpasties.com/minnesota-timberwolves-new-era-nba-butter-so-soft-9fifty-snapback-cap-hatl">minnesota timberwolves new era nba butter so soft 9fifty snapback cap</a> <a href="http://www.newliveportal.com/adidas-ace-16.1-fg-ag-solar-vert-solar-rose-core-noir-chaussuresh">adidas ace 16.1 fg ag solar vert solar rose core noir</a>
    icxeber http://www.icxeber.com/

欢迎留言




用户登录

sitemap