Dark Horse programmer----Three major features (encapsulation, inheritance, polymorphism)

Source: Internet
Author: User

I. Encapsulation (set method and get method)
    • Benefits of Encapsulation:
Filter out unreasonable values, shielding the interior of the assignment details, so that the outside world is not more concerned about the internal details.
    • Set Method:

1. Function: Provide a method to set the value of member variables to the outside world
2. Naming conventions:
1> method Name must be set start
2> set followed by the name of the member variable, and the first letter of the member variable must be uppercase
3> return value must be void
4> must accept a parameter, and the parameter type is consistent with the type of the member variable
5> parameter name cannot be the same as member variable name


-(void) Setage: (int//  method declaration -(void) Setage: (int// Method Implementation {  if0) {  1;  //  }  = newage;}
    • Get method:

1. Function: Returns the value of the member variable inside the object
2. Naming conventions:
1> must have a return value, and the return value type must be the same as the member variable type
2> the same method name as the member variable name
3> does not need to accept any parameters


-(int//  method declaration -(int//  method implementation {  return Age ;  }
    • Naming conventions for member variables

Naming conventions for member variables: Be sure to start with the underscore "_";

Effect: 1. To separate the name of the member variable from the Get method

2. Can be separated from the local variables, a see the beginning of the underscore variable, is usually a member variable

Code Exercise:
#import<Foundation/Foundation.h>@interfacestudent:nsobject{//member variables try not to use @public to ensure the security of the data, no @public can not be assigned by the object---member variable (access), which is provided a set method to set the value of the member variables (although there is no @public, However, the member variable can be accessed directly within the object method)//@public    intAge ;}- (void) Setage: (int) NewAge;- (int) age;- (void) study;@end@implementationStudent//implementation of Set method- (void) Setage: (int) newage{//Filter the parameters passed in    if(NewAge <=0) {NewAge=1; } Age=NewAge;}//implementation of the Get method- (int) age{returnAge ;}- (void) study{NSLog (@"students at the age of%d are studying", age);//the use of age here can be accessed directly even when declaring member variables without writing @public (the object method can directly access member variables)}@endintMain () {Student*stu = [StudentNew]; //assigning a member variable inside an object by calling the Set method[Stu setage:-Ten];        [Stu Study]; //since there is no @public this is called get method gets the member variable value inside the objectNSLog (@"The age of the student is%d", [Stu Age]); return 0;}

Dark Horse programmer----Three major features (encapsulation, inheritance, polymorphism)

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.