Initialization of the parent class and subclass of Init

Source: Internet
Author: User

To set the initialization method of a variable pass parameter

-(Instancetype) Initwithhour: (int) H and Minute: (int) m and second: (int) s;

And then in the implementation section

-(Instancetype) Initwithhour: (int) H and Minute: (int) m and second: (int) s;

{

Self=[super Init];

if (self!) =nil) {

Hour=h;

Minute=m;

Second=s;

}

}

Then the main function below is directly assigned

Time*tm2=[[time alloc]initwithhour:13 and minute:30 and second:0];

Background Direct Display 13:30:00

Overriding Init only needs to be written in the implementation section directly and does not need to be re-declared in the declared. h file

Init is a method of NSObject

For classes that inherit from NSObject

The element that initializes the element subclass of the parent class when space is opened for memory needs to be reinitialized initialized as follows

Initialize the part of the parent class first

self=[Super Init];

Then initialize your own part first to determine if self is nil nil stating that the parent class failed to initialize

-(ID) init//id is a type that points to any "object" or can be filled in instancetype he only uses it in the initialization.

{

if (Self=[super init]) {//super is part of the initialization parent class if the parent class fails to initialize then self is nil

{

m_m=10;

m_s=10;

m_h=20;

}

return self;

}

}

}

}

Initialization of the parent class and subclass of Init

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.