OC Classes and objects

Source: Internet
Author: User

Main.m

OC01 Classes and objects

//

Created by Dllo on 15/7/15.

Copyright (c) 2015 CML. All rights reserved.

//

#import <Foundation/Foundation.h>

First header file

#import "Phone.h"

#import "Audio car.h"

#import "Student.h"

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

Print

NSLog (@ "@ Liushan");

Plastic

Nsinteger i = 100;

NSLog (@ "%ld", I);

Floating point Type

CGFloat f = 3.14;

NSLog (@ "%g", f);

String

OC string side Chinese no problem

NSString *str = @ "Liushan";

NSLog (@ "%@", str);

The array in OC

Nsarray *arr = @[@ "1", @ "2"];

NSLog (@ "%@", arr);

For (Nsinteger i =0; i<2; i++) {

NSLog (@ "%@", Arr[i]);

//    }

Create an Object

Creating an object takes two steps

1. Need to open space for the object, open up the memory of space

Student *stu = [Student alloc];

2. After the object memory is opened up, the object needs to be initialized

Stu =[stu Init];

Take two steps to merge

Student *stu =[[student alloc] init];

Invoking behavior through objects

[Stu Sayhi];

Action member variable

object to access its own member variables

NSLog (@ "%@", stu->_stuage);

//

Stu->_stuage = 100;

NSLog (@ "%ld", Stu->_stuage);

//

Change name

Stu->_stuname = @ "Yang Lin";

NSLog (@ "%@", stu->_stuname);

stu->_stuname [email protected] "Liushan";

NSLog (@ "%@", stu->_stuname);

Create objects by car class, and modify the member variables of the object

Phone *phone = [[Phone alloc] init];

Audio_car *car = [[Audio_car alloc] init];

[Car Run];

Car->_carcolor [email protected] "red";

NSLog (@ "%@", Car->_carcolor);

Create objects from the phone class, and modify the member variables of the object

Phone *phone =[[phone alloc] init];

Phone->_color [email protected] "red";

NSLog (@ "@ color", phone->color);

//

Student *stu = [[Student alloc] init];

NSLog (@ "%@", stu->_stuname);

//

Phone *phone =[[phone alloc] init];

NSLog (@ "%g", Phone->_price);

//

Ways to use Sayhi

[Phone sayhi];

Phone->_type = @ "Hello";

[Phone sayhi];

//

30 Classes

Five member variables

A Sayhi method that can print all the information

2 random ways.

Complete the override of the Init method

return 0;

}

//

Student.h

OC01 Classes and objects

//

Created by Dllo on 15/7/15.

Copyright (c) 2015 CML. All rights reserved.

//

#import <Foundation/Foundation.h>

@interface interface file, start of a class

Student is the current class name

NSObject is the inherited parent class

class to @end only to end

@interface Student:nsobject

Characteristics

{

@public

The visibility of member variables

Member variables, instance variables

NSString *_stuname; Name

NSString *_stusex; Gender

Nsinteger _stuage; Age

CGFloat _stuscore; Results

NSString *_stuhobby; Hobby

}

Behavior

Say hello

-(void) sayhi;

Eat

-(void) eat;

Play

-(void) play;

@end

Student.m

OC01 Classes and objects

//

Created by Dllo on 15/7/15.

Copyright (c) 2015 CML. All rights reserved.

//

#import "Student.h"

The corresponding implementation file

@implementation Student

Say hello

-(void) sayhi{

NSLog (@ "Hello");

}

Eat

-(void) eat{

NSLog (@ "Eat bar");

}

Play

-(void) play{

NSLog (@ "want to play");

}

Rewrite the inherited Init method.

-(ID) init{

_stuname [email protected] "HO Bank";

_stusex [email protected] "female";

[Email protected] "male";

_stuage = 20;

_stuscore=100;

return self;

}

@end

/Audio Car.h

OC01 Classes and objects

//

Created by Dllo on 15/7/15.

Copyright (c) 2015 CML. All rights reserved.

//

#import <Foundation/Foundation.h>

@interface Audio_car:nsobject

{

@public

Nsinteger _carmoney;

CGFloat _carv;

NSString *_carcolor;

CGFloat _cardisplacement;

NSString *_carsize;

CGFloat _carweight;

}

-(void) run;

-(void) fuelcharge;

-(void) knock;

@end

Audio CAR.M

OC01 Classes and objects

//

Created by Dllo on 15/7/15.

Copyright (c) 2015 CML. All rights reserved.

//

#import "Audio car.h"

@implementation Audio_car

-(void) run{

NSLog (@ "Drive");

}

-(void) fuelcharge{

NSLog (@ "");

}

-(void) knock{

NSLog (@ "");

}

@end

Phone.h

OC01 Classes and objects

//

Created by Dllo on 15/7/15.

Copyright (c) 2015 CML. All rights reserved.

//

#import <Foundation/Foundation.h>

@interface Phone:nsobject

{

@public

NSString *_phonename;

NSString *_phonecolor;

Nsinteger Phoneprice;

}

-(void) Tpboy;

-(void)

Write a function to print all the information

-(void) sayhi;

Phone.m

OC01 Classes and objects

//

Created by Dllo on 15/7/15.

Copyright (c) 2015 CML. All rights reserved.

//

#import "Phone.h"

@implementation Phone

How to override a phone's initialization

-(ID) init

{

_color = @ "White";

_price = 1999;

_type= @ "big screen";

_brand = @ "Huawei";

_screen = @ "4.7 inch";

return self;

}

-(void) Sayhi

{

NSLog (@ "%@,%@,%@,%@,%g", _brand, _type, _color, _screen, _price);

}

@end

OC Classes and objects

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.