////main.m//04-Create Class 2////Created by Apple on 14/11/17.//Copyright (c) 2014 itcast. All rights reserved.//#import<Foundation/Foundation.h>/*class Name: Zombies properties: Life, color, position, shape behavior: Walk, Bite, die, Loselife shells class name: Cannonball properties: Size, power, flight distance, speed behavior: Fly, Explode car class name: Car properties: Carrier, speed, license plate behavior: Run, stop Student class Name: Student attribute: Name, school number, gender behavior: learning, truancy Mobile: Class Name: Phone properties: size, size, color, _cup, memory behavior: Call, send SMS*///there is no namespace in OC and package name is not supported//when OC defines classes, enumerations, structs, they need to be prefixed.//Zombies//Statementtypedefenum_czcolor{czcolorwhite, Czcolorblack, czcoloryellow} czcolor;typedefstruct_czpoint{intx; inty;} Czpoint;@interfaceczzombies:nsobject{@public int_life;//Health ValueCzcolor _color;//ColorCzpoint _position;//location}/** * go to that position * * @param position go to the position*/- (void) Walkto: (czpoint) position;- (void) bite;@end@implementationczzombies/** * go to that position * * @param position go to the position*/- (void) Walkto: (czpoint) position{_position=position; NSLog (@"go to the location of (%d,%d)", _POSITION.X,_POSITION.Y);}- (void) bite{NSLog (@"Bite");}@endvoidTest () {//Creating Objects//there is no relationship between multiple objects created by the same class, and member variables are owned by themselves and have no relation to other objectsCzzombies *zombie = [czzombiesNew]; Czpoint position= {Ten,Ten}; [Zombie walkto:position]; Czzombies*zom = [czzombiesNew]; NSLog (@"zom position (%d,%d)",zom->_position.x,zom->_position.y);}/*Mobile: Class Name: IPhone properties: Size, color, model behavior: Aboutphone call, Callto: (char *) phonenum texting sendMessage: (NSString * Content to: (NSString *) Phonenum*///define a size structure bodytypedefstruct_czsize {DoubleWidth//Wide DoubleHeight//High} czsize;//to prevent the prefix from being removed from the previous conflicttypedefenum_color{colorwhite, Colorblack, coloryellow} Color;//Define Phone class//Statement@interfacecziphone:nsobject{@publicczsize _size;//DimensionsColor _color;//ColorNSString * _model;//Model}/** * About mobile phone*/- (void) Aboutphone;/** * Call * * @param phonenum mobile phone number*/- (void) Callto: (NSString *) Phonenum;/** * Send SMS * * @param content text message * @param phonenum mobile phone number*/- (void) SendMessage: (NSString *) content to: (NSString *) Phonenum;@end@implementationCziphone/** * About mobile phone*/- (void) aboutphone{NSLog (@"This Part%@ color is%d, size width =%.2lf,height =%.2lf", _model,_color,_size.width,_size.height);}/** * Call * * @param phonenum mobile phone number*/- (void) Callto: (NSString *) phonenum{NSLog (@"give%@ a call .", Phonenum);}/** * Send SMS * * @param content text message * @param phonenum mobile phone number*/- (void) SendMessage: (NSString *) content to: (NSString *) phonenum{NSLog (@"send SMS to%@ with%@ content", phonenum,content);}@endintMainintargcConst Char*argv[]) { //Creating ObjectsCziphone *myphone = [CziphoneNew]; Myphone->_color =Colorwhite; Myphone->_size = (czsize) { the,480}; Myphone->_model =@"iphone4s"; [Myphone Aboutphone]; [Myphone callto:@"10086"]; [Myphone sendMessage:@"wife's not coming home for dinner."To:@" the"]; return 0;}
Summarize:
/*
1,OC Introduction:
OC is a superset of the C language, and it adds the smallest object-oriented syntax based on the C language .
2, compare C to learn OC
data types, keywords, process controls, functions
3. Object-Oriented thinking
1, it is a more close to the real life of a programming idea
2, it thinks the question to emphasize who to do
3, to complete a task, first to find the completion of the Task object, if not create a, and then command the object to do things.
4. Objects
Object-oriented objects in all things
from another point of view, any specific thing in real life can be considered an object.
5. Class
from the design of programming: The abstract of a class of things in reality is to extract the properties and behaviors common to this kind of things.
from programming language: Creating objects must be classes, objects are instances of classes;
6. Relationship between class and object
the relationship between automobile drawing and automobile
7. Design Class
1.design a class by means of a text description
noun Extraction: a class can be extracted whenever a related noun is encountered
2, take a look at the picture to extract a class
There are some common things on the graph that can be a commission of a class
A class consists of three parts:
1.class Name: Name of this kind of thing
2. Attributes: Some kinds of things share some state characteristics
3. Behavior: Some kinds of things share some functional characteristics
8. Create a class
0. Design class
1, statement: is the summary of the description, it is for the use of the people to see
1. Determine the class name
2, to inherit the NSObject, is to let the class have the ability to create objects
3. The declaration must begin with a @interface with the end of the @end
4.declare attributes, must be in curly braces between @interface and @end
property cannot be initialized when declaring properties
5, the declaration method, must be between {} and @end declaration, can not have the method implementation
Format: Method type identifier ( return value type ) method name :( parameter type ) parameter name method name :( parameter type ) Parameter name
1.all data types should be expanded with parentheses.
2. One parameter corresponds to a colon
3. The colon is part of the method name
2.implementation: Declaration of all Methods
must be written in @implementation and @end
Note:1, only the declaration does not implement the class, the link when the error
2, only the implementation of non-declared classes, you can use (OC Weak syntax ), but it is not possible to do this when programming
3, Declaration and implementation can not be nested, the inside of a class cannot declare other classes
4, less @end
5.the declared attribute is not written in curly braces
6.the method of declaration is written in curly braces
7, call the object method of the class only through the class object, cannot be called directly like a function
8.the function cannot directly access the member variables of an object
9. Functions and Methods
1, functions are parallel, no one who belongs to the relationship
2, variables defined in the function are local variables, the function can directly manipulate the member variables of a class
3, the function call is directly through the function name to call
Object method:
1, which is subordinate to the object of the class, only the object of that class is called
2.member variables can be accessed directly in the object method, because they are all objects that belong to the class.
Creation of Objects
Object * obj = [ class name new];
1. Createstorage space for class objects in the heap
2.Initialize all member variables to 0 if the object type is initialized to nil
3.returns a pointer to the object
*/
OC Base Notes--Create Class 2 (Guangzhou Black Horse first phase)