oc-exercises for objects and methods

Source: Internet
Author: User

Explanation of Exercises
Thinking & Implementation 1:
Design a "dog" class
1) Properties: color, speed, gender, weight
2) behavior
Eat: Every time you eat, weight gain 0.5kg, output after eating the weight
Bark: Output All properties
Run: Every run, weight loss 0.5kg, output speed and after running the weight
Compare color: Compare color with other dog, if same, two value do subtraction get 0, return No (0 value), not same, do
Subtract not 0 value, return Yes (1)
Compare speed: Compare speed with other dog, return speed difference (own speed-other dog's speed)


#import <Foundation/Foundation.h>
Enumeration that defines the color of a dog
typedef enum{kcolorwhite,kcoloryellow,kcolorhua,kcolorblack}color;
typedef enum{ksexgong,ksexmu,ksexyao}sex;


Definition of #pragma mark Dog class
@interface Dog:nsobject
{
The dog's instance variable
Color _color; The color of the dog nsstring *_color;
float _weight; The weight of the dog
Sex _sex; Gender
int _speed; Speed
}
The behavior of the dog
-(void) Eat: (NSString *) foodname; Weight +0.5
-(void) run; Weight-0.5
-(void) bark; Print all the information
Compare the color of dog hair
-(void) Iscomparecolorwithother: (dog *) Dog;
Compare speed
-(void) Comparespeedwithother: (dog *) Dog;
@end

#pragma the implementation of Mark's dog class
@implementation DOg
-(void) Eat: (NSString *) Foodname {
_weight + = 0.5f;
NSLog (@ "The dog is eating%@, after eating also played a hiccup!", Foodname);
}
-(void) Run {
_weight-= 0.5f;
NSLog (@ "Dog is running mad at speed%d!", _speed);
}
-(void) Bark {
NSLog (@ "Sex:%d, color:%d, speed:%d, current weight:%d", _sex,_color,_speed,_weight);
}
-(void) Iscomparecolorwithother: (dog *) Dog {
if (_color = = Dog->_color)
return YES;
else return NO;
}
-(void) Comparespeedwithother: (dog *) Dog {
Return _speed-dog->_speed;
}

@end


int main () {
@autoreleasepool {
Create two Dogs
Dog *D1 = [dog new];
D1->_speed = 80;
D1->_weight = 20;
D1->_sex = Ksexyao;
D1->_color = Kcolorhua;
[D1 bark];
[D1 eat:@ "a piece of corn stained with baba"];
[D1 eat:@ "a piece of sweet potatoes stained with baba"];
[D1 bark];

Dog *D2 = [dog new];
D2->_speed = 30;
D2->_weight = 50;
D2->_sex = Ksexgong;
D2->_color = Kcolorblack;
[D2 bark];
[D2 eat:@ "a piece of corn stained with baba"];
[D2 eat:@ "a piece of sweet potatoes stained with baba"];
[D2 bark];
1 Same 0 different
Bool Issamecolor = [D1 iscomparecolorwithother:d2];
NSLog (@ "issamecolor=%d", Issamecolor);
Compare the speed difference of a dog
int cha = [D1 comparespeedwithother:d2];
NSLog (@ "Speed difference:%d", cha);
}
return 0;
}

oc-exercises for objects and methods

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.