TA的每日心情 | 汗 2024-10-15 10:05 |
---|
签到天数: 372 天 [LV.9]以坛为家II
|
Dog.h- #import <Foundation/Foundation.h>
- @interface Dos:NSObject
- {
- @protected
- int ID;
- @public
- int age;
- @private
- float price
- }
- - (id) init;
- - (id) intWithID:(int)newID;
- - (id) intWithID:(int)newID andAge:(int)newAge;
- - (id) intWithID:(int)newID andAge:(int)newAge andPrice(float)newPrice;
- - (void) setID:(int)newID;
- - (int) getID;
- - (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;
- @end
复制代码
Dog.m- #import "Dog.h"
- @implenentation Dog
- - (id) init
- {
- self = [super init];
- if(self){
- ID = 1;
- age = 2;
- price = 60.0f;
- }
- return self;
- //return [self initWithID:1];
- }
- - (id) initWithID:(int)newID
- {
- self = [super init];
- if(self){
- ID = newID;
- age = 2;
- price = 60.0f;
- }
- return self;
- //return [self initWithID:1 andAge:2];
- }
- - (id) initWithID:(int)newID andAge:(int)newAge
- {
- self = [super init];
- if(self){
- ID = newID;
- age = newAge;
- price = 60.0f;
- }
- return self;
- //return [self initWithID:1 andAge:2 andPrice:60.0f];
- }
- - (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
复制代码
main.m- #import <Foundation/Foundation.h>
- #import "Dog.h"
- int main(int argc, const char * argv[])
- {
- @autoreleasepool{
- 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 \n",ID,age,price);
- // dog1 id is 1 age is 2 price is 60.0000
-
- Dog *dog2 = [[Dog alloc] initWithID:100 andAge:36 andPrice:68.88];
- int ID = [dog2 getID];
- int age = [dog2 getAge];
- float price = [dog2 getPrice];
- printf("dog2 id is %d age is %d price is %f \n",ID,age,price);
- //dog2 id is 100 age is 36 price is 68.879997
-
- Dog *dog2 = [[Dog alloc] setID:2012 andAge:38 andPrice:87.2];
- int ID = [dog2 getID];
- int age = [dog2 getAge];
- float price = [dog2 getPrice];
- printf("dog2 new id is %d age is %d price is %f \n",ID,age,price);
- //dog2 net id is 2012 age is 38 price is 87.19998
- }
- return 0
- }
复制代码 |
|