@fiy-fish
2015-07-14T11:21:41.000000Z
字数 1025
阅读 1407
Objective-c
// day01-05-构造方法//// Created by Aaron on 15/7/1.// Copyright (c) 2015年 Aaron. All rights reserved.//#import <Foundation/Foundation.h>#import "Clothes.h"int main(int argc, const char * argv[]) {@autoreleasepool {//初始化---对象产生的时候就已经给实例变量赋值的过程//构造方法---init或者以init开头的方法//作用是给对象初始化//构造方法在创建对象的时候必须调用一次Clothes *c = [Clothes alloc];c = [c init];//[c setColors:"red"];[c showColor];}return 0;}
#import <Foundation/Foundation.h>@interface Clothes : NSObject{char _colors[20];char *_ss;}//设值-(void)setColors:(char *)color;//显示-(void)showColor;@end
#import "Clothes.h"@implementation Clothes//id 泛型对象指针,可以指向任意类型的对象//instancetype 关联方法-(instancetype)init{//self是一个指针//表示对象自身//self就是消息的接收者//super 是一个编译器符号//表示调用父类的方法if(self = [super init]){strcpy(_colors, "black");}return self;}-(id)test1{return [[Clothes alloc] init];}//instancetype 表示只能返回当前类的对象//也是泛型,在不同的类里面出现,就表示不同的对象类型//instancetype 做返回值的方法叫做关联方法//只能做返回值类型//但是id可以做参数-(instancetype)test2{return [[Clothes alloc] init];}-(void)setColors:(char *)color{strcpy(_colors, color);}-(void)showColor{NSLog(@"....%s",_colors);}@end
