Dark Horse Programmer--oc Language Set method and get method

Source: Internet
Author: User

@public members can be arbitrarily assigned, you should use the Set method and the Get method to manage access to members

A) Set method

1. Function: Provide a method to set the value of member variables to the outside world, can be in the method to face the parameters of the corresponding filter

2. Naming conventions:

1> method name must begin with set

2> set followed by the name of the member variable, the first letter of the member variable must be capitalized

3> return value must be void

4> must receive a parameter, and the parameter type is consistent with the member variable type

The name of the 5> parameter cannot be the same as the member variable name

II) Get method

Get method

1. Function: Returns 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

The 2> method name is the same as the member variable name

3> does not need to receive any parameters

c) member variable naming specification

The member variable must begin with an 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

IV) Practice

Design a grade class
* C language Score (readable and writable)
* OC result (readable and writable)
* Total score (read only)
* Average score (read only)

Analysis: Readable and writable are both set and get methods to write

Read-only is write-only get method

Write your own code as follows

Dark Horse Programmer--oc Language Set method and get method

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.