Objective-c object-oriented, does the class have a real private method and a private property?

Source: Internet
Author: User

In object-oriented languages such as java/c#, access to a method can be controlled through public/private/protected to control its access rights. In OC, however, the method does not have access modifiers. So do we have a way to make it private?

1. If a method is not declared in the header file, then this method sends a message to the object in the form of [receiver MethodName] at compile time, and the compiler will have a warning telling you that the method could not be found, but the actual operation will still work.

Cause: At compile time, even if this method is not declared in the header file, the compiler will still compile the signature of the method into the list of methods of the class, and when the message is sent, the message list will be automatically found, and will be triggered if a message of the same name is located)

2. If a method is defined in an anonymous taxonomy, then at compile time, the compiler sends a message to the object via [receiver MethodName], and the compiler will still have a warning telling you that the method is not found, but it still works when it actually runs

Reason: Ibid.

Conclusion: In OC, there is actually no private method in real sense.

Do you have a real private property?

1. If a property is defined in the anonymous classification of a method, the compiler will prompt for an error at compile time by Obj.prop to the property, and the compilation does not pass. In this way, it seems that you can define a private property.

But....

We know that all the method calls in OC are passed through the message, and even if you use the dot syntax of Obj.prop to assign a value to an object property, the compiled code is still a call to convert to a message.

Obj.prop actually sends an SetProp method to the object, which is equivalent to [obj SetProp].

According to the above explanation, if you use [obj SetProp] to assign a value to this so-called "private property", you can still assign a value that is successful and works as expected.

In fact, there is no real private method and private property in OC. But in actual use, we should abide by the rules, cannot call the method, do not call.

Objective-c object-oriented, does the class have a real private method and a private property?

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.