First lecture, Class and Object

Source: Internet
Author: User

First, OC overview

Code format

int main (int argc, const char * argv[]) {
@autoreleasepool {


Code Area


}
return 0;
}

1, OC support basic data type int, float

2. Use string Object @ "Hello" in the string "Hello" oc in C language

3, NSString is provided by the System class Library--String class
The variable that defines the object type must be added * NSString * name = @ "Zhangyixing";

4. New Type class type (object type) in OC

5, printf (C) NSLog (OC) Output string content NSLog (@ "zhangyixing");

6, output the value of the basic data type
NSLog (@ "number =%d, score=%.2f, age =%ld", number, score, age);

7. Output Object
NSLog (@ "name =%@", name); NSLog (@ "name =%@", @ "zhangyixing");


Second, object-oriented programming

Object-oriented programming oop (Object Oriented programming)

Object-oriented language: c++,java,c#

Concepts: objects, classes, encapsulation, inheritance, polymorphism, etc.


III. Classes and objects

Define the class first, then create the object, and use the object.

A class is a template, and an object is a concrete representation of any object that consumes memory space.

1, the definition of the class----interface part, implementation part

New file OS X-------Cocoa CALSS

Set Name:person (name file with class name), subclass Of:nsobject, Language:oc

1) interface part----The characteristics and behavior of the external declaration----------Person.h File


#import <Foundation/Foundation.h>

Start of the interface

@interface Person:nsobject (class name: Parent type)

{

Declaring the characteristics of a class

Instance variables: Stores the characteristics of an object note: The first letter is _

NSString * _NAME;

int _age;

}

Declaring the behavior of a class

Method: Similar to the function in C, the concept of no function in OC

(1) + indicates that this method belongs to a class, only class execution [class name Method name]

(2)-Indicates that this method belongs to an object and only the object executes [object method name]

-(void) sayhi;

End of interface

@end


2) Implement partial----internal implementation----------PERSON.M file


#import "Person.h"
The beginning of the implementation section
@implementation person
-(void) sayhi//minus spaces
{
NSLog (@ "Hello");
}

End of implementation
@end


2. Create object: Allocate memory space, initialize, heap area-------main.m file

The main function imports the file----#import "Person.h"

1) allocating memory space---[Class name alloc]
Using the [] Call method in OC Alloc is a method that the system-supplied class can invoke
(1) Allocating memory space in the heap area;

(2) The data of the memory space is cleared automatically, and the default value of all instance variables is set to 0;

(3) Return the first address of the allocated storage space

Person * p1 = [Person alloc];
The pointer P1 points to the person object stored in the heap area, so the P1 is usually referred to as the person object

2) Initialize object------[Object init]

Initialize: Sets an initial value for an instance variable of an object

Init initialization is a system-provided method of object initialization that can only be called by an object

P1 = [P1 init];
Person * p2 =[[person alloc] init];
P1 p2 are of the same type, two different objects

Person * P3 = nil; P3 points to NULL, is a null pointer and cannot be treated as an object

In OC, any operation on nil is invalid.

P3 = p1; P3, P1 point to the same object

3, through the object invocation method, realizes the function

Using the [] Call method in OC Alloc is a method that the system-supplied class can invoke

[P1 Sayhi]; Print----Hello


This article is from the OC Learning Notes blog, so be sure to keep this source http://10613955.blog.51cto.com/10603955/1684156

First lecture, Class and Object

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.