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