OC处理对象-打印对象和description方法

Object-C技术学习,更多资源请访问 https://www.itkc8.com

//  main.m

//  打印对象和description方法

//

//  Created by Goddog on 15/1/10.

//  Copyright (c) 2015年 Goddog. All rights reserved.

//

/*

 1.description方法是NSObject类的一个实例,是一个“自我描述”方法,重写description方法返回该对象。

 */

 

//
//  Girl.h
//  打印对象和description方法
//
//  Created by Goddog on 15/1/10.
//  Copyright (c) 2015年 Goddog. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Girl : NSObject
//定义成员变量
@property (nonatomic,copy) NSString* name;
@property (nonatomic,copy) NSString* size;
@property (nonatomic,assign) double height;

//有参数
-(id) initWithNam:(NSString*) name;
-(id) initWithSize:(NSString*)size andDouble:(double)height;
//无参数
-(void) info;
@end

 

//
//  Girl.m
//  打印对象和description方法
//
//  Created by Goddog on 15/1/10.
//  Copyright (c) 2015年 Goddog. All rights reserved.
//

#import "Girl.h"

@implementation Girl
@synthesize name = _name;
-(id) initWithNam:(NSString *)name
{
    if (self == [super init]) {
        self.name = name;
    }
    return self;
}
-(void) info
{
    NSLog(@"艺名:%@",self.name);
}

-(id) initWithSize:(NSString*)size andDouble:(double)height
{
    if (self == [super init]) {
        self.height = height;
        self.size = size;
    }
    return self;
}

//重新父类的description方法
-(NSString*) description
{
    //返回一个字符串
    return [NSString stringWithFormat:@"身高是:%g,胸围是:%@",self.height,self.size];
}

@end

 

#import <Foundation/Foundation.h>
#import "Girl.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //创建对象
        //Girl* girl = [[Girl alloc] init];
        
        //创建对象
        Girl* supGirl = [[Girl alloc] initWithNam:@"松岛枫"];
        
        //打印该对象
        NSLog(@"%@",[supGirl description]);
        
        //创建对象
        Girl* niceGirl = [[Girl alloc] initWithSize:@"34D" andDouble:1.80];
        NSLog(@"%@",niceGirl);
    }
    return 0;
}

Object-C技术学习,更多资源请访问 https://www.itkc8.com

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值