Functions and objects, function objects

Source: Internet
Author: User

Functions and objects, function objects

Example: Write a car class

 

// Declare the class

@ Interface Car: NSObject

{

Int _ speed; // speed

Int _ wheel; // wheel

}

-(Void) run;

@ End

 

// Class implementation

@ Implementation Car

-(Void) run {

NSLog (@ "car speed: % I, wheel is % I", _ speed, _ wheel );

}

@ End

// The function does not depend on the class.

Void test1 (Car * _ car ){

_ Car-> _ speed = 120;

_ Car-> _ wheel = 2;

}

 

// OC classes are all data types and can be used as parameters

Void test2 (Car * _ car ){

Car * car2 = [Car new];

Car2-> _ wheel = 4;

Car2-> _ speed = 200;

 

_ Car = car2

 

_ Car-> _ speed = 220;

_ Car-> _ wheel = 6;

}

 

# Import <Foundation/Foundation. h>

Int main (){

Car * car1 = [Car new];

Car1-> _ speed = 100;

Car1-> _ wheel = 4;

  

Test2 (car1 );

[Car1 run]; // The result is still 100 4.

Return 0;

}

 


Differences between functions and objects in java

// Assume that the object is a public class Person {private String name; // the object can have the private int age attribute; // age attribute // This is the method (or function). This is the public void eat () {System. out. println ("eat");} // This is the public boolean isFat (int kg) with the returned value {if (kg = 100) {System. out. println ("Too fat ");}}}

Object and member functions

A class is a data type of c ++. It is an encapsulation body of an attribute (data) set and its operations (Actions). An object is a specific instance of a class, has the attributes of this class; the two are the relationship between commonality and individuality.
When defining a class, it generally includes declaring data members and member functions. member functions specify the behavior attributes of the class. Of course, the constructor is also a member function.
You can understand the first two questions. Finally, this object does not completely belong to this class. It has its own unique data and behavior, so it is an object of A derived class of this class, that is, all attributes of a derived class that has a base class. In the final analysis, an object is an instance of the corresponding class.

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.