【Objective-C】OC中的許可權控制

來源:互聯網
上載者:User

1:預設的執行個體變數的範圍範圍是:這個類體內.預設繼承的執行個體變數可以直接使用。

2:執行個體變數的許可權控制符號

3:預設是projected

4:oc中只支援單繼承

5:共性的東西放在父類,個性的東西放在子類

修飾符 類內部 子類 任何地方
private yes

projected yes
yes
yes
public yes
yes
yes

下面使用汽車的繼承關係來示範一下預設的許可權(protected)

一:汽車Car.h

    

////  Car.h//  Car////  Created by hmjiangqq on 14-1-22.//  Copyright (c) 2014年 hmjiangqq. All rights reserved.//#import <Foundation/Foundation.h>@interface Car : NSObject{    @protected    //預設許可權    int cID;         //id    float speed;     //車速    NSString *name;  //名稱}-(void)run;@end
二:汽車Car.m
   
////  Car.m//  Car////  Created by hmjiangqq on 14-1-22.//  Copyright (c) 2014年 hmjiangqq. All rights reserved.//#import "Car.h"@implementation Car//執行個體化汽車的屬性-(id)init{    if(self=[super init]){        cID=1000;        speed=120;        name=@"小汽車";    }    return  self;}-(void)run{    NSLog(@"start ...");}@end
三:BMW.h
   
////  BMW.h//  Car////  Created by hmjiangqq on 14-1-22.//  Copyright (c) 2014年 hmjiangqq. All rights reserved.//#import "Car.h"@interface BMW : Car@end
四:BMW.m
  
////  BMW.m//  Car////  Created by hmjiangqq on 14-1-22.//  Copyright (c) 2014年 hmjiangqq. All rights reserved.//#import "BMW.h"@implementation BMW-(void)run{    name=@"寶馬";    NSLog(@"car name is %@\n",name);}@end
main.m
  
////  main.m//  Car////  Created by hmjiangqq on 14-1-22.//  Copyright (c) 2014年 hmjiangqq. All rights reserved.//#import <Foundation/Foundation.h>#import "Car.h"#import "BMW.h"int main(int argc, const char * argv[]){    @autoreleasepool {                // insert code here...        NSLog(@"Hello, World!");        Car *car=[[Car alloc]init];        [car run];        BMW *bmw=[[BMW alloc]init];        [bmw run];    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.