"C # sister and Objective-C aunt dialogue" (02) This is a Class-aunt's dog

Source: Internet
Author: User
C # sister: Aunt, I 've been walking my dog so early today ??

Aunt Objective-C: Yes, my dog is old, and I am a little insomnia. I started to get up early... you see, this dog is so intelligent and obedient.
// ********* File Dog. h **************** # import <Foundation/Foundation. h> typedef enum {kBlackColor, kYellowColor, kWhiteColor} Color;
@interface Dog : NSObject {    Color Color;    int Age;}-(void) Sound;-(void) Eat:(NSString  *) food  EatQty:(int) count;@end//Dog
-

/// ********** File Dog. m **************** # import "Dog. h "@ implementation Dog-(void) Sound {NSLog (@" Wang! ");}-(Void) Eat :( NSString *) food EatQty :( int) count {NSLog (@" Eat % d kg % @ ", count, food );} @ end // Dog
C # sister: Wow, twin dog ....
Aunt Objective-C: What a blind eye... a dog is divided into two files .. Do you know that the C language has header files? C # sister :... Take a closer look. There are methods and variables... This class also inherits from NSObject. Aunt Objective-C: You're right,  . Each class consists of two parts: interface (@ interface starts with @ end) and implementation (@ implementation

@ End at the beginning) is also called an interface, but it is different from your interface. Here, the interface tells the compiler that this class provides the data members (instance variables) and methods. Here, the concept similar to. NET's "interface" is "protocol". There are many similar things. Don't take the same keywords for granted .. C # sister:Well, I basically understand it, but I still have some questions.
-(Void) Eat :( NSString *) food EatQty :( int) count {NSLog (@ "Eat % d kg % @", count, food );}
If I write this code, it should look like this, the difference is pretty big.
Public void Eat (String food, int eatQty) {Console. WriteLine ("Eat {1:0} {0}", food, eatQty );}
Aunt Objective-C:Yes, the biggest difference should be the parameter of the method. My methods have their own characteristics, The method names and parameters are combined.For example, this operation is required to call my method.
Dog * d = [Dog new]; [d Sound]; [d Eat: @ "bone" EatQty: 3];
If it is C #, it should be called in this way.
Dog a = new Dog (); a. Sound (); a. Eat ("bone", 5 );
Have you seen the difference? If there are many parameters, it is easy to tell the meaning of each parameter through my code. C # sister:Well, it's really convenient (I thought: It's really cool, it's easy to use smart sensing ...), In addition, how can each statement be expanded with brackets? Aunt Objective-C:Yes, here I will notify the object to execute a method called "Send message". [d Sound] indicates to send a Sound message to the d object, which is actually to call the instance method. Do you know why my code file extension is m? Because it is short for message. C # sister:What is the meaning of "-" before the method? Can there be a "+" number? Aunt Objective-C:"+" And "-" are available, "-" Indicates the instance method, and "+" indicates the class method.That is, your static method. C # sister:Do you have no access modifier? Aunt Objective-C:I can use @ public, @ protected, and @ private as access modifiers to modify instance variables (member variables). The default value is @ protected. However, only member variables have access modifiers. class variables, class methods, and member methods do not have access modifiers. All methods are public and all class variables are private.
C # sister: That's clear. It's really Objective-C, old and dirty !!! Aunt Objective-C: C # sister:Come to the dog and eat something .....
[D Sound]; [d Eat: @ "bone" EatQty: 1];
Dog:
C # sister:And then feed the meat .....
[D Sound]; [d Eat: @ "meat" EatQty: 2];
Dog: C # sister:Hey hey, they all say that the dog can't eat shit again. Come on ........
[D Eat: @ "stool !!!!!! "EatQty: 999];
Dog:........... C # sister:Aunt Objective-C, your dog is really good... 88

-

Dear colleagues, it takes a short time to learn Objective-C. learning Objective-C is not for Mac or iPhone development, but not practical,
Actually, it is a C # Learning note for the user to learn Objective-C. The exact purpose of learning is to help me understand C #After all, it is impossible to know the characteristics of C # without comparison.
Please take a critical look at this.If it is found that there are conflicts with other articles, books, comments, and materials, please refer to other articles as far as possible. And leave me a message
I also invite all experts to make bricks actively. I just used it to build a house ~~~
C # dialogue between sister and Aunt Objective-C

(01) meet Objective-C: greetings from the first meeting
(02) This is an aunt's dog.
(03) NSString -- meet another dog
(04) Basis of garbage collection-about the demolition team

Pending renewal

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.