Dark Horse Programmer--oc Language learning experience-- attribute declarations and Assignments
-------Java training , Android training ,iOS training ,. Net Training , look forward to communicating with you! -------
1, all classes in OC inherit from the ultimate parent class object
2, the declaration of a character variable with n is a string *_xxx instance variable, usually preceded by an underscore
3, in OC, the method is distinguished by the +-number- The number beginning with the instance method or object method + is the class method pre-used object called by the latter with the class name called
5, Property assignment is available. The way or the pointer is assigned to the value
- @interface Person:nsobject
- {
- int _pid;
- NSString *_name;
- int _age;
- NSString *_email;
- NSString *_address;
- }
- Get Set method
- -(void) Setpid: (int) PID;
- -(int) returnpid;
- A simple method to implement the method of attribute declaration automatically Setget
- @property (nonatomic) int pid;//nonatomic represents non-atomic, non-thread-safe atomic represents thread safety
- @property NSString *name;
- @property int age;
- @property NSString *email;
- @property NSString *address;
- @end
Copy Code
- @implementation person
- Assign value
- -(void) Setpid: (int) pid{
- Access by pointer
- self->_pid=pid;
- }
- Take value
- -(int) returnpid{
- Wrong wording Self->return PID
- Return self->_pid;
- }
- The implementation of the property is then used directly. Call
- @synthesize Name=_name;
- @synthesize Age=_age;
- @synthesize Email=_email;
- @synthesize address=_address;
Copy Code
- #import <Foundation/Foundation.h>
- #import "Person.h"
- #import "Person+ext.h"
- int main (int argc, const char * argv[]) {
- @autoreleasepool {
- Insert code here ...
- NSLog (@ "First function!!");
- Create an instance
- Person *per=[[person Alloc]init];
- Call yourself to write the Setpid method to assign a value
- [Per setpid:1234567];
- Call the value method Returnpid return the new value
- int Pid=[per Returnpid];
- NSLog (@ "pid=%d", PID);
- Use the. Call implementation to assign a value
- [Email protected] "Zhinzik";
- per.age=20;
- [Email protected] "xxxxxxx";
- [Email protected] "Chengdu";
- Using the. Call implementation value '
- NSString *name=per.name;
- int age=per.age;
- NSString *email=per.email;
- NSString *address=per.address;
- NSLog (@ "%@\n%d\n%@\n%@", name,age,email,address);
- [Per Test];
- }
- return 0;
- }
Copy Code
-------Java training , Android training ,iOS training ,. Net Training , look forward to communicating with you! -------
Dark Horse programmer--oc Language learning experience--attribute declaration and assignment