Objective-C: 04 _ object-oriented-three major features

Source: Internet
Author: User

Three features: encapsulation, inheritance, and Polymorphism

  Encapsulation:Encapsulation of member variables in order to filter invalid attribute values, we need to provide a method to set the age attribute values for the outside world. Set Method Purpose: provide a method to set the member variable value for the outside world. Name: The method name must start with set. Set is followed by the name of the member variable. The first letter of the member variable must be capitalized. The return value must be void. Be sure to receive a parameter, and the parameter type is consistent with the member variable type. The name of the parameter variable cannot be the same as that of the member variable. Get method: Purpose: return the value of the member variable inside the object. Naming rules: There must be a return value. The return value type must be consistent with the member variable type. The method name is the same as the member variable name. No parameters need to be receivedDo not use @ public enumeration to define member variables: typedof Enum {sexman, sexwoman} sex; Naming rules for member variables: Start With underscore _ Purpose: 1. Separate member variables from get methods. 2. It can also be separated from local variables. When you see variables starting with an underscore, they are generally member variables. In essence, OC is still a C language, but it is also a line-by-line process. Or weak syntax. No declaration is not implemented: Only warnings are triggered when a method without declaration is called during compilation. Only the following error is reported during running: -[Person test]: Unrecognized selector sent to instance 0x7fe9804076a0: An unrecognized message is sent to the person object: Test   The statement is not implemented: If the object declares a method but does not implement it, no error will be reported during compilation and only a warning will be given. An error is reported only during execution (the error is the same as the above ). The OC checks whether the object implements the corresponding method (dynamic detection) during running)   There is no declared implementation:In this case, there is no warning or error. The program will crash when an error occurs during running. Class method: All class methods start with the plus sign (+ ). Then, call [person printclassname] using the class name directly.Class methods can only be called by class names, but cannot be called by objects. The method names of allowed class methods are the same as those of object methods. Instance variables cannot be accessed in class methodsObject method:-> minus sign-start-> only objects can be called. -"Member variables (instance variables) that can access the current object in the object Method)Class Method:-"plus sign + beginning-" can only be called by class (name) -"Instance variables cannot be accessed in class methods Benefits and usage of class methods: -Object-independent, high execution efficiency -"Class methods can be used whenever possible. -"Occasion: when a member variable is not needed inside a method, you can change it to a class method. Tool class:There are basically no member variables, and the methods are basically class methods. Self keyword SELF: who calls the current method, self represents who (if self appears in the object method, self represents the object. If Self appears in the class method, self indicates the class) When the member variables and local variables have the same name, the proximity principle is adopted to access local variables. Use self to access member variables and distinguish local variables with the same name You can use "self-> member variable name" to access the member variables in the current object. You can use the [self method name] to call other object Methods \ class methods. Whether in a class method or an object method, if self is called, an endless loop is formed. The self in the class method cannot call the object method, and the self in the object method cannot call the class method. Self cannot call Functions Inheritance:Benefits of inheritance: 1. Repeated code extraction 2. Relationships between classes are established 3. subclasses can have all member variables and methods in the parent class. Note: Basically, the root class of all classes is nsobject. If it is written in the same file, the parent class must be written in front of S. Member variables with the same name of child classes and parent classes are not allowed in OC. Subclass re-implements a method of the parent class and does not need keywords like those in C #. When a method is called, it is first found in the current class. If it cannot be found, it is found in the parent class. Each class has a superclass pointer pointing to its parent class.Disadvantages of inheritance: coupling is too strong. Therefore, inheritance cannot be used in disorder.Inheritance application scenarios: 1. When two classes have the same attributes and methods, you can extract the same things to a parent class. 2. When Class A has some attributes and methods in Class B, you can consider B inheriting Class: XX is a combination of XXX: XXX has a combination of XXX: (the student inherits the score, which is logically unavailable. In this case, you can use a combination) @ interface score: nsobject {int _ cscore; int _ ocscore ;} @ end @ implementation score @ end @ interface Student: nsobject { Score * _ score; // This method is a combination.Int _ age ;}@ end @ implementation student @ end Super: Call the method of the parent class. (Use [Super method name] to call the test method or other methods of the parent class in the test method of rewriting the parent class) Super is in the object method, so it will call the object method in the parent class Super is in the class method, then it will call the class method in the parent class Use Cases: When a subclass overrides the method of the parent class, it wants to retain some behavior of the parent class. Polymorphism Parent class pointer pointing to subclass object No polymorphism without inheritance Code embodiment: a pointer to a subclass object of the parent class Benefit: If the function \ method parameter uses the parent class type, you can pass in the parent class and subclass object Limitations: A variable of the parent class type cannot directly call the method unique to the subclass. You must convert it to a variable of the subclass type before you can directly call the method unique to the subclass.When a method is called, the real image of the object is detected. If the parameter uses the parent class type, the limitations of the polymorphism of the parent class and subclass object can be passed in: A variable of the parent class cannot be used to call the subclass method.The dog class inherits from the animal class animal * AA = [dog new]; dog * dd = ( Dog *) AA ; // Forced type conversion: converts AA to a variable of the dog * typeIf something is wrong, please correct it !!

Objective-C: 04 _ object-oriented-three major features

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.