2012-08-27 60 views

回答

1

在objective-c中,您有properties用于有效自动创建访问实例变量的getter和setter。

@interface MyClass 
{ 
    UILabel *instanceLabel; //Not required, but I find it can make it clearer 
} 

@property (nonatomic, retain) UILabel *instanceLabel; 

@end 



@implementation MyClass 

@synthesize instanceLabel; //Not required as of XCode 4.4 
@end 

然后从您的其他类中使用点运算符访问这些属性的简单情况。

myClassInstance.instanceLabel.text = @"newText"; 

您不必使用点操作:

[myClassInstance instanceVariable].text = @"newText"; 
+0

我需要将头部导入myClassInstance类吗? – user1628311

+0

是的。您还需要创建一个MyClass的实例来使用。 –

-1

通常人们为此使用set-functions。

即。伪代码:然后

class YourClass 
{ 
    private var str; 

    public YourClass(var str) 
    { 
     this.str = str; 
    } 

    public setString(var str) 
    { 
     this.str = str; 
    } 

} 




class SecondClass 
{ 
    private final YourClass myInstance; 

    public SecondClass(final YourClass myInstance) 
    { 
     this.myInstance = myInstance; 
    } 

    public performChange() 
    { 
     myInstance.setString("hello"); 
    } 
} 

调用二等:: performChange()会改变 “YourClass将myInstance的” 实例变量 “你好”。