The following code in order to fully learn to understand the rational design of class
1 /*2 Student3 member variables: Gender, birthday, weight, favorite color, dog (weight, hair color, eat, run)4 method: Eat, run, walk the dog, feed the dog5 6 */7 8 #import<Foundation/Foundation.h>9 Ten //Gender Enumeration Onetypedefenum{//enumeration type constants are typically preceded by an enumeration name (canonical) ASexman,//0 -Sexwoman//1 - } Sex; the - //Birthday Structural Body -typedefstruct { - intYear ; + intmonth; - intDay ; + } Date; A at //Color Structure Body -typedefenum { -Colorblack,//0 -Colorred,//1 -Colorgreen//2 - } Color; in - //Statement of the dog class to @interfaceDog:nsobject +{//member Variables - @public the DoubleWeight//Weight *Color Curcolor;//Hair Color $ }Panax Notoginseng //Method Declaration -- (void) eat; the- (void) run; + @end A the //the implementation of the Dog class + @implementationDog - //the way the dog eats . $- (void) Eat { $weight++; -NSLog (@"after eating this, the weight is%.2f.", weight); - } the //the way the dog runs -- (void) Run {Wuyiweight--; theNSLog (@"after running this time, the weight is%.2f.", weight); - } Wu @end - About //Declaration of the Student class $ @interfaceStudent:nsobject - { - @public - Char*name;//name ASex sex;//Sex +Date birthday;//Birthday the DoubleWeight//weight (kg) -Color Favcolor;//Favorite Color $Dog *dog;//the dog that was raised the } the //the way people eat things statement the- (void) eat; the //Statement of how people run -- (void) run; in //Information method declaration of the output person the- (void) print; the //man Walking Dog method statement About- (void) Liudog; the //man feeding Dog method statement the- (void) feed; the @end + - //implementation of the student class the @implementationStudentBayi the //the way people eat things the- (void) Eat { -weight++; -NSLog (@"the dog's weight after eating this time is%.2f.", weight); the } the the //How to achieve a person's running the- (void) Run { -weight--; theNSLog (@"the dog's weight after running this time is%.2f.", weight); the } the 94 //realization of the information method of the output person the- (void) Print { theNSLog (@"name =%s, gender =%d, birthday =%d-%d-%d, weight =%.2f, favorite color =%d", name, sex, Birthday.year, birthday.month, Birthday.day, weight, favcolor); the }98 About //The method of walking the dog is realized. -- (void) Liudog {101 //let the dog run up (call the dog's Run method)102 [dog run];103 }104 the //people feeding the dog method to achieve106- (void) Feed {107 //let the dog eat (call the dog's Eat method)108 [Dog eat];109 } the @end111 the intMain () {113 the //Create a Student object theStudent *s = [StudentNew]; the 117 //attribute assignment of a person118S->name ="Jack";119S->weight = -; -S->sex =Sexman;121 //birthday;122 /*123 s->birthday.year =;124 s->birthday.month = 9; the s->birthday.day = ten;126 */127Date d = { .,9,Ten}; -S->birthday =D;129 //the color you like theS->favcolor =colorred;131 the //man's method call133 [s eat];134 [s eat];135 [s run];136 [s run];137 [s print];138 139 //create an object for a dog $Dog *dog = [dogNew];141 142 //property Assignment of a dog143Dog->weight = -;144Dog->curcolor =Colorblack;145 146 //a dog attribute that assigns a dog object to a person147S->dog =Dog;148 149 //man's method call Max [s liudog];151 [s feed]; the 153 return 0;154}
The reasonable design of the 03 category of video iOS