OBJECTIVE-C Classes and objects

Source: Internet
Author: User

Simple use of 0.NSString

1. Class encapsulation, data protection;

2.setter and Getter methods;

3.self,supper,id

4. Initialization method;

5. Class methods

=========================================================

0, NSString Simple use

NSString is the OC string class;

@“....” A string object;

NSString *s = @ "I am a string";

1. Permissions for instance variables in classes

Requirements: Access member variables;

1) Encapsulation of classes, protection of data

2) Three permissions and default permissions

@public, @protected, @private

Default in OC is @protected

"Variable Permissions"

Dog:name,age,weight

Special methods:

"Set method and get Method"

Set method Function: Modify/set member variables;

Get method function: Gets the member variable value;

Named:

Set method:-(void) Setxxxx: (parameter type) parameter name;

Get method:-(return value type) getxxxx;

"Test1"

Practice:

Human: Name NSString *,age, school number;

1.set method Setting member variables

The 2.get method gets the value of each member variable

"Self,supper,id"

1) ID type, is the pointer type;

ID: Generic object pointer, similar to C-language void *

ID type can point to any object type, do not need to force the reload, can be used directly;

"ID Sample"

Practice:

"Composite class Design"

Refrigerator icebox storage items (ID)

Refrigerator class: Icebox

Elephant class: Elephant

Human: Person

Dogs: Dog

The refrigerator opens the door;

The refrigerator stores the item ID and prints out the item type;

Refrigerator closed;

===============================

Requirements: How do I invoke other methods inside a method?

Self and supper in the instance method?

1) Super

Super: The keyword used to invoke the parent class method;//temporarily understand the following

2) Self

Self: A pointer to the current object, with a pointer to the current object in each object, which is self;

Methods within the class can be called to each other using self

5.3. Initialization method

"How to initialize the method" -(ID) init;

Initialize the member variable when the object is created by the function of the Initialize method;

Initialize method format

1, without parameters of the construction method

-(ID) init;

2, the construction method with the parameter

-(ID) initwithxxxx: (parameter type) parameter 1 [Label]:(parameter type) parameter 2 ...;

Practice:

1. Create a road, member variables have length and width, write a construction method for length and width, and setter getter methods.

Construction method: Requires no parameters and two versions with parameters, without parameters, the default length is 1000, width is 1;

"Road"

2. There is a car class, price prices, brand type;

Three initialization methods;

Create and initialize a car by price

Create and initialize your car through branding

Create and initialize cars with price and branding

"Car"

Class 3.5 Methods

"Dog"

class method function:

"Create an object from a class method"

Member methods (instance methods)

Class method:

Practice:

1. Create a Postcard class card

Property:

Name

Unit

Phone number

Method:

Construction method (with parameters and without parameters)

creating objects from class methods (with parameters and without parameters)

Display information methods:

Homework:

1. Create a bank account class

Property:

Account number

Password

Storage Amount

Storage life

Method:

Construction method

Setup, account number, password

Deposit

Set years

Withdrawal

2. There is a fraction of the class, containing the numerator and denominator, to implement the construction, set the value method, to achieve two points of the +,

-, *,/arithmetic;

OBJECTIVE-C Classes and objects

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.