oc-explore whether the properties of private adornments can be inherited

Source: Internet
Author: User

A purely personal point of view, if there is a similar, purely coincidental!

Let's start by reviewing some of the common sense of inheritance in the OC we are familiar with.

Subclasses can inherit from the parent class's: Public/protect decorated member variable ("Property" or "field"). The private modifier cannot be inherited

~ ~ ~ After finishing the review, then the problem came

Can the OC Neutron class inherit the property of the parent class that is private decorated?

Individuals tend to be lazy when writing programs, such as:

The parent fun has a property: name, which is often written for Jingxiang when declared:


@property (nonatomic,copy) nsstirng *name;


After the subclass inherits the parent class, the subclass also has the name attribute. That's how it used to be used.


Self.name = @ "Zhang San";

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


But in the past I have never thought about what the name is, and then when it comes to this kind of problem I guess it makes protect, but the protect-modified property

This can also be used in subclasses:


_name = "Zhang San";

NSLog ("%@", _name);


and property is not allowed, online said this is the default, the default is private, called the Beast said: Private is not able to be inherited-----you are not kidding me, I followed the beast so used to be used to

Now only to find that the beast himself dug a pit himself and jumped inside, and incredibly did not realize that he fell into the hole, but also did not realize that the pit or dug himself.

So is the property statement private? Private is not able to inherit the quilt class?

I first tested the inheritance of private attributes: The process is described below

Define a parent class fun, subclass Funa, Funb

Fun's private properties:

@private NSString *_name;


Public functions:

The setter function for name

-(void) Setnamewith: (NSString *) newname{

_name = NewName;

}


Getter function for Name

-(NSString *) getname{

return _name;

}


Print function: Nsstringfromclass ([Self class]) is the name of the class that is currently calling this function

-(void) print{

NSLog ("%@--->%@", nsstringfromclass ([Self class]), _name);

}

The properties and functions of the parent class come to an end, and then there is the argument

The argument is based on:

Private field assignment for parent class: name = @ "Emperor", if the parent class's private property cannot be inherited, then Funb calls the print function

The result should be "the Emperor",

Funa calls the setter function of name and passes the value: "Prince a", and then Funb calls the print function, the result should be: "Prince a",

If the above is established, the private attribute cannot be inherited by the conclusion of the establishment


Demonstration process:

Create three objects, respectively:


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

Funa *funa = [[Fun alloc] init];

Funb *funb = [[Fun alloc] init];


[Fun setname:@ "The Emperor"];

[Fun print];

[Funa print];

[Funb print];


Printing results:

2014-12-28 16:53:47.993 Test---property[1669:60b] fun---> Your Majesty

2014-12-28 16:53:47.993 Test---property[1669:60b] funa---> (null)

2014-12-28 16:53:47.993 Test---property[1669:60b] funb----> (NULL)


Continuation of the above code continues to knock:

[Funa setname:@ "Prince a"];

[fun b setname:@ "Prince b"];

[Fun print];

[Funa print];

[Funb print];


Printing results:

2014-12-28 16:53:47.993 Test---property[1669:60b] fun---> Your Majesty

2014-12-28 16:53:47.993 Test---property[1669:60b] funa---> Prince a

2014-12-28 16:53:47.993 Test---property[1669:60b] funb----> Prince b

This result should indicate that the property of the private adornment can be inherited by the quilt class. Therefore, it is common to use only one @property to declare the creation of a property,

The property is private, the subclass can inherit the property, but it needs to be provided by the parent class.


Subclasses can inherit the private properties of the parent class, but if the parent class does not provide a function for the subclass to manipulate the private property, then the tangled inheritance is meaningless.


oc-explore whether the properties of private adornments can be inherited

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.