Constructor Singleton Mode

Source: Internet
Author: User

1. If the setter or getter method is specified during declaration, the method specified by the user is called by default when the vertex operator method is used.
2. Singleton: uniqueness. For example, the Chairman can only have one
3. Convenient constructor and Singleton Mode
4.-Minus signs represent the instance method, that is, the object method, which can only be called by the object
+ The plus sign represents the class method, called by the class, and does not depend on the specific object. It is equivalent to the method declared by static. The space is opened before other methods. When the program is loaded, the space is opened.
5. multi-parameter functions
-(Void) setname :( nsstring *) newname
Andage :( INT) newage
Andaddress :( nsstring *) newaddress
Andholobby :( nsstring *) newholobby;
This method is used to assign values to multiple instance variables at the same time. Using this method alone is not convenient for output in the main function. It is easier for the main function output to call attributes using the vertex method.
6. ID represents any type of pointer, such as (void *) in C *)
7.-(student *) initname :( nsstring *) newname //: method name on the left side of the colon, and form parameter on the right side
Andage :( INT) newage
Andaddress :( nsstring *) newaddress
Andholobby :( nsstring *) newholobby
{
Self = [Super init]; // initialize the parent class and assign it to self.
If (Self) {// If self is not empty, that is, the parent class is initialized and assigned a value to the subclass. The if judgment statement is entered.
Self. Name = newname; // use self to point to the object passed by the call function. Self directly points to the call object without opening up space.
If (newage <18 ){
Newage = 18;
}
Self. Age = newage;
Self. Address = newaddress;
Self. holobby = newholobby;
}
Return self; // If the if judgment body is not entered, a null pointer NIL is returned.
}
Self indicates the currently called object, that is, the object to which the method is called, and the returned value is the object of the student class. The returned value is the pointer to the object. Here is self, that is, the object that calls this method
8. Constructor (factory Mode)
+ (ID) studentwithname :( nsstring *) newname // ID represents any type of pointer. Here it represents student *, Class Method
Andage :( INT) newage
Andaddress :( nsstring *) newaddress
Andholobby :( nsstring *) newholobby
{
Student * Stu = [[STUDENT alloc] initname: newname andage: newage andaddress: newaddress andholobby: newholobby];
Return [STU autorelease]; // automatically released
}
9. Singleton Mode
Use static to declare the Object Pointer static Singleton * st;
(+) In the plus sign method, if the object pointer is nil, the space is opened and initialized, and the single-instance object pointer is returned.
@ Implementation Singleton
Static Singleton * st;
+ (ID) instance {
@ Synchronized (Self) {// keyword, synchronous, indicates that only one thread can access
If (! St ){
St = [[Singleton alloc] init];
}
Return st;
}
}
Personal Understanding: Singleton is a method for creating objects. It defines the static Class Object st. When you create an object through the class method, the static pointer defined is returned, therefore, the memory address remains unchanged. When you call this method to create an object, it points to the same memory, because there are judgment statements in the method, as long as the pointer st is not empty, it will directly return the pointer St.

Class Code:
**************************************** **********************************
// Student. h
// Oc_class3 constructor and Singleton Mode
//
// Created by stjy on 13-1-17.
// Copyright (c) 2013 stjy. All rights reserved.
//

# Import

@ Interface Student: nsobject
@ Property (assign, nonatomic) int age;
@ Property (retain, nonatomic) nsstring * Name, * address, * holobby;
+ (ID) instanceofstudent;
// Method function: Assign the specified value to the attributes of the student object
//-Minus signs indicate the instance method, that is, the method can only be called by the object, and the method name is setname: andage: andaddress: andholobby:
// The Return Value of the method is void.
// Method parameter (nsstring *) newname indicates the Student name, (INT) newage indicates the student age, (nsstring *) newaddress indicates the student address, and (nsstring *) newhober indicates the students' hobbies.
-(Void) setname :( nsstring *) newname //: The method name on the left side of the colon, and the form parameter on the right side
Andage :( INT) newage
Andaddress :( nsstring *) newaddress
Andholobby :( nsstring *) newholobby;
// Method function: Specifies the initialization content for the custom initialization function of the student class. The returned value is student *, indicating that an available student object is returned.
-(Student *) initname :( nsstring *) newname //: method name on the left side of the colon, and form parameter on the right side
Andage :( INT) newage
Andaddress :( nsstring *) newaddress
Andholobby :( nsstring *) newholobby;
// Convenient constructor for student classes to facilitate student object creation. alloc init is omitted, and the plus sign (+) indicates that this method is a class method, that is, it can only be called by class name, because the related object is not generated, but we want to call the relevant method, we can only call the class method. The method name prefix is a class name, and the first character is in the lower case of studentwithname: andage: andaddress: andholobby :. Return Value ID, which indicates that any type of OC pointer object is returned (like void * in C *)
+ (ID) studentwithname :( nsstring *) newname // ID indicates any type of pointer. Student *
Andage :( INT) newage
Andaddress :( nsstring *) newaddress
Andholobby :( nsstring *) newholobby;
-(Void) sayhi;
@ End




