Presentation 4th
-
Upload
gl-finance-plc -
Category
Documents
-
view
105 -
download
0
Transcript of Presentation 4th
![Page 1: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/1.jpg)
More On Variable,Categories and Protocols
Chhorn Chhaly
Leang Bunrong
Cheng Udam
Kan Vichhai
Srou Chanthorn
Em Vy
Seth Sambo
Chea Socheat
![Page 2: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/2.jpg)
Content1. More on Variables and Data Types
Initialize Objects
Scoped Revisited More on properties, Synthesized Accessors, and Instance Variables
Global Variable
Static Variable
Enumerated Data Types
The typedef statement
Data Type Convers
![Page 3: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/3.jpg)
• Categories and Protocols
Categories
Class Extensions
Protocols and Delegation
Composite Object
![Page 4: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/4.jpg)
Initializing Objects
• We initializing object by calling “init” method
• For example Fraction *f=[[Fraction alloc]init];
• The example above we just get an Object that has nothing or field value
• If we want to initialize object with many fields value, we have to create our custom method to initialize object
![Page 5: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/5.jpg)
Objective-C Java
![Page 6: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/6.jpg)
Initializing Objects
• The method initWithNumerator:(int)n withDenominator:(int)d is the custom method to initializing object
• All initializing methods must start with word “init…..”
• Self=[super init] means the super class calls “init” method to initialize for current object
• But it just an object without any fields value
• After that line we see that we initialize _numerator and _denomitor
• Then we return “self” that fulfill of object with field values
![Page 7: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/7.jpg)
Variable Scope
In Objective-C, a variable has been declared it may or may not be accessible to other sections of the program code. This accessibility depends on where and how the variable was declared and where the code is that needs to access it. This is know as variable scope.
![Page 8: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/8.jpg)
Local Variables
Variables that are declared inside a function or block are called local variables. They can be used only by statement that are inside that function or block of code. Local variable are not known to functions outside their own.Example:
Objective-C:-(void) sum:(int) x secondP:(int) y {
int total;total = x+y;NSLog(@”Total: %i”, total);
}NSLog(@”Total: %i”, total); // illegal Total is now out of scope.
Java:public void sum(int x, int y){
int total;total = x+y;System.out.println(“Total: ” + total);
}System.out.println(“Total: ”+ total); // illegal Total is now out of scope.
![Page 9: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/9.jpg)
Global VariablesGlobal variables are defined outside of a function, usually on top of the program. A global variable can be accessed by any function. That is, a global variable is
Objective-C#import <Foundation/Foundation.h>Int myVar = 321;Int main (int argc, conts char * argv[]){ @autoreleasepool{
NSLog(@”myVar = %i”, myVar); } return 0;}
The second file, named display.m contain the code for the displayit() function.
#import <Foundation/Foundation.h>extern int myVar; void displayit(){ NSLog(@”MyVar from different source file is i%”, myVar);}
JavaPublic class Global{ Int myVar = 321; public static void main (String args[]){
System.out.println(“myVar = ”+ myVar);}
void displayit(){ System.out.println(“myVar = ”+myVar); }}
![Page 10: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/10.jpg)
Multiple Classes In .h file In the Models.h file we declare two classes
![Page 11: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/11.jpg)
Multiple Classes in .h file(cont.)
Import Models.h we get two classes
![Page 12: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/12.jpg)
Accessing static and non static field to category Models class(.h.m)
![Page 13: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/13.jpg)
Accessing static and non static field to category Calling static field(check) and non static field(modelsfield) to MyCatagory.
![Page 14: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/14.jpg)
Enumerated Data Types
• Enumerated Type : Is an user-defined type that is used in Objective-C to represent the
data that can be stored in one of several predefined values (all of them are int).
• What is it’s purpose : The purpose of enumerated types is to create specifically named
constants.
• Syntax : enum name_of_enumerator { element1, element2,…};
• enum : tell compiler to create enumeration
• name_of_enum : is the name to be assigned the enumeration
• element: field are the name that can be used to set variable
![Page 15: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/15.jpg)
Enumerated Data Types
NOTE : • enumerators use numbers corresponding to the element
• By default the first value corresponds to 0, the second to 1 and so on
• It is also possible to specify the values used by each value name
enum temperatureTypes{ cold = 10, // (redefined up = 10) warm, // down will 11 (increment one from
previous) hot = 95, // (redefined left = 95) veryhot // right will 96 (increment one from
previous) };
Example
![Page 16: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/16.jpg)
The Typedef Statement
• The Typedef Statement : allows the programmer to define one Objective-C type as
another.
• To define a new typedef :
• Write the statement as if a variable of the original type was being declared
• Where you would put the name of the variable (after the type), place the new type name.
• Prefix everything with typedef
![Page 17: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/17.jpg)
The Typedef Statement
• Syntax:
• Example:
in this example above, define the name Counter to be equivalent to the Objective-C data type int.
The second line we declare variable to be type of Counter.
typedef DataType variable;
typedef int Counter;
Counter i, j;
![Page 18: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/18.jpg)
Data Type Conversion
• Type casting is a way to convert a variable from one data type to another data type.
• In Objective-C when we evaluation of the two operands, type of the result is convert to the big size between the two operands automatically but except char, short int or of an enumarated data type they are converted to int.
• Example: Output
![Page 19: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/19.jpg)
• Example:Output
![Page 20: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/20.jpg)
Data Type Conversion (cont.)
• In Objective-C is almost the same with Java but still have a little bit different below:
• In Java long and short are data type but in Objective-C is not so it make a conversion different.
In Java:In Objective-C:
![Page 21: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/21.jpg)
Categories Category is the best way to add more methods to class in objective – C language.
We can override method in base class but it is poor programming practice( duplicate functionalities of method when running time).
Name of category start with base class plus category name( BaseClassName+CategoryName) in import section.
.m
.h
class
Category
![Page 22: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/22.jpg)
Categories
Syntax:
@interface ClassName (CategoryName)
//// methods declaration part
@end
![Page 23: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/23.jpg)
Categories
Cateories Example:
MyModel class has only one method (ownMethod).
![Page 24: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/24.jpg)
CategoriesCateories Example:
CategoryOfMyModel has one method (newMethod).
![Page 25: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/25.jpg)
CategoriesCateories Example:
MyModel
-(void)ownMethod-(void)newMethod
Compile time
MyModel
-(void)ownMethod
MyModel+CategoryModel
-(void)newMethod
![Page 26: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/26.jpg)
Class Extensions
• Class extension is a special case allows creating a category without a name
- when you define an unnamed category like this, you can extend the class by adding
additional instance variables and properties.
- This is not allowed for named categories.
![Page 27: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/27.jpg)
Class Extensions Cont......
MyClass.h
MyClass.m
![Page 28: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/28.jpg)
Class Extensions Count......
Main method
![Page 29: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/29.jpg)
Protocol• protocol fully complete abstraction every method in protocol is abstract
• it is list the method name without body, it wait other class implement it and implement body to it.
• we use it to create the standard method for all classes use the same method when they implement protocol, it is look like interface in java.
• and it use for delegate also
syntax in objective c syntax in java
![Page 30: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/30.jpg)
Protocol
• in objective divided two part of protocol @required and @optional
• @required it mean that we need to implement method when the class implements it.
• @optional it mean that we are not need to implement method when the class implements it, it is up to you.
syntax:
![Page 31: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/31.jpg)
Protocol
• we can implement more than one protocol
• how to implement single and multiple implement protocol
• first we need to import protocol
single implementation in objective c Single implementation in java
![Page 32: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/32.jpg)
Protocol
multiple implementation in objective c multiple implementation in java
![Page 33: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/33.jpg)
Delegate
• Delegate allow one object send message to other object when an event happen
• We use delegate almost for event handler in IOS
• We can use delegate when we want it to do something when something happen in our program and raise it up.
• We almost use id for create a delegate
• We will raise it detail in later chapter that detail about the delegate
![Page 34: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/34.jpg)
Composite Object• What is composite object?
A technique that a class consists of one ore more objects from other classes is call “Composite Object”.
Diagram above show you that ClassA has ClassB as its composite.
If ClassA is deleted then ClassB is also deleted as a result. But if ClassB is deleted ClassA will not be delete.
ClassA ClassB
![Page 35: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/35.jpg)
Composite Object
• Example of composite object:
![Page 36: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/36.jpg)
Composite Object
• Let Create other Square class that have Rectangle as its composite
![Page 37: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/37.jpg)
Composite Object
• In our main.m file
![Page 38: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/38.jpg)
Composition Object
• Let compare our code with java code:
Rectangle.java
![Page 39: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/39.jpg)
Composite Object• Square.java
![Page 40: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/40.jpg)
Composite Object
• Main.java
![Page 41: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/41.jpg)
Composite Object
• The result of these two will output the same
![Page 42: Presentation 4th](https://reader033.fdocuments.in/reader033/viewer/2022061612/587c0cef1a28ab03768b5d3d/html5/thumbnails/42.jpg)
Thank you