OC class with Object instantiation, method call between Classes small topic

Source: Internet
Author: User

Two OC classes and object instantiation topics

Requirements:
* Class name, attribute name, attribute type, method name, method parameter, method return value self-fitting
* Write your own main function test design is reasonable

/*

1. Design a "dog" category
1> Property
* Color
* Speed (unit m/s)
* Gender
* Weight (unit is kg)

2> behavior
* Eat: Every time you eat, weight gain of 0.5kg, output after eating the weight
* Bark (Bark): Outputs all properties
* Run: Every run, weight loss of 0.5kg, output speed and after running the weight
* Compare color: compare color with other dog, if same, return yes, different, return no
* Compare Speed: Compare speed with other dog, return speed difference (own speed-other dog's speed)
*/

/*
#import <Foundation/Foundation.h>
typedef enum {Yellow,black,red,green}dcolor;
typedef enum {male, female}dsex;
@interface Dog:nsobject
{
@public
Dcolor _color;
int _speed;
Dsex _sex;
float _weight;
}

-(void) Eat: (float) weight;
-(void) Wangwang;
-(void) Run: (float) weight;
-(BOOL) Abccolor: (Dcolor) Color1 Andcolor: (dcolor) Color2;
-(int) abcspeed: (int) speed1 andspeed: (int) speed2;

@end

@implementation Dog

-(void) Eat: (float) weight
{
weight+=0.5;
NSLog (@ "Weight changes:%.2f", weight);
}

-(void) Wangwang
{
NSLog (@ "color:%d,speed:%d,sex:%d,weight:%.2f", _color,_speed,_sex,_weight);
}

-(void) Run: (float) weight
{
weight=weight-0.5;
NSLog (@ "Weight changes:%.2f", weight);
}

-(BOOL) Abccolor: (Dcolor) Color1 Andcolor: (dcolor) Color2
{
if (COLOR1==COLOR2)
return YES;
Else
return NO;
}

-(int) abcspeed: (int) speed1 andspeed: (int) speed2
{
int speed12=speed1-speed2;
return speed12;
}

@end

int main ()
{
@autoreleasepool {
Dog *dog1=[dog New];
dog1->_color=red;
dog1->_sex= public;
dog1->_speed=50;
dog1->_weight=8.5;

Dog *dog2=[dog New];
dog2->_color=yellow;
dog2->_sex= mother;
dog2->_speed=30;
dog2->_weight=6.5;

[Dog1 eat:dog1->_weight];
[Dog1 Wangwang];
[Dog1 run:dog1->_weight];
BOOL abc1=[dog1 Abccolor:dog1->_color andcolor:dog2->_color];
int Abc2=[dog1 Abcspeed:dog1->_speed andspeed:dog2->_speed];
NSLog (@ "The same color?) Answer:%d ", ABC1);
NSLog (@ "Speed difference is:%d", ABC2);
}
return 0;
}
*/

/*
2. Combining the "dog" category above, design a "human" category
1> Property
* Name
* Dog (keep a dog)

2> behavior
* Feed the dog: every time you feed, the dog will perform the "eat" behavior
* Walk the dog: Every time, the dog will perform the "Run" behavior
*/


#import <Foundation/Foundation.h>
typedef enum {Yellow,black,red,green}dcolor;
typedef enum {male, female}dsex;

Dog class
@interface Dog:nsobject
{
@public
Dcolor _color;
int _speed;
Dsex _sex;
float _weight;
}
class method (behavior) Declaration of Dog
-(void) Eat: (float) weight;
-(void) Wangwang;
-(void) Run: (float) weight;
-(BOOL) Abccolor: (Dcolor) Color1 Andcolor: (dcolor) Color2;
-(int) abcspeed: (int) speed1 andspeed: (int) speed2;

@end

Person class
@interface Person:nsobject
{
@public
NSString *_name;
Dog *_dog; This attribute is defined as the dog class, which is an instance object of the dog class, so _dog can call methods in the Dog class
}
class method (behavior) Declaration for person
-(void) Weigou;
-(void) Liugou;

@end

Dog class method definition
@implementation Dog

-(void) Eat: (float) weight
{
weight+=0.5;
NSLog (@ "Weight changes:%.2f", weight);
}

-(void) Run: (float) weight
{
weight=weight-0.5;
NSLog (@ "Weight changes:%.2f", weight);
}

@end

Person class method definition
@implementation person

-(void) weigou{
[_dog eat:_dog->_weight]; In the Weigou and Liugou methods inside the person class, call the Eat and run methods inside the dog class, where the property of the calling method is exactly an object of the dog class.
}

-(void) liugou{
[_dog run:_dog->_weight]; Ditto
}

@end

int main ()
{
@autoreleasepool {
Person *man1=[person new]; Create an instance object of the person class
Dog *xiaobai=[dog New]; Create an object of the dog class, the instance object of the dog class is just one property of the person class instance object we created
xiaobai->_weight=10; We just need to xiaobai the _weight property, so just initialize this property.
man1->_dog=xiaobai;
[Man1 Weigou];
[Man1 Liugou];
}
return 0;
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

OC class with Object instantiation, method call between Classes small topic

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.