- subclass and superclass, overriding concept
- Interface and Implementation
- NSObject class
- Accessibility within a class
- The usage on @class in header file
- How to define constant
- object alloc
<Basic Concept of SubClass and SuperClass>
Objective-C likes many other object-oriented languages, thus it also has the concept of subclass and superclass.
A class may have many subclasses, but a class can have only one immediate superclass.
For example, a class Animals can have a subclass Dog and another subclass Cat while the class Dog can only have the class Animals as its superclass.
The reason for the class–subclass relationship is to allow related classes to share functionality.
Suppose, for example, we have a Dog class and a Cat class, and we are considering defining a method walk for both of them. So we can define the method walk as a method of the class Animals.
A subclass can redefine a method inherited from its superclass.
For example, perhaps some dogs bark differently from other dogs. We might have a class NoisyDog, for instance, that is a subclass of class Dog.
Dog defines a method bark, but NoisyDog also defines a method bark with different meaning. This is called overriding - if a subclass overrides a method inherited from its superclass, then when the corresponding message is sent to an instance of that subclass, it is the subclass version of that method that is called.
<The usage on @class in header file>
But how come if MyClass has a public method that takes or returns an instance of MyOtherClass when MyClass.h does not import MyOtherClass.h? Since MyClass.h doesn't know about MyOtherClass, and the compiler will complain.
The solution for such case is to use @class MyOtherClass in MyClass.h:
#import <UIKit/UIKit.h> @class MyOtherClass; |
@class directive simply tells the compiler, "Don't worry, MyOtherClass really is the name of a class." That's all the compiler needs to know in order to permit the mention of the type MyOtherClass* in the header file.
<Code Example for Inheritance>
To create a subclass of BankAccount that we call SavingsAccount:
Now, we add some instance variables and methods that provide the new functionality rather than those from its parent class bankAccount:
interface file (i.e. savingsAccount.h)
Notes:
- 1 new instance variable named "interestRate.
- 2 new instance methods named "calcaulateInterest" and "setInterestRate:"
- 1 overriding instance method "displayInfo"
implementation file (i.e. savingsAccount.m)
main.h
<don't know why fail ....>
<The Global Namespace>
Objective-C has no namespaces.
In order to prevent collision with Cocoa framework which usually with prefixes like NSArray, CGFloat and CGRect, and so on.
<Class Methods>
You don’t have to do anything to create a class object. One class object for every class
your program defines is created for you automatically as the program starts up. (This
includes the classes your program imports, so there’s a MyClass class object because you
defined MyClass, and there’s an NSString class object because you imported UIKit.h
and the whole Cocoa framework.) It is to this class object that you’re referring when
you send a message to the name of the class. [Page 83]
<alloc and init>
The alloc class method is implemented by the NSObject class, the root class from which all other classes inherit. It causes memory to be set aside for the instance so that an instance pointer can point to it. You must never, never, never call alloc by itself. You must immediately call another method, an instance method that initializes the newly created instance, placing it into a known valid state so that it can be sent other messages. Such a method is called an initializer.
Moreover, an initializer returns an instance — usually the same instance, initialized. Therefore you can, and always should, call alloc and the initializer in the same line of code. The minimal initializer is init.
The basic pattern for instantiation from scratch
SomeClass* aVariable = [[SomeClass alloc] init];
or
SomeClass* aVariable = [SomeClass new];
The convention is that all initializers, and only initializers, begin with the word init. The ultimate bare-bones initializer is called simply init, and takes no parameters. Other initializers do take parameters, and usually begin with the phrase initWith followed by descriptions of their parameters.
In looking through the documentation for an initializer, don’t forget to look upward through the class hierarchy. For example, the class documentation for UIWebView lists no initializers, but UIWebView inherits from UIView, and in UIView’s class documentation you’ll discover initWithFrame:. Moreover, the init method is defined as an instance method of the NSObject class, so every class inherits it and every newly minted instance can be sent the init message. Thus it is a given that if a class defines no initializers of its own, you can initialize an instance of it with init.
<check UIWebView and UIView in details>
<Other initialization will be done automatically through the Storyboard>
Most Xcode projects will include at least one nib file (or storyboard file), which will be built into the app bundle, and will then be loaded as the app runs. A nib file consists, in a sense, of the names of classes along with instructions for instantiating and initializing them. When the app runs and a nib file is loaded, those instructions are carried out — those classes are instantiated and initialised.
<Reference>
http://books.google.com.hk/books?id=KOoS3nIxU-AC&pg=PT164&lpg=PT164&dq=objective+c+superclass+subclass+example&source=bl&ots=P7QgfRpRii&sig=nDEGoQr_6_9Miqdu8yMIsFnqMik&hl=en&sa=X&ei=4YFuUo2BDcWDlAXRxYGACQ&ved=0CCoQ6AEwADgK#v=onepage&q=objective%20c%20superclass%20subclass%20example&f=false
http://www.apeth.com/iOSBook/ch04.html
<Exercises>
1. Write a new class Person and define its variables and methods.
2. Write another new class Employee and define its variables and methods which is inherited from class Person.
No comments:
Post a Comment