OC basics: inheritance. Initialization Method, convenient Constructor

Source: Internet
Author: User

OC basics: inheritance. Initialization Method, convenient Constructor

Inheritance:

1. Unidirectional inheritance. A class can have only one parent class, and a parent class can have multiple child classes.

2. Unidirectional inheritance. The base class (root class) is OSObject.

3. Subclass can inherit the attributes and methods of the parent class

 

When the method of the parent class does not meet the requirements of the subclass, The subclass can override the method of the parent class, override the method of the parent class, and do not need to declare it again in the subclass.

1. completely rewrite

2. Partially rewrite using super

 

After an inheritance relationship is established, the Child class can inherit from the parent class:

1. instance variable, @ public @ protected

2. Public Methods

If a method is declared in. h, this method is a public method. If it is not declared, it is private.

There is no absolute private method in OC.

 

Process of sub-class calling method:

First, search for methods in this class. If the methods are found, call them directly. If the methods are not found, search for them in the parent class. If the methods are not found in the parent class, the next step is to look up (the parent class of the parent class) until it is found.

If it cannot be found, an exception is thrown, causing crash.

 

Objects can be assigned values. objects are pointers.

 

The custom initialization method does not need to be declared in the. h file because it is overwritten.

 

 

 

The traversal constructor must be a class method.

Return Value Type (id, instancetype)

 

 

Custom initialization method: initialization + assignment.

Convenience constructor: Open up space + initialization + assignment.

 

Custom initialization method:

 

 

-(Id) initWithColor :( NSString *) color

Price :( NSString *) price {

If (self = [super init]) {

_ Price = price;

_ Color = color;

}

Return self;

}

Constructor:

 

 

+ (Id) appleWithColor :( NSString *) color

Price :( NSString *) price {

Apple * apple = [[Apple alloc] initWithColor: color price: price];

Return apple;

}



 

 

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.