OBJECTIVE-C Basic syntax: The difference between an instance variable and a member variable. Example method and class method difference

Source: Internet
Author: User



http://leopard168.blog.163.com/blog/static/16847184420138153296930/



http://blog.csdn.net/thdxs/article/details/10988343



1. The class method belongs to the entire class, not to an object.
2. Class methods can access only class member variables, cannot access instance variables, and instance methods access class member variables and instance variables.
3. The invocation of a class method can be done through the class name. class methods and objects. Class methods, and instance methods can only be accessed through the object. Instance method.
4. Class methods can only access class methods, and instance methods have access to class methods and instance methods.
5, the class method cannot be overwritten, the instance method can be overwritten.












My understanding: In



{



NSString * STR;



int A;



}



An instance variable is a variable that inherits the instantiation of a class declaration with OC



The member variable is the basic type






Abstract: Objective-C introduces the concept of " instance variables " , but at the same time, the sound of "member variables" is often seen. What is the difference between an instance variable and a member variable?

In Objective-c, defining a class requires two parts: the first is the interface (interface), and the second is the implementation (implementation). 
interface to the interface file, and the implementation corresponds to the implementation file. The interface file contains the declaration of the class, 
the member variable (member variable), and the method. interface files are usually. h

An implementation file is usually a. m file. The method that is declared in the interface needs to be implemented by x code in the. m file.



when viewing Apple's official documentation today (see below). Https://developer.apple.com/library/mac/referencelibrary/GettingStarted/Learning_Objective-C_A_Primer /index.html



A new noun was found: the member variable (member variable). It is novel because it is called an instance variable (instance variable). Remember, when first contact objective-c, see instance variable this noun, feel very novel. We also try to compare the instance variables with the member variables and study their differences. From this diagram below, it can be concluded that the instance variables I saw earlier, essentially, are member variables. For iOS beginners, a novel noun can easily make people think too much.



If you have been in contact with C + + before, the member variable (member Varialbe) is certainly not unfamiliar. Today, you see this familiar term, you will feel objective-c not as difficult as you think.



At this point, ears rang the slogan: The original formula, or the familiar taste. HA:)


in the figure "Member Variable declarations", literally means: "Member variable Declaration". It appears that the variable declared in {} is a member variable. So what does an instance variable mean? since OC is expressed in English, it may be useful to look up the original meaning in English.

Class:Class (Description/Template ForAnObject)
Example: Instance (manifestation of a class)
News: Message (Sent toObjectTo make It act)
Method: Method (Code invokedByAMessage Instance variable ( Object-specific storage)
superclass/subclass ( inheritance)
protocol:   protocol (non-class-specific methods)


From the English description given, it can be seen that: Instance is for the class. An instance is a declaration of a class; from this reasoning, an Instance Variable is an object declared by a class.
Strictly speaking, the int count; in the above figure is a member variable. And NSString * name; is an instance variable. As for id data should be a member variable or an instance variable? Because id is a OC-specific type. Essentially, id is equivalent to (void *).
  So id data should belong to the instance variable.

Related Article

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.