標籤:os io 2014 ar new log res ios
//
// main.m
// OCbasic1
//
// Created by apple on 14-8-5.
// Copyright (c) 2014年 蘋果IOS軟體開發人員. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Dog.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
//write code here.
Dog *dog = [[Dog alloc] init];
//建立一個dog對象並初始化
//定義參數,調用方法
int ID = [dog getID];
int age = [dog getage];
float price = [dog getprice];
printf("dog ID: %d age: %d price: %f\n",ID,age,price);
//dog ID: 1 age: 2 price: 60.000000
Dog *dog2 = [[Dog alloc ]initWithID:20 andage:5 andprice:100.88];
ID =[dog2 getID];
age = [dog2 getage];
price = [dog2 getprice];
printf("dog2 ID: %d age: %d price: %f\n",ID,age,price);
//dog2 ID: 20 age: 5 price: 100.879997 return 0;
[dog2 setID:19 andage:4 andprice:86.8];
ID =[dog2 getID];
age = [dog2 getage];
price = [dog2 getprice];
printf("dog2 ID: %d age: %d price: %f\n",ID,age,price); }
}
//
// Dog.h
// OCbasic1
//
// Created by apple on 14-8-5.
// Copyright (c) 2014年 蘋果IOS軟體開發人員. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Dog : NSObject
{
//欄位(成員變數)及其存取權限
@protected
int ID;
@public
int age;
@private
float price;
}
//凡是以initXX開頭的都是建構函式
- (id) init;
//函數名為init,不帶參數
- (id) initWithID:(int)newID;
//函數名為initWith:,帶一個int的參數
- (id) initWithID:(int)newID andage:(int)newage;
//函數名為initWithID:andage:,帶兩個int參數
- (id) initWithID:(int)newID andage:(int)newage andprice:(float)newprice;
//函數名為initWithID:andage:andprince
- (void) setID:(int)newID;
- (int) getID;
//set/get ID
- (void) setage:(int)newage;
- (int) getage;
//set/get age
- (void) setprice:(float)newprice;
- (float) getprice;
//set/get price
- (void) setID:(int)newID andage:(int)newage;
// setID:andage:兩個參數
- (void) setID:(int)newID andage:(int)newage andprice:(float)newprice;
// setID:andage:andprice: 3個參數
@end
//
// Dog.m
// OCbasic1
//
// Created by apple on 14-8-5.
// Copyright (c) 2014年 蘋果IOS軟體開發人員. All rights reserved.
//
#import "Dog.h"
@implementation Dog
- (id)init
{
return [self initWithID:1 andage:2 andprice:60.0];
// self = [super init];
// // super表示父類
// // 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:60.0f];
// 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