6 位址,Block,SEL,多型

2021-07-04 06:28:35 字數 4498 閱讀 8532

int maxvalue(int a,int b)

int main(int argc, const char * argv) ;

block();//--->暴風雨來了

//block自己不會執行,必須呼叫才會執行block大括號裡面的**段

//2.有引數無返回值

void (^maxblock)(int,int) = ^(int a,int b);

maxblock(6,9);//--->9

//void (^)(int,int)是block的型別,他的名字是block,我們也可以通過名進行呼叫

void (^arrblock)(nsarray *) = ^(nsarray *arr)

};nsarray *arr = @[@"1",@"2",@"3"];

arrblock(arr);//--->1,2,3

//3.無引數有返回值

int (^oneblock)() = ^;

nslog(@"%d",oneblock());//block呼叫沒有引數也得有()--->10

//4.有引數有返回值

int (^mblock)(int,int) = ^(int a,int b);

nslog(@"%d",mblock(3,8));//--->8

nsstring *(^ablcok)(nsarray *,nsinteger) = ^(nsarray *arr,nsinteger index);

nslog(@"%@",ablcok(@[@"1",@"2"],1));//--->2

//5.如果想改變block**塊之外的變數值,就必須在之前加入__block關鍵字

__block int x = 100;

int (^sumblock)(int a,int b) = ^int(int a,int b);

//block自己不會執行,必須呼叫才會執行block大括號裡面的**段

nslog(@"x = %d",x);//100

nslog(@"a+b=%d",sumblock(11,14));//25

nslog(@"x = %d",x);//25

//當在開發中乙個方法有多種實現的時候,如果不確定用哪種方法更好,我們可以考慮把方法中的乙個引數定義成block方式

calculate *cal = [[calculate alloc]init];

int sum = [cal calculatewithnumber1:10 andnumber2:20 andcalculate:sumblock];

nslog(@"a + b = %d",sum);//30

//通過用系統的block對陣列進行遍歷(用法用回車)

nsarray *array = [nsarray arraywithobjects:@"1",@"2",@"3",@"4", nil];

[array enumerateobjectsusingblock:^(id obj, nsuinteger idx, bool *stop) ];

//陣列排序陣列對字串的排序

//不可變陣列

nsarray *arr1 = @[@"zhan",@"wuhe",@"liusansan",@"wangjumin",@"hehehd"];

//sel叫方法選擇器,他是方法選擇器的型別

nsarray *arra = [arr1 sortedarrayusingselector:@selector(compare:)];

nslog(@"%@",arra);//--->hehehd,liusansan,wangjumin,wuhe,zhan

//可變陣列排序

nsmutablearray *array1 = [nsmutablearray arraywithobjects:@"zhan",@"wuhe",@"liusansan",@"wangjumin",@"hehehd",nil];

[array1 sortusingselector:@selector(compare:)];

nslog(@"---%@",array1);//--->hehehd,liusansan,wangjumin,wuhe,zhan

//對物件進行排序

//新建四個人的物件

person *per1 = [person basewithname:@"zhangxiaoyun" ***:@"nv"];

person *per2 = [person basewithname:@"wangya" ***:@"nv"];

person *per3 = [person basewithname:@"liushuxi" ***:@"nan"];

person *per4 = [person basewithname:@"shiyu" ***:@"nv"];

nsarray *arr2 = @[per1,per2,per3,per4];

nsarray *newarr = [arr2 sortedarrayusingcomparator:^nscomparisonresult(person *obj1, person *obj2) ];

for (person *per in newarr)

nsmutablearray *arr3 = [nsmutablearray arraywithobjects:per1,per2,per3,per4,nil];

[arr3 sortusingcomparator:^nscomparisonresult(id obj1, id obj2) ];

for (person *per in arr3)

//---------------------------//sel資料型別------------------------------------

calculate *person = [[calculate alloc]init];

//[person eat];//訊息機制

//使用@select就能夠把乙個方法包裝成sel資料型別

sel s1 = @selector(eat);

[person performselector:s1];

//上面兩句合併等價於下面一句

[person performselector:@selector(eat)];

sel s2 = @selector(call:);

[person performselector:@selector(call:) withobject:@"15382006306"];

sel s3 = @selector(fingname);

nsstring *str = [person performselector:s3];

nslog(@"%@",str);

}

return 0;

#import 

typedef

int (^calculateblock)(int a,int b);

@inte***ce

calculate : nsobject

- (int)calculatewithnumber1:(int)number1 andnumber2:(int)number andcalculate:(calculateblock)calculate;

- (void)eat;

- (void)call:(nsstring *)number;

-(nsstring *)fingname;

//簡單的多型-------------------------------------------->

@property(nonatomic,copy)nsstring *name;

@property(nonatomic,copy)nsstring ****;

- (id)initwithname:(nsstring *)name ***:(nsstring *)***;

+ (id)basewithname:(nsstring *)name ***:(nsstring *)***;

@end

#import "calculate.h"

@implementation

calculate

- (int)calculatewithnumber1:(int)number1 andnumber2:(int)number2 andcalculate:(calculateblock)calculate

- (void)eat

- (void)call:(nsstring *)number

-(nsstring *)fingname

//簡單的多型-------------------------------------------->

- (id)initwithname:(nsstring *)name ***:(nsstring *)***

return

self;

}+ (id)basewithname:(nsstring *)name ***:(nsstring *)***

@end

IPv6 位址格式

ipv6 位址大小為 128 位。首選的 ipv6 位址表示為 x x x x x x x x 其中每個 x 是代表乙個 4 位的十六進製制數字。ipv6 位址範圍從 0000 0000 0000 0000 0000 0000 0000 0000 至 ffff ffff ffff ffff ffff...

IPV6位址格式

一 單播位址 unicast ipv6addresses 1.可聚合的全球單播 aggregatable global unicast 即global位址,全球範圍內可達 2.鏈路本地位址 link local 用於同乙個鏈路上相鄰節點之間通訊。ipv6的路由器不會 鏈路本地位址的資料報。link ...

IPv6位址格式

ipv6 位址格式 ipv6 的位址長度是 128位 bit 將這128位的位址按每 16位劃分為乙個段,將每個段轉換成十六進製制數字,並用冒號隔開。例如 2000 0000 0000 0000 0001 2345 6789 abcd 這個位址很長,可以用兩種方法對這個位址進行壓縮,前導零壓縮法 將...