"Learn the path of iOS: Object-c" inheritance, convenience builder. NSString applications

Source: Internet
Author: User

1. Inheritance Definitions

Common Parent Class Usage scenario: when there are some of the same characteristics and behaviors between multiple classes, in order to prevent multiple classes from writing duplicate code, create a new public parent class that fills in the same characteristics and behaviors in multiple classes in the parent class to inherit from multiple classes.

2. Inheritance characteristics

*. Inherited content: Father all the strength variables and methods
*. Ownership of inherited content: inherited content is its own, but from the parent class,
*. Only single inheritance is allowed in OC, that is, a class can have only one parent class,
*. Inheritance has transitivity. A inherits from B,b inherits all the contents of B and C from C,a.
*. Inheritance does not have reciprocity; cannot inherit b,b a again.
*. The inherited content should be meaningful.
*. Inheritance belongs to one of the three main features of object-oriented.

3. Inheritance usage:

Note: Inherited becomes a parent class, or a superclass. (Super Class) (subclass) Subclass
Subclasses can directly use some of the characteristics and behaviors of the parent class,

[e-Mail protected] Application

1. In the. h file, the @class declaration needs to be in the. m file in the import


MAN.M implementation file

5. Traversing the constructor

Traversal constructor: A way to quickly create an object. It is essentially an initialization method that has been encapsulated for ease of external use.

Convenience Builder: + number method

Example: 1. Declaration

+ (ID) Studentwithscore: (NSString *) name               withage: (nsinteger) Age               withsex: (NSString *) Sex            withnumber: ( Nsinteger) Number             Withscore: (cgfloat) score;
2. Implement

<span style= "color: #000000;" >+ (ID) Studentwithscore: (NSString *) name               withage: (nsinteger) Age               withsex: (NSString *) Sex            withnumber :(nsinteger) Number             Withscore: (cgfloat) score {    Student *stu = [[Student alloc] Initwithname:name                                          withage: Age                                          withsex:sex                                       withnumber:anumber                                        Withscore:ascore];    return Stu;} </span>
3.creates an instance object by traversing the constructor.
    <span style= "color: #000000;" >student *stu = [Student studentwithscore:23.8                          withage:23 withsex:@ "man"                          withnumber:234234 withscore:34.5 ];</span>


"Learn the path of iOS: Object-c" inheritance, convenience builder. NSString applications

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.