【学习笔记之ios开发】1.Object-C语法概述
2014-01-27 17:14
585 查看
Dog.m // // Dog.m // OCBasic1 // // Created by peter on 14-1-26. // Copyright (c) 2014年 peter. All rights reserved. // #import "Dog.h" @implementation Dog - (id) init { return [self initWithID:1]; // 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 andPrice:60.0f]; // 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]; } - (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
Dog.h
// OCBasic1 // Dog.m // Created by peter on 14-1-26. // Copyright (c) 2014年 peter. All rights reserved. // #import <Foundation/Foundation.h> @interface Dog : NSObject { //写字段 @protected int ID; @public int age; @private float price; } //凡是以initXXXX开头的都是构造函数 - (id) init; //函数名为init 不带参数 - (id) initWithID:(int)newID; //函数名为initWithID:带一个int的参数 - (id) initWithID:(int)newID andAge:(int)newAge; //函数名为initWithID:andAge:带两个参数,都为int - (id) initWithID:(int)newID andAge:(int)newAge andPrice:(float)newPrice; //函数名为initWithID:andAge:andPrice:带3个参数 - (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; //setID:andAge: 两个参数 - (void) setID:(int)newID andAge:(int)newAge andPrice:(float)newPrice; //setID:andAge:andPrice 三个参数 @end
main.m
// // main.m // OCBasic1 // // Created by peter on 14-1-26. // Copyright (c) 2014年 peter. 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 *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.000000 // Dog *dog2 = [Dog alloc]; // [dog2 initWithID:100 andAge:26 andPrice:68.88]; Dog *dog2 = [[Dog alloc] initWithID:100 andAge:36 andPrice:68.88]; ID = [dog2 getID]; age = [dog2 getAge]; 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 [dog2 setID:2014 andAge:38 andPrice:87.2]; ID = [dog2 getID]; age = [dog2 getAge]; price = [dog2 getPrice]; printf("dog2 new id is %d age is %d price is %f\n",ID,age,price); //dog2 new id is 2014 age is 38 price is 87.199997 } return 0; }
工程源码链接:
http://pan.baidu.com/s/1nt4p4ut
相关文章推荐
- 黑马程序员——零基础学习iOS开发——10 Object-C 面向对象思想、OC语法简介、对象、类、
- ios蓝牙开发学习笔记(一)蓝牙概述
- iOS开发笔记 之 第1章 Objective-C语法学习
- iOS APP开发概述----学习笔记001
- IOS开发学习笔记012-核心语法
- iOS学习笔记:Object-C基础语法(OC介绍与Foundation初步)
- 苹果开发学习笔记《Object-C 的语法与 Cocoa 框架》
- iOS APP开发概述----学习笔记001
- ios开发学习笔记
- 2011年冬斯坦福大学公开课 iOS应用开发教程学习笔记(第四课) Views 视图
- iOS开发学习笔记 2-1 C语言部分 初识C语言
- ios开发学习笔记
- IOS开发基础Object-C(11)—Category语法
- IOS开发学习笔记009-OC基本知识
- IOS开发学习笔记043-QQ聊天界面实现
- IOS学习之斯坦福大学IOS开发课程笔记(第四课)
- IOS学习笔记之Object-C(一)
- IOS 学习笔记1 架构及概述
- IOS开发学习笔记之调试
- IOS学习笔记2:IOS开发入门