开发者

Objects and Classes

I come from a java background, I know how to call methods from objects but would the following be impossible?

main(){
     int v1=2;
     int v2=3:
     int sum;
     sum = [Add: (int) v1, (v2)];
}
add ((int) v1, (int) v2)){
    return v1+v2;
}

And my second question, How would I define a variable that is static throughout every instance of an object. For example, If i want the开发者_如何学C variable tax_rate to be .07 in every instance of an object.


Methods are written and invoked a bit differently in Objective-C. A method definition might look like this:

- (int)addThis:(int)v1 andThat:(int)v2
{
    return v1 + v2;
}

And you would invoke it like so:

int v1 = 2;
int v2 = 3;
int sum = [self addThis:v1 andThat:v2];

The syntax with the brackets is just for methods on objects (or classes). If you're calling a plain old function, the syntax is the same as in C.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