看完征戰(zhàn)Objective-C和Objective-C面向?qū)ο蟪躞w驗(yàn)
發(fā)現(xiàn)Object-C的語法和C語言是一致的,用官方表述來說粮彤,就是OC是C的超集导坟,完全支持C的語法惫周。
在磁盤存儲(chǔ)的物理形式上闯两,OC頭文件后綴名為.h
漾狼,實(shí)現(xiàn)文件后綴名為.m
逊躁,與c++混寫的實(shí)現(xiàn)文件后綴名為.mm
準(zhǔn)備工作:用xCode做oc開發(fā)必備技巧
/**
* 必備插件:
* 1. Xcode plug-in:快速生成格式化的注釋 [VVDocumenter-Xcode](https://github.com/onevcat/VVDocumenter-Xcode)
* 必須記住的快捷鍵:
* * 格式化代碼(重排縮進(jìn)):ctrl+i
* * 生成格式化注釋: ///
* * 折疊稽煤、展開代碼段 option+cmd+ ← | →
* * 在h和m文件間切換:cmd+ctrl+向上的方向鍵
*/
直接寫代碼感受下吧。下面這個(gè)Main方法中展示的OC的基本語法結(jié)構(gòu)轧简,包括
- 變量與數(shù)據(jù)類型
- 條件控制語句
- 循環(huán)控制語句
- 函數(shù)
- 面向?qū)ο蟮姆庋b、繼承匾二、多態(tài)
參考代碼:
Main中引入了兩個(gè)類:一個(gè)父類People、一個(gè)子類Man
//
// main.m
// HelloObjC
//
// Created by shitianci on 16/5/25.
// Copyright ? 2016年 Panda. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "People.h"
#import "Man.h"
/**
* 求面積的函數(shù)
*
* @param width 寬
* @param height 高
*
* @return <#return value description#>
*/
double getArea(double width, double height){
return width*height;
}
/**
* 打印主函數(shù)的信息
*
* @param argc 參數(shù)個(gè)數(shù)
* @param argv 參數(shù)列表
*
*/
void showInfo(int argc, const char * argv[]) {
NSLog(@"傳入?yún)?shù)%d個(gè)", argc);
for (int i = 0; i < argc; i++) {
NSLog(@"argv[%d] = %s", i, argv[i]);
}
return;
}
/**
* 入口函數(shù)
*
* @param argc 參數(shù)個(gè)數(shù)
* @param argv 參數(shù)列表
*
* @return 執(zhí)行結(jié)果
*/
int main(int argc, const char * argv[]) {
@autoreleasepool {
/**
* 必備插件:
* 1. Xcode plug-in:快速生成格式化的注釋 [VVDocumenter-Xcode](https://github.com/onevcat/VVDocumenter-Xcode)
* 必須記住的快捷鍵:
* * 格式化代碼(重排縮進(jìn)):ctrl+i
* * 生成格式化注釋: ///
* * 折疊察藐、展開代碼段 option+cmd+ ← | →
* * 在h和m文件間切換:cmd+ctrl+向上的方向鍵
*/
@autoreleasepool {
NSLog(@"Hello, %s, Welcome Object C World!" , "Panda");
showInfo(argc, argv);
#pragma mark 變量與數(shù)據(jù)類型
/**
* 變量
*/
{
int i = 1; //32bits, 4bytes
short s = 1; //16bits, 2bytes
long l = 1L; //32bits, 4bytes
long long ll = 1LL; //64bits, 8bytes
unsigned int ui = 1;
signed int si = -1;
float f = 0.1;
double d = 100.0;
char c = 'A';
char *string = "string";
NSLog(@"基礎(chǔ)數(shù)據(jù)類型:\n \t int: %d,\n \t float: %f,\n \t double: %lf,\n \t char: %c,\n \t string: %s\n" , i,f,d,c,string);
}
/**
* 表達(dá)式:跟C語言一致
*/
{
int a = 1;
NSLog(@"\n\ta 等于 %d, a++ 等于 %d", a, a++);
NSLog(@"\n\ta 等于 %d, ++a 等于 %d", a, ++a);
NSLog(@"\n\ta 等于 %d, a++ 等于 %d", a, a++);
int b = 2;
int c = 5;
int d = 31;
float f = 100.0f;
NSLog(@"\n\t (((a+b)*c)%%d)/f 等于 %f", (((a+b)*c)%d)/f);
}
#pragma mark 條件控制語句
/**
* 分支語句if
*/
{
int a = 1;
int b = 2;
if(a < b && 0 && YES){
NSLog(@"這句話是真的");
}
else if(!NO){
NSLog(@"這句話是假的");
}
}
/**
* 高級(jí)跳轉(zhuǎn)語句goto
*/
{
int i = 0;
a:
i++;
NSLog(@"i = %d", i);
if(i < 5){
goto a;
}
else{
goto b;
NSLog(@"不會(huì)被執(zhí)行到哦~");
}
b:
NSLog(@"跳到b了");
}
/**
* switch分支語句
*/
{
int i = 10;
switch (i) {
default:
NSLog(@"switch: i的值超出范圍");
// break;
case 1:
NSLog(@"switch: i的值等于1");
break;
case 2:
NSLog(@"switch: i的值等于2");
break;
}
}
#pragma mark 循環(huán)控制語句
{
int i = 0;
while (i < 5) {
i++;
NSLog(@"while: i = %d", i);
}
do {
i++;
NSLog(@" do …… while: i = %d", i);
} while (i < 5);
int j;
for (j = 0; j < 100 ;j++) {
if(j > 10){
break;
}
else if(j > 5){
NSLog(@"continue: j = %d", j);
continue;
}
NSLog(@"for: j = %d", j);
}
NSLog(@"break: j = %d", j);
}
}
#pragma mark 函數(shù)
{
double width = 10.0;
double height = 6.0;
NSLog(@"width = %f, height = %f, area = %f", width, height, getArea(width, height));
}
#pragma mark 面向?qū)ο?
{
/**
初始化
*/
People *p1 = [[People alloc]init];
People *p2 = [People new];
NSLog(@"p1 - %p", p1);
NSLog(@"p2 - %p", p2);
/**
* 成員變量和屬性
*/
NSLog(@"p1.peopleName - %@", p1.peopleName);
// NSLog(@"p1.peopleName - %s", [p1 peopleName]);
p2.peopleName = @"栗色";
NSLog(@"p2.peopleName - %@", p2.peopleName);
/**
* 方法
*/
NSLog(p2->_peopleOut);
[p2 testSubtractMethod];
[People testAddMethod];
[p2 showWithA:1];
[p2 showWithA:1 andB:2];
[p2 :1 :2];
People *p3 = [[People alloc]initWithPeopleName:@"王五" andPeopleAge:18];
[p3 showBaseInfo];
/**
* 封裝、繼承分飞、多態(tài)(oc只支持方法重寫悴务、不支持方法重載)
*/
Man *man = [[Man alloc]init];
[man showBaseInfo];
}
}
return 0;
}
//
// People.h
// HelloObjC
//
// Created by shitianci on 16/6/6.
// Copyright ? 2016年 Panda. All rights reserved.
//
#import <Foundation/Foundation.h>
/**
* 人(oc不支持多繼承)
*/
@interface People : NSObject
/**
* 成員變量(默認(rèn)為類內(nèi)使用)
*/
{
@public //可以在類外用 ->方式 進(jìn)行調(diào)用,(不推薦這樣使用)
NSString *_peopleOut;
// NSString *_peopleName;
@protected //在類內(nèi)可以使用譬猫,在類外不行,子類可以使用(默認(rèn)值)
int _peopleAge;
@private //在類內(nèi)可以使用染服,在類外不行肌索,子類不可以使用(默認(rèn)值)
int _peopleSex;
@package //框架權(quán)限,在框架內(nèi)相當(dāng)于@protected,在框架外相當(dāng)于@private
}
/**
* 屬性是成員變量的外部接口闸准,替代相應(yīng)的get益愈、set方法
*/
@property(nonatomic, strong) NSString *peopleName;
- (void) testSubtractMethod;
+ (void) testAddMethod;
//方法名為 "showWithA:"
- (int) showWithA:(int) a;
//方法名為 "showWithA: andB:"
- (int) showWithA:(int) a andB:(int)b;
//方法名為 ": :"
- (int) :(int) a :(int)b;
- (void)showBaseInfo;
- (instancetype)initWithPeopleName:(NSString * )peopleName andPeopleAge:(int)peopleAge;
@end
//
// People.m
// HelloObjC
//
// Created by shitianci on 16/6/6.
// Copyright ? 2016年 Panda. All rights reserved.
//
#import "People.h"
@implementation People
//成員
//@synthesize peopleName = _peopleName;
/**
* 重寫初始化方法
*
* @return 對(duì)象本身
*/
- (instancetype)init
{
self = [super init];
if (self) {
_peopleName = @"張三"; //系統(tǒng)會(huì)根據(jù)屬性默認(rèn)生成 _peopleName 這個(gè)成員變量
_peopleOut = @"直接調(diào)用對(duì)象成員變量(不推薦)";
}
return self;
}
- (void)showBaseInfo
{
NSLog(@"_peopleName: %@", _peopleName);
NSLog(@"_peopleAge: %d", _peopleAge);
NSLog(@"_peopleOut: %@", _peopleOut);
}
- (instancetype)initWithPeopleName:(NSString * )peopleName andPeopleAge:(int)peopleAge
{
self = [self init];
if (self) {
_peopleName = peopleName; //系統(tǒng)會(huì)根據(jù)屬性默認(rèn)生成 _peopleName 這個(gè)成員變量
_peopleAge = peopleAge;
}
return self;
}
- (void)testSubtractMethod
{
NSLog(@"這是一個(gè)減號(hào)方法");
}
+ (void)testAddMethod
{
NSLog(@"這是一個(gè)加號(hào)方法");
}
- (int)showWithA:(int)a
{
NSLog(@"方法 showWithA 攜帶參數(shù) a = %d", a);
return a;
}
- (int)showWithA:(int)a andB:(int)b
{
NSLog(@"方法 showWithA andB 攜帶參數(shù) a = %d,b = %d", a, b);
return a+b;
}
- (int) :(int) a :(int)b
{
NSLog(@"方法 : : 攜帶參數(shù) a = %d库快,b = %d", a, b);
return a+b;
}
@end
//
// Man.h
// HelloObjC
//
// Created by shitianci on 16/6/6.
// Copyright ? 2016年 Panda. All rights reserved.
//
#import "People.h"
@interface Man : People
@end
//
// Man.m
// HelloObjC
//
// Created by shitianci on 16/6/6.
// Copyright ? 2016年 Panda. All rights reserved.
//
#import "Man.h"
@implementation Man
- (void)showBaseInfo
{
NSLog(@"This is a Man摸袁。");
[super showBaseInfo];
}
@end
Panda
2016-06-06