OC, oc Language

Source: Internet
Author: User

OC, oc Language

I. Encapsulation

1> encapsulation Definition

  • Hides the attributes and implementation details of an object and only exposes the interface to control the read and modify access levels of attributes in the program.

2> advantages of encapsulation

  • You can use the set method to prevent improper values for member variables.
  • Only public interfaces are provided to the outside, and the operation process of member variables is blocked.
  • External users can indirectly manipulate member variables through interfaces without having to focus on internal details.

3> set and get Methods

Ii. Inheritance

1> inherited Definitions

  • An object uses the attributes and methods of another object directly, and extends its own unique attributes and methods.

2> advantages and disadvantages

3> basic usage

  • You can use the member variables and methods inherited from the parent class.
  • You can expand your own methods as needed
  • You can use the same name method in the subclass to override the method of the parent class.
  • When calling a method, find the method in the current class. If the method cannot be found, find it in the parent class.

4> usage notes

  • User-Defined classes generally inherit from the root class in the system, such as NSObject.
  • The inheritance of OC language is single inheritance
  • Only non-private member variables and methods of the parent class can be inherited.
  • Inheritance cannot reduce the member variables and methods in the parent class.
  • Member variables with the same name as the parent class are not allowed in subclass
  • The parent class must be declared before the subclass.

5> inheritance and combination

6> self and super

Iii. Polymorphism

1> concept of dynamic binding

  • Determine the actual type of the referenced object during the execution of the program, and call the corresponding method based on the actual type.

2> basic usage

  • Polymorphism usually refers to multiple forms
  • Represents a parent class pointer pointing to a subclass object
  • Dynamic binding of Classes

3> advantages and disadvantages

4> usage notes

  • Polymorphism must be based on inheritance.
  • During running, the system dynamically detects the real object pointed to by the pointer.
  • The OC language is a weak syntax. You can use any type of pointer to point to a known object and only issue a warning.

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.