IOS--DAY04---Classes with objects, inheritance, instances, attributes, point languages

Source: Internet
Author: User

Creating a class requires two parts: interfaces and implementations

-----

The. h header file corresponds to the interface

@interface Aa:nsobject//base class

{

Member Variable declaration

Inside the curly brace, add the current class property

String type

NSString *name; Name NSString is a class

Integral type

Nsinteger age; Age is a numeric type not a class

NSString *gender;

}

Method declaration

@end

------

The. m file is equivalent to the implementation class

#import "Aa.h"

@implementation AA

{

Method implementation

}

@end

-------

The. mm implementation file comes out of the OC Code and C code, and can also contain C + + code, which is used only if the C + + class or feature in the OC code is actually referenced

So the creation of a class three keywords is @interface @implementation @end

----------------------------------------------------------------

Setup and accessors (setter getter)

declarations in. h files

-----

Set setter

-(void) SetName: (NSString *) _name;

-(void) Setage: (Nsinteger) _age;

-(void) Setgender: (NSString *) _gender;

Accessor getter

-(NSString *) getName;

-(Nsinteger) getage;

-(NSString *) Getgender;

----

Implemented in. m files

Set setter

-(void) SetName: (NSString *) _name{

name = _name;

}

-(void) Setage: (Nsinteger) _age{

age = _age;

}

-(void) Setgender: (NSString *) _gender{

gender = _gender;

}

Accessor getter

-(NSString *) getname{

return name;

}

-(Nsinteger) getage{

return age;

}

-(NSString *) getgender{

return gender;

}

------------

Inheritance

---differs from Java OC only allowing each class to have only one parent class

@interface childclass:superclass{}

You can override the parent class method

Inheritance has transitivity

-----

Method

There are two main types of methods in OC: Class (+) method and instance (-) method

Class methods must be called by the class name to be equivalent to the static method in Java

Instance methods must be invoked with an instance object to

---------

Instantiation methods in OC: Custom initialization methods and convenience constructors

Custom initialization methods

Create objects in two steps: Allocate space and initialize

The function of an initialization method is to assign an initial value to an object, usually starting with Init

A class can contain more than one initialization method, but there is only one initialization method, regardless of which initialization method is invoked, the initialization method is executed, and the initialization method of the specified initialization method and the parent class have a certain connection

-------

Convenience Builder

is a way to quickly create an object. Essentially, the initialization method is encapsulated

is a class (+) method

--------

Self and Super OC keywords

Super: Sending a message to a parent class object

Self: Send a message to this object

Self always represents an object that accepts messages

-----------

Property------is a set of settings and accessors that need to be declared and implemented

and instance variables different properties are a set of methods, the instance variable is a variable

. h in

@property int age;

Quite a statement of Setter,getter

. m medium

@synthesize Age=_age; _age The default instance variable added as the current class

Equivalent to achieving the Setter,getter

Attributes of a property

Mainly includes three aspects:

Read/write Features

ReadWrite read/write (both set and accessor)

ReadOnly Read only (accessor only)

Getter = Method name Specifies the accessor's method name

Setter = Method Name Specifies the method name of the setting

Atomic Properties

Nonatomic non-atomicity, no guarantee of multithreading security

Atomic atomicity, multi-threaded access is more secure

Semantic characteristics

Assign direct assignment for basic data types

Retain when assigning memory optimizations for object types

Copy copies a copy for a special object type (an object that implements the Nscopying protocol) such as a string

---

Dot syntax

The point syntax of OC can be used once the setup and accessor of the instance variable are available

P.age = 20; Equivalent to [P setage:20];

int age = P.age; equivalent to int age = [P age];

Point syntax and [receiver message] equivalent

IOS--DAY04---Classes with objects, inheritance, instances, attributes, point languages

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.