//
// main.m
// basic1.1
//
// Created by lyricdon on 15/4/24.
// Copyright (c) 2015年 lyricdon. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Dog.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
Dog *dog1=[Dog alloc];
[dog1 init];
int ID=[dog1 getID];
int age=[dog1 getAge];
float price=[dog1 getPrice];
printf("dog1 id is %d age is %d price is %f",ID,age,price);
}
return 0;
}
//
// Dog.h
// basic1.1
//
// Created by lyricdon on 15/4/24.
// Copyright (c) 2015年 lyricdon. All rights reserved.
//
#ifndef basic1_1_Dog_h
#define basic1_1_Dog_h
@interface Dog:NSObject{
@protected
int ID;
@public
int age;
@private
float price;
}
//凡是以initxxxx开头的都是构造函数
-(id)init;
//函数名为init 不带参数
-(id)initWithID:(int)newID;
//带一个int参数
-(id)initWithID:(int)newID andAge:(int)newAge;
-(id)initWithID:(int)newID andAge:(int)newAge andPrice:(float)newPrice;
-(void)setID:(int)newID;
-(int)getID;
//set/get ID
-(void)setAge:(int)newAge;
-(int)getAge;
-(void)setPrice:(float)newPrice;
-(float)getPrice;
-(void)setID:(int)newID andAge:(int)newAge;
-(void)setID:(int)newID andAge:(int)newAge andPrice:(float)newPrice;
//set id age price
@end
#endif
//
// Dog.m
// basic1.1
//
// Created by lyricdon on 15/4/24.
// Copyright (c) 2015年 lyricdon. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Dog.h"
@implementation Dog
-(id)init
{
return [self initWithID:1];
// self=[super init];
// //self表示父类
// //self表示对象自己
// if(self){
// ID=1;
// age=2;
// price=60.0f;
// }
// return self;
}
-(id) initWithID:(int)newID
{
return [self initWithID:newID andAge:2];
// self=[super init];
// if(self){
// ID=newID;
// age=2;
// price=60.0f;
// }
// return self;
}
-(id)initWithID:(int)newID andAge:(int)newAge
{
return [self initWithID:newID andAge:newAge andPrice:10.6f];
// self=[super init];
// if(self){
// ID=newID;
// age=newAge;
// price=60.0f;
// }
// return self;
}
-(id)initWithID:(int)newID andAge:(int)newAge andPrice:(float)newPrice{
self=[super init];
if(self){
ID=newID;
age=newAge;
price=newPrice;
}
return self;
}
-(void)setID:(int)newID
{
ID=newID;
}
-(int)getID
{
return ID;
}
-(void)setAge:(int)newAge
{
age=newAge;
}
-(int)getAge
{
return age;
}
-(void)setPrice:(float)newPrice
{
price=newPrice;
}
-(float)getPrice
{
return price;
}
-(void)setID:(int)newID andAge:(int)newAge
{
ID=newID;
age=newAge;
}
-(void)setID:(int)newID andAge:(int)newAge andPrice:(float)newPrice
{
ID=newID;
age=newAge;
price=newPrice;
}
@end
|
|