Object-oriented syntax-02

Source: Internet
Author: User

    • Next, simulate real-life situations in OC and create a car. First you have to have a car class, and then use the car class to create a car object
                  1. Classes that define OC and objects that create OC
    • To describe a class in OC is a little cumbersome, in 2 steps: The declaration of the class, the implementation of the class (definition). Similar to functions, functions have a sub-declaration and definition
    1. Declaration of the class
    1. Code writing
    • Define a car class with 2 properties: Number of wheels, speed, 1 behaviors: Running
    • Naming rules for class names \ Properties: Rules for identifiers
    • Naming conventions for class names: Meaningful, hump-marked, first-letter capitalization
Declaration of #import <Foundation/Foundation.h>// class @interface  car:nsobject{     @public    int // How many wheels?    int // Speed }-(void//  run behavior @end

    1. Member variables
    • Variables declared in @interface curly braces {}: Wheels, Speed
    • @interface curly braces and curly braces for functions are not the same
    • Default is initialized to 0

    1. @public

@public allows the wheels and speed properties of the car object to be accessed by the outside world

    1. NSObject

Plus: The purpose of NSObject is to have the car class capable of creating objects

    1. class the implementation
// implementation of the class @implementation car-(void) run{    NSLog (@ "%i wheels,%i the speed of the car ran up. " , wheels, Speed);} @end

    1. Creating objects
    1. Code writing
// Main function int Main () {    //  Create car object      new];    C3;    C;        [C Run];     return 0 ;}

    1. Code analysis, memory analysis for the main function (objects have members in memory)
    • [Car New] Creates a new object each time, and returns the address of the object, then the address of the object should be saved with a pointer variable
New];

Use a pointer variable C to point to the in-Memory car object

    • Set the properties of a car object

As with pointers to structs, access struct properties,

3 ; c ;

    1. Create multiple Car objects
    • Set only wheels, speed properties, respectively
New ];c14new];c2; [ C1 run];

    • 1 assign to another, and then modify the property
New ];c14; C1*c2 = c1;c23; [ C1 run];

    1. The benefits of object-oriented encapsulation
    • Closer to the way people think
    • Just focus on the object and don't need to follow the steps

    1. object and Function parameters
    • Object member variables as function arguments
    • Pointer to object as function parameter
    • To modify a pointer to a member of an object
    • To modify the pointer's pointing

    1. Declaration and implementation of a class
    1. @interface and the @implementation Division of Labor

    • @interface's like exposing the outside clock surface
    • @implementation is like a structure hidden inside the clock.

    1. Declaring and defining multiple classes

    1. Common errors
    • Only the declaration of the class, no implementation of the class
    • Missed the @end.
    • @interface and @implementation nesting
    • Declaration nesting of two classes
    • Member variables are not written in parentheses
    • The declaration of the method is written in curly braces.

    1. Syntax details
    • Member variables cannot be initialized in {} and cannot be accessed directly
    • Method cannot be called as a function
    • member variable \ Method cannot be decorated with keywords such as static, do not mix with C language (temporarily ignore)
    • The implementation of the class can be written after the main function, mainly after the declaration.

    1. The difference between OC methods and functions
    • The OC method can only be declared between @interface and @end and can only be implemented between @implementation and @end. This means that the OC method cannot exist independently of the class
    • C functions do not belong to the class, and the class is not associated with the C function only to define the function of the file all
    • C functions cannot access members of OC objects
    • Low-level Error: The method has a declaration, but the implementation of the time is written as a function

    1. OC the method of attention
    • Method only declared, not implemented (classic error)
    • method is not declared, only implemented (compiler warning, but can invoke, OC's weak syntax)
    • Compile time: Access no member variable directly error, Access no method, just warning

    1. @ Implementation
    • There is no @interface, only @implementation, is also able to successfully define a class of
@implementation car:nsobject{    @public    int//  number of wheels    int//  speed }-(void) run{    NSLog (@ "%i wheels,% I ran at the speed of the car ", Wheels; @end

    • The same member variable cannot be declared in the @implementation as @interface

    1. Method

Design a Caculator calculator class, which has the function of calculation (behavior)

    1. Methods with no parameters
    • Design a method to return pi
// Method declaration -(double) pi; // method Implementation -(double) pi{    return3.14;}

    • Method invocation

    1. Method with one parameter
    • Design a method for calculating squares
// Method declaration -(double) square: (double) number; // method Implementation -(double) square: (double) number{    return number *  number;}

    • Method invocation

    1. A method with multiple parameters
    • Design a method of calculation and
// Method declaration -(double) sumOfNum1: (Double) num1 andNum2: (double)num2; // method Implementation -(double) sumOfNum1: (double) num1 andNum2: (Double)num2{     return num1 + num2;}

    • Method invocation

    1. Method Name Note
    • The colon is also part of the method name
    • Two object methods with the same name are not allowed in the same class

    1. Exercises

Car class design a method to compare the speed with other cars, if the car speed, return 1, if the car is slow, return 1, the same speed return 0

    1. Anonymous objects
    • Property access
      [Car  New ;

    • Method invocation
[[Car  New]  run];

Object-oriented syntax-02

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.