嗨我想學習目標C中的Opps概念,但我知道PHP,所以我採取了公共,私人和保護提到的波紋管程序。公共私人和保護在objective-c
<?php
//Public properties and method can be inherited and can be accessed outside the class.
//private properties and method can not be inherited and can not be accessed outside the class.
//protected properties and method can be inherited but can not be accessed outside the class.
class one
{
var $a=20;
private $b=30;
protected $c=40;
}
class two extends one
{
function disp()
{
print $this->c;
echo "<br>";
}
}
$obj2=new two;
$obj2->disp(); //Inheritance
echo"<br>";
$obj1=new one;
print $obj1->c; //Outside the class
?>
因此,我試圖在下面提到的Objective C代碼中進行轉換。
#import <Foundation/Foundation.h>
@interface one : NSObject
{
@private int a;
@public int b;
@protected int c;
}
@property int a;
@property int b;
@property int c;
@end
@implementation one
@synthesize a,b,c;
int a=10;
int b=20;
int c=30;
@end
@interface two : one
-(void)setlocation;
@end
@implementation two
-(void)setlocation;
{
// NSLog(@"%d",a);
NSLog(@"%d",b);
// NSLog(@"%d",c);
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
two *newtwo;
newtwo =[[two alloc]init];
//calling function
[newtwo setlocation];
}
return 0;
}
當我運行上面的代碼我得到
2015-11-03 23:20:16.877 Access Specifier[3562:303] 0
有人能解決我的問題。
你正在編譯iOS或OS X嗎? –
我正在編譯OS X命令行工具。只是想知道公共私人和受保護的概念。 – VyTcdc