Objective-c inheritance and combination of classes in base 1:oc

Source: Internet
Author: User
Tags uppercase letter

1. Definition and declaration of classes

The class declaration in OC ends with @interface beginning @end.

The class definition in OC ends with @implementation beginning @end.

Method declarations in OC:

-(void) SetName: (nsstring*) strName; Front Dash-Indicates that this is a method, (void) represents the return value, SetName represents the method name, (nsstring*) strName indicates that the parameter is a nsstring* type, Name is strname, note: () be sure to add

The member variable in OC is defined in the {} of the class declaration, which is not the same as the method declaration, the method is outside the {}

Object creation method in OC: animal* Animal = [Animal new]; Indicates that the animal class calls the new method to allocate a piece of memory to create the object.

Method invocation in OC: [Animal SetName: @ "Akon"]; indicates that the calling object animal method SetName, the passed-in parameter is Akon.

It is important to note that: Method invocations in OC are enclosed in brackets [].

programming Habits: The class name begins with an uppercase letter, and the method name and variable name begin with a lowercase letter.

Example:

Statement @interface animal:nsobject{@privateNSString * strName;} -(void) SetName: (nsstring*) strname;-(nsstring*) getName; @end//Definition @implementation animal-(void) SetName: (nsstring*) strnamein{    strName = Strnamein;} -(nsstring*) getname{    return strName;} @endint Main (int argc, const char * argv[]) {        animal* Animal = [Animal new];    [Animal SetName: @ "Akon"];    nsstring* strName = [Animal getName];    NSLog (@ "name:%@", strName);    return 0;}

2. Inheritance of Classes

In fact, the animal in the example above demonstrates that the class inherits from the animal class nsobject, in order to deepen the impression, to create two classes of cat and dog inherit from the parent class animal, the code is as follows:

@interface animal:nsobject{@privateNSString * strName;} -(void) SetName: (nsstring*) strname;-(nsstring*) getname;-(nsstring*) Mancallme; @end @interface Cat:animal@end@inter Face Dog:animal@end#endif

The above animal class defines a method Mancallme, which is intended to implement a virtual function like C + +, which is dynamically bound by virtual functions.

So how does oc implement dynamic binding? as long as the subclass overrides the method of the parent class. Code See below:

  animal.m//  testcons////  Created by Mac on 15/1/17.//  Copyright (c) 2015 Akon. All rights reserved.//#import <Foundation/Foundation.h> #import "Animal.h" @implementation animal-(void) setName : (nsstring*) strnamein{    strName = Strnamein;} -(nsstring*) getname{    return strName;} -(nsstring*) mancallme{    return @ "Animal";} @end @implementation Cat-(nsstring*) mancallme{    return @ "Cat";} @end @implementation Dog-(nsstring*) mancallme{    return @ "dog";} @end

When creating the object, save the object pointer with the ID type as follows:

int main (int argc, const char * argv[]) {        ID animal[3];    ANIMAL[0] = [Animal new];    [Animal[0] SetName: @ "a"];    ANIMAL[1] = [Cat new];    [Animal[1] SetName: @ "B"];    ANIMAL[2] = [Dog new];    [animal[2] SetName: @ "C"];    for (int i = 0; i < 3; ++i) {        NSLog (@ "getname:%@, mancallme:%@", [Animal[i] getName], [Animal[i] Mancallme]);    }    return 0;}

sometimes subclasses need to call the method of the parent class , how to do it, similar to the C++__super keyword, there are super keywords in OC, such as calling animal's Mancallme method in cat, so to do:

[Super Mancallme]

3. Combinations of classes

Inheritance and composition are the two methods of reusing classes, so when do you use inheritance to combine them? Inheritance emphasizes "is a kind of" relationship, such as Cat is a kind of animal, enough is a kind of animal. Then cats and dogs should inherit from animals. The combination emphasizes the relationship of ' having ', such as the animal has legs and eyes, and that kind of animal should combine legs and eyes.

The implementation of the combination is simple, simply delegating the method invocation to the grouped class. For example, the animal class has a method of "discharge", then the implementation of this method can be called the eyes of the discharge method to achieve. Because it is very simple, the code is not up.

Objective-c inheritance and combination of classes in base 1:oc

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.