iOS 类中变量的定义和使用

iOS 码拜 9年前 (2015-10-18) 1583次浏览
是对ios假如调用类中变量的方式的疑惑。

1-定义XYPoint接口文件

#import <Foundation/Foundation.h>
@interface XYpoint : NSObject
@property int x,y;
-(void) setX:(int)xVal andY:(int) yVal;
@end

2-XYpoint实现代码

#import "XYpoint.h"
@implementation XYpoint
@synthesize x,y;
-(void) setX:(int)xVal andY:(int)yVal
{
    x = xVal;
    y = yVal;
}
@end

3-rectangl类接口代码

#import <Foundation/Foundation.h>
@class XYpoint;
@interface Rectangl : NSObject
@property int width,height;
-(XYpoint *) origin;
-(void) setOrigin:(XYpoint *) pt;
-(void) setWidth:(int)w andHeight:(int)y;
-(int) area;
-(int) perimeter;
@end

4-rectangl实现代码

#import "Rectangl.h"
#import "XYpoint.h"
@implementation Rectangl
{
    XYpoint *origin;
}
@synthesize width,height;
-(void) setWidth:(int)w andHeight:(int)y
{
    width = w;
    height = y;
}
-(void) setOrigin:(XYpoint *)pt
{
    origin = pt;
}
-(int) area
{
    return width*height;
}
-(int) perimeter
{
    return (width+height)*2;
}
-(XYpoint *) origin
{
    return origin;
}
@end

5-主函数

#import <Foundation/Foundation.h>
#import "XYpoint.h"
#import "Rectangl.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Rectangl *myrect = [[Rectangl alloc]init];
        XYpoint *myPoint = [[XYpoint alloc]init];
        
        [myPoint setX:100 andY:200];
        [myrect setWidth:5 andHeight:8];
        
        //myrect.origin = myPoint;
        [myrect setOrigin:myPoint];
        
        //NSLog(@"myrect width = %i,height = %i",myrect.width,myrect.height);
        NSLog(@"myrect width = %i,height = %i",[myrect width],myrect.height);
        NSLog(@"origin at (%i,%i)",myrect.origin.x,myrect.origin.y);     //注释[1]
        NSLog(@"area = %i,perimeter = %i",[myrect area],[myrect perimeter]);
            }
    return 0;
}

---分割线---  // 

以下123是本人一开始的疑惑。

    1、rectangl接口文件中定义了setorigin方法,主函数中为什么没有用到?

    2、经过测试,在主函数中myrect.origin = myPoint;作用等同与[myrect setOrigin:myPoint]; 为什么?本人觉得

    使用[myrect setOrigin:myPoint];这种书写方式是更好的,原因是可以很直观的看到这是一个方法的调用。

    3、在rectangl实现代码中,定义了

    -(XYpoint *) origin

    {

       return origin;

    }  这个方法,这个方法的作用是什么呢?是在主函数中  注释1  哪个地方使用的么?为了使用对象mypoint中的私有参数?

      [myrect setOrigin:myPoint]方法不是已经把 对象mypoint赋予了rectangl类中的变量origin了么?

    

    -分割线    

    以上是本人一开始的疑惑,通过测试本人发现oc对变量对赋值和调用对模式,不知道对不对。以下是本人想法。

    1、在类中,当定义类一个变量时,如@property int x; 系统自动会生成一个-(void) setx:(int) xVal 的方法,这个

    方法是自动的,不需要在类的实现代码中手写;在主函数中可以直接使用 ;但是假如在类中使用另外一个类(暂时这样说);那么就

    必须手写一个 -(void) setFuncName:(FuncNmae *) value ; 的这样一个方法,系统不会像变量那样自动生成。

    而且这个方法的调用,跟本文主函数中那样,myrect.origin = myPoint 其实等同与 [myrect setOrigin:myPoint];

    

    2、第二个是关于在主函数中使用类的变量的问题。也是关于本人对本人刚开始疑惑(上面问题3)对思考。

     oc在类对接口文件中定义了一个变量x,@property int x; 第一系统自动定义了一个 -(void) setx:(int)xVal;对赋值方法。

     同时也自动定义了一个 -(int) x { return x}; 对方法。这辆个方法都不需要手工写(不使用@property这个方式定义变量的应该还是要手工写的吧?)

      我们在主函数中通过 调用-(void) setx:(int)x 方法,对类变量x赋值了;

    那么在使用的时候,nslog(@”x = %i”,myrect.x);其实等同于nslog(@”x = %i”,[myrect x]);myrect.x 其实就是调用了方法,对不对?

     那么回到我们本次的代码,本人在rectangl类中使用xypoint类,就必须手写一个返回方法,才能在主函数中使用对象mypoint的变量x,对么?

     本人又测试了一个,rectangl类的实现代码中,方法 origin不能改名,必须要和 XYpoint *origin;定义的对象名origin一样么?

    

    

解决方案:40分
@synthesis。 @synthesis对应于实现文件,@property对应于接口文件,这里有篇博客说的挺详细的。

http://justcoding.iteye.com/blog/1444548


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明iOS 类中变量的定义和使用
喜欢 (0)
[1034331897@qq.com]
分享 (0)