i can't understand how make a variable private in Object-C, in Java i can do it in this way:
public class Counter {
private int cont;
public Counter(){
cont = 0;
}
public Counter(int v){
cont = v; }
public void setValue(int v){
cont = v;
}
public void inc(){
cont++; }
public int getValue(){
return cont;
}
}
and then:
public class MyClass extends Counter {
public static void main(String[] args) {
Counter myC = new Counter();
System.out.println(myC.getValue());
//System.out.println(myC.cont); don't work because it's private
}
}
so i can't access at the variable myC.cont because obviously it's private, in Object-C i make the same thing but don't work:
@interface Counter : NSObject {
@private int count;
}
- (id)initWithCount:(int)value;
- (void)setCount:(int)value;
- (void)inc;
-(int)getValueCount;
@end
#import "Counter.h"
@implementation Counter
-(id)init {
count = 0;
return self;
}
-(id)initWithCount:(int)value {
self = [super init];
[self setCount:value];
return self;
}
- (void)setCount:(int)value {
count = value;
}
- (void)inc {
count++;
}
-(int)getValueCount {
return count;
}
@end
and then call it from the main.m:
#import "Counter.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSLog(@"Hello, World!");
Counter *myC = [[Counter alloc] init];
[myC inc];
[myC inc];
[myC inc];
myC.count = 1;
NSLog(@"%d",myC.getValueCount); //it's 1 instead of 3
}
return 0;
}
i can't understand i can access at the count variable, how i can make it private like in java?