//
// Student. m
// Oc_class3 constructor and Singleton Mode
//
// Created by stjy on 13-1-17.
// Copyright (c) 2013 stjy. All rights reserved.
//

# Import "student. H"

@ Implementation student
@ Synthesize age = _ age, name = _ name, address = _ address, holobby = _ holobby;
Static student * instance;
+ (ID) instanceofstudent {
@ Synchronized (Self) {// keyword, synchronous, indicates that only one thread can be accessed at a time. The self in the brackets is the pointer to whom to synchronize, which is equivalent to the atomic in the attribute.
If (instance = nil ){
Instance = [[STUDENT alloc] init];
}
Return instance;
}
}
-(Void) setname :( nsstring *) newname //: The method name on the left side of the colon, and the form parameter on the right side
Andage :( INT) newage
Andaddress :( nsstring *) newaddress
Andholobby :( nsstring *) newholobby
{
_ Name = newname;
If (newage <18 ){
Newage = 18;
}
_ Age = newage;
_ Address = newaddress;
_ Holobby = newholobby;
}
-(Student *) initname :( nsstring *) newname //: method name on the left side of the colon, and form parameter on the right side
Andage :( INT) newage
Andaddress :( nsstring *) newaddress
Andholobby :( nsstring *) newholobby
{
// Student * Stu = [STUDENT alloc];
// Stu. Name = newname;
// If (newage <18 ){
// Newage = 18;
//}
// Stu. Age = newage;
// Stu. Address = newaddress;
// Stu. holobby = newholobby;
// Return Stu;
Self = [Super init]; // initializes instance variables and instance methods inherited by self through parent class initialization.
If (Self) {// If self is not empty, that is, the parent class is initialized and assigned a value to the subclass. The if judgment statement is entered.
Self. Name = newname; // use self to point to the object passed by the call function. Self directly points to the call object without opening up space.
If (newage <18 ){
Newage = 18;
}
Self. Age = newage;
Self. Address = newaddress;
Self. holobby = newholobby;
}
Return self; // If the if judgment body is not entered, a null pointer NIL is returned.

}
// Constructor
+ (ID) studentwithname :( nsstring *) newname // ID represents any type of pointer. Here it represents student *, Class Method
Andage :( INT) newage
Andaddress :( nsstring *) newaddress
Andholobby :( nsstring *) newholobby
{
Student * Stu = [[STUDENT alloc] initname: newname andage: newage andaddress: newaddress andholobby: newholobby];
Return [STU autorelease]; // automatically released
}
-(Void) sayhi {
Nslog (@ "My name is % @, % d this year, from % @, hobby % @", _ name, _ age, _ address, _ holobby );
}
@ End

**************************************** **********************************
# Import "student. H"

# Import

Int main (INT argc, const char * argv [])
{

@ Autoreleasepool {
Student * Stu = [[STUDENT alloc] init];
[STU setname: @ "zzh" andage: 20 andaddress: @ "Baoding" andholobby: @ "reading"];
[STU sayhi];
Student * stu2 = [[STUDENT alloc] initname: @ "zht" andage: 21 andaddress: @ "Langfang" andholobby: @ "study"]; // call the function, directly open up space and assign values
[Stu2 sayhi];
Student * stu3 = [STUDENT studentwithname: @ "mmm" andage: 19 andaddress: @ "Shijiazhuang" andholobby: @ "playing"];
[Stu3 sayhi];
// Example stu4/stu5/stu6
Student * stu4 = [STUDENT instanceofstudent]; // stu4/stu5/stu6 points to the same address. This is a singleton. There will always be only one object.
[Email protected] "Baiyun ";
Stu4.age = 20;
Student * stu5 = [STUDENT instanceofstudent];
Student * stu6 = [STUDENT instanceofstudent];
[Stu5 sayhi];
[Stu6 sayhi];
}
Return 0;
}


**************************************** **********************************







Courseware assignments
**************************************** **********************************
Parent class


//
// Men. h
// Courseware assignments
//
// Created by stjy on 13-1-20.
// Copyright (c) 2013 huibao_er. All rights reserved.
//

# Import

@ Interface men: nsobject
@ Property (retain, nonatomic) nsstring * Name, * Sex, * job;
@ Property (assign, nonatomic) int age;

-(Void) sayhi;
+ (ID) menwithname :( nsstring *) aname
Andsex :( nsstring *) asex
Andage :( INT) anage
Andjob :( nsstring *) ajob;

-(ID) initwithname :( nsstring *) aname
Andsex :( nsstring *) asex
Andage :( INT) anage
Andjob :( nsstring *) ajob;

@ End





//
// Men. m
// Courseware assignments
//
// Created by stjy on 13-1-20.
// Copyright (c) 2013 huibao_er. All rights reserved.
//

# Import "men. H"

@ Implementation men
@ Synthesize name, sex, age, job;


-(ID) initwithname :( nsstring *) aname
Andsex :( nsstring *) asex
Andage :( INT) anage
Andjob :( nsstring *) ajob
{
If (Self = [Super init]) {
Self. Name = aname;
Self. Sex = asex;
Self. Age = anage;
If (self. age> 100 | self. age <0 ){
Self. Age = 18;
}
Self. Job = ajob;
}
Return self;

} // Initialization Method

+ (ID) menwithname :( nsstring *) aname
Andsex :( nsstring *) asex
Andage :( INT) anage
Andjob :( nsstring *) ajob
{
Men * Man = [[Men alloc] initwithname: aname andsex: asex andage: anage andjob: ajob];
Return [Man autorelease];
}




-(Void) sayhi
{
Nslog (@ "My name is % @, gender is % @, age is % d, work is % @. ", Self. Name, self. Sex, self. Age, self. Job );
}

@ End
**************************************** ********************************
//
// King. h
// Courseware assignments
//
// Created by stjy on 13-1-20.
// Copyright (c) 2013 huibao_er. All rights reserved.
//

# Import
# Import "men. H"

@ Interface King: Men

+ (ID) instanceofking;
@ End


//
// King. m
// Courseware assignments
//
// Created by stjy on 13-1-20.
// Copyright (c) 2013 huibao_er. All rights reserved.
//

# Import "King. H"

@ Implementation king

Static king * instance;
+ (ID) instanceofking
{
@ Synchronized (Self)
{
If (instance = nil ){
Instance = [[King alloc] init];
}
Return instance;
}
} // Singleton Mode

@ End
**************************************** **********************************
//
// Worker. h
// Courseware assignments
//
// Created by stjy on 13-1-20.
// Copyright (c) 2013 huibao_er. All rights reserved.
//

# Import "men. H"

@ Interface worker: Men

@ End




//
// Worker. m
// Courseware assignments
//
// Created by stjy on 13-1-20.
// Copyright (c) 2013 huibao_er. All rights reserved.
//

# Import "worker. H"

@ Implementation worker

@ End
**************************************** ******************************

//
// Farmer. h
// Courseware assignments
//
// Created by stjy on 13-1-20.
// Copyright (c) 2013 huibao_er. All rights reserved.
//

# Import "men. H"

@ Interface farmer: Men

@ End



//
// Farmer. m
// Courseware assignments
//
// Created by stjy on 13-1-20.
// Copyright (c) 2013 huibao_er. All rights reserved.
//

# Import "farmer. H"

@ Implementation farmer

@ End
**************************************** ********************************
//
// Actor. h
// Courseware assignments
//
// Created by stjy on 13-1-20.
// Copyright (c) 2013 huibao_er. All rights reserved.
//

# Import "men. H"

@ Interface actor: Men

@ End


//
// Actor. m
// Courseware assignments
//
// Created by stjy on 13-1-20.
// Copyright (c) 2013 huibao_er. All rights reserved.
//

# Import "actor. H"

@ Implementation actor

@ End
**************************************** *********************************

From: http://blog.sina.com.cn/s/blog_bf9eee6f0101h6nh.html

Constructor Singleton Mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.