The difference between @interface and Java in OC and the @implementation @protocol

Source: Internet
Author: User

Java in Java interface is ' interface ' meaning, and Java class declaration with class, that is, interface with interface declaration, class is declared with class, is two separate parts.

Only when a class declares to implement an interface do they establish a relationship, for example:

[HTML]View Plaincopyprint?
    1. Interface ai{
    2. void print ();
    3. };
    4. Class ac{
    5. };

At this time, the AI and AC are independent, AC does not have to build a relationship with the AI to compile the error, the following modifications to the AC, the AI to establish a relationship with AC, AC must implement the method declared in the AI to compile.

[HTML]View Plaincopyprint?
    1. Class AC Implement ai{
    2. void print () {
    3. System.out.println (' Hello world ');
    4. }
    5. };

objective-cOn the internet everyone will be in OC interface understood as "informal agreement (or interface)", Prototal understood as "formal agreement (or interface)", I think it is not difficult to understand, but very twisted, so I do a bit of their own understanding:
1, protocal is equivalent to Java in the interface;
2, while interface and implementation together represent a class, the combination of the two is equivalent to the class in Java, that is, the class in OC must consist of two parts, interface part and implementation part, This is the complete declaration of a class in OC, and then the declaration portion of the member variable and the member method is placed in the interface section of the OC, including the inheritance relationship, protocal implementation of the relationship, all in the interface inside the head to declare, Then put the implementation part in the implementation section, equivalent to split the class into declarations and implementation of two parts, which is indispensable, so in OC, may not be called the interface interface, directly called the class declaration part comes easy to understand more, in short, Interface in OC is a part of the class, and implementation together to form a complete class. In addition to the OC we can define in the @interface can also be defined in the property variables, what is the difference between the two?
    1. If you define variables only in @nterface, the variables you define can only be accessed in the current class, not in other classes, and variables declared with @property may be accessed externally.
    2. Using @property to declare variables, you can use the "self. Variable name" method to read and write variables. And the way to use @interface is not possible.
    3. Here is a link: http://stackoverflow.com/questions/9702258/ Difference-between-properties-and-variables-in-ios-header-file want to see it more clearly.

The difference between @interface and Java in OC and the @implementation @protocol

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.