The second OC class and the second oc class

Source: Internet
Author: User

The second OC class and the second oc class

Example:

Creates a class, creates an object, calls a method, and changes the attribute value.

 

// Declare the class

@ Interface Person: NSObject

{

@ Public // make it possible to call member variables outside the class

Int _ weight; // weight

}

-(Void) run;

-(Void) rat :( char *) food;

@ End

 

// Class implementation

@ Implementation Person

-(Void) run {

_ Weight-= 2;

NSLog (@ "the weight after the step is % I", _ weight );

}

-(Void) eat :( char *) food {

_ Weight + = 10;

NSLog (@ "the weight after % s is % I", food, _ weight );

}

@ End

 

# Import <Foundation/Foundation. h>

Int main (){

Person * person1 = [Person new]

Person1-> _ weight = 100;

NSLog (@ "weight before meals: % I", _ weight );

[Person1 run];

 

NSLog (@ "weight before meals: % I", person1-> _ weight );

[Person1 eat: "steamed bread"];

Return 0;

}


How does oc determine whether an object is an instance of a subclass of a class?

If ([childObject. Parent class] isKindOfClass: [Parent class]
{
}
 
In OC, how does one perform subclass calling of superclass in the maimm file?

There is no way to do this in OC. If you want B to call the method of parent class. As you said, it can only be called in the print method of B.
[Super print] That is to say, B should complete its proper encapsulation. Other classes (such as main) should not care about what the print method of B has done.


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.