OC-exploring whether private-modified attributes can be inherited

Source: Internet
Author: User

OC-exploring whether private-modified attributes can be inherited

This is purely a personal opinion. If there are similarities, it is a coincidence!

First, let's take a look at some common knowledge about inheritance in the well-known OC.

Subclass can inherit the member variables ("attribute" or "field") modified by public/protect of the parent class "). Private modification cannot be inherited

~~~~ After the review, the question is ·········

Can subclass in OC inherit the attributes modified by the parent class by private?

Individuals often get lazy when writing programs, such:

The parent Fun class has a property: name, which is often written in this way to save effort:


@ Property (nonatomic, copy) NSStirng * name;


After the child class inherits the parent class, the Child class also has the name attribute. That's how it works.


Self. name = @ "James ";

NSLog ("% @", self. name );


However, I have never considered the attribute of this name before. When I encountered this kind of problem, I guess it made protect, but the Attribute Modified by protect

This can also be used in subclass:


_ Name = "James ";

NSLog ("% @", _ name );


However, property cannot be used. It is set to the default value on the Internet. The default value is private. It is called the beast. The private value cannot be inherited. Are you kidding me, i'm used to using it like this.

Now I found that the beast had dug a hole and jumped into it. I didn't even realize that I had fallen into the trap, even though I didn't realize that I had dug it myself.

Is the property declared private? Can the private category inherit from the quilt class?

First, I tested the inheritance of private attributes: the process is as follows:

Define a parent class Fun, sub-class FunA, FunB

Private attributes of Fun:

@ Private NSString * _ name;


Common functions:

// Setter function of name

-(Void) setNameWith :( NSString *) newName {

_ Name = newName;

}


// Name's getter Function

-(NSString *) getName {

Return _ name;

}


// Print the function: NSStringFromClass ([self class]) is the name of the class currently calling this function

-(Void) print {

NSLog ("% @ ---> % @", NSStringFromClass ([self class]), _ name );

}

The attributes and functions of the parent class are now finished, and then the arguments are demonstrated.

The rationale is:

The value of the private field of the parent class is: name = @ "". If the private attribute of the parent class cannot be inherited, FunB calls the print function.

The result should be "Emperor ",

FunA calls the setter function of name and transmits the value "Prince A". Then FunB calls the print function and the result is: "prince ",

If the above is true, the conclusion that the private property cannot be inherited is true.


Demonstration process:

Create three objects:


Fun * fun = [[Fun alloc] init];

FunA * funA = [[Fun alloc] init];

FunB * funB = [[Fun alloc] init];


[Fun setName: @ ""];

[Fun print];

[FunA print];

[FunB print];


Print result:

16:53:47. 993 test --- property [1669: 60b] Fun --->Emperor

16:53:47. 993 test --- property [1669: 60b] FunA ---> (null)

16:53:47. 993 test --- property [1669: 60b] FunB ---->(Null)


Continue with the above Code:

[FunA setName: @ "A"];

[FunB setName: @ "B"];

[Fun print];

[FunA print];

[FunB print];


Print result:

16:53:47. 993 test --- property [1669: 60b] Fun ---> Huang Shang

16:53:47. 993 test --- property [1669: 60b] FunA ---> Prince

16:53:47. 993 test --- property [1669: 60b] FunB ----> Prince B

This result indicates that the attributes modified by private attributes can be inherited by the quilt class. Therefore, only one @ property is usually used to declare the creation of an attribute,

The property of this attribute is private, and the subclass can inherit and use this property, but it only needs the function provided by the parent class.


Subclass can inherit the private attributes of the parent class. However, if the parent class does not provide a function for the subclass to operate on this private attribute, it makes no sense to tangle inheritance.


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.