(object-c) Study notes (ii)--oc language object-oriented and basic concepts

Source: Internet
Author: User
Tags instance method

Most of today's advanced programming languages are object-oriented programming.

So what is object-oriented? I use my opinion for an example: Suppose you are editing two things with Word 1. A person walks to the front of the table (1) Pick up the Coke (2) Open the lid (3) Drink light (4) Drop the bottle (5). 2. A man walks to the front of the table (1) picks up the dessert (2) and eats (3).

So if you're programming in a process-oriented way, the first thing is a curly brace that says these 5 steps. The second thing is a curly brace that says 3 steps. Write so two things all handwriting, do not allow paste copy, altogether 8 steps.

If you use an object-oriented approach to programming, divide the process into actions and items, write them out but put them aside, copy and paste whatever you need, and the same action or item doesn't have to be written two times. The above two things to write a total of 7 steps on the line, of course, if you are the second thing to drink Sprite, you only need 5 steps + a bottle of Sprite on the line.

1. Process oriented

Data structure + algorithm

2. Object-oriented

Attribute + behavior

  Why use object-oriented programming ideas

  Using object-oriented programming idea can greatly reduce the development cycle and cost, improve the development efficiency, improve the reusability of code, reduce the difficulty of development, improve the readability and maintainability of code.

What is a class

An important object-oriented concept. Class is an abstract concept, which is what we often call a kind of person, that is the meaning. For example, students, workers, this is a large part of the people represented. Specifically, in the OC language, the class is a pair of associated. M. h files (with the same name). This pair is a class.

What is an object

An object is an instantiated class, such as a student who has a Zhang San, not the same as an abstract student, you can know exactly Zhang San ID, name, height, weight, etc.

Variable

  What is declared in. h is called a public variable, which is declared in. m as a private variable.

@public can be accessed at any location

@package can be accessed inside the package

@protect can be accessed in this class and in subclasses

@practice can only be accessed in this class

Member variables and instance variables

  A variable declared in a class is called a member variable, and when the class is instantiated into an instance, the variable in the instance is called an instance variable.

Method

  A method is a part of a class that represents a function that a class can do, similar to C.

Method definition:-(void) show; multi-parameter method:-(void) Show: (nsstring) name andage: (int) age;

Void inside parentheses is the return value returns what type of write type

Instance methods and class methods

  -The first is an instance method, which can only be called by an instance object. + starts with the class method, which is called directly by the class name.

What is a property

Attributes are composed of three parts, the setter and getter methods, and an instance variable that holds the value. In today's iOS version, setter and getter are hidden, and we define attributes only in. h or. m

@property nsstring* name; defined on the line.

Example code:

#import <Foundation/Foundation.h>@interface  trstudent:nsobject@property nsstring *intint  ID; @property nsstring* address; -(ID) Initwithname: (nsstring*) name andage: (int) Age Andid: (int) ID andaddress: ( nsstring*) address; -(void) show; @end
#import "TRStudent.h"@implementationtrstudent-(ID) init{if(self=[Super Init]) {Self.name=@"Liu"; Self.age= $; Self.id=123456; Self.address=@"Baotu Spring"; }    returnSelf ;}-(ID) Initwithname: (NSString *) name Andage: (int) Age Andid: (int) ID andaddress: (NSString *) address{if(self=[Super Init]) {Self.name=name; Self.age=Age ; Self.id=ID; Self.address=address; }    returnSelf ;}-(void) show{NSLog (@"%@%d%d%@", self.name,self.age,self.id,self.address);}@end
intMainintargcConst Char*argv[]) {@autoreleasepool {trstudent*trs=[[Trstudent alloc]init];        [TRS show]; Trstudent*trs1=[[trstudent Alloc]initwithname:@"Wang"Andage: theAndid:1234Andaddress:@"Autumn Leaves Valley"];    [Trs1 show]; }//cocoa. H. M-min Open    return 0;}

(object-c) Study notes (ii)--oc language object-oriented and basic concepts

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.