OC object: Examples of encapsulation, inheritance, and polymorphism I. oc Polymorphism
// The example of this code in the online video. It is suitable for students with unclear class and object content. It is only for learning and sharing. Thank you.
// Create a Pointtest class, which uses attributes x and y to represent the Coordinate Position of a vertex and calculate the distance between two points. Two methods are used: class method and object method.
# Import <Foundation/Foundation. h>
# Import <math. h> // use the square root and square root functions pow and sqrt. Therefore, declare the header file.
// Declare the attributes and methods of the class
@ Interface Pointtest: NSObject
{
Double _ x; // sets the x coordinate.
Double _ y; // set y coordinates
}
// Set the setter and getter methods of x. You should know what this is.
-(Void) setX :( double) x;
-(Double) x;
// Set the setter and getter methods of y. You should know what this is.
-(Void) setY :( double) y;
-(Double) y;
// Set to get the x and y values at the same time. Why do I need to set it once? Because of the encapsulation idea: hiding the attributes and implementation details of an object
-(Void) setX :( double) x andY :( double) y;
// Write an object method to calculate the distance between the vertex object and other vertex objects.
-(Double) distanceWithOther :( Pointtest *) other;
// Write a class method to calculate the distance between two vertex objects
+ (Double) distanceBetweenPoint1 :( Pointtest *) p1 andPoint2 :( Pointtest *) p2;
@ End
// Implementation
@ Implementation Pointtest
// Set the setter and getter methods of x. You should know what this is.
-(Void) setX :( double) x
{
// You can add judgment statements or other algorithms here to achieve encapsulation effect.
_ X = x;
}
-(Double) x
{
Return _ x;
}
// Set the setter and getter methods of y. You should know what this is.
-(Void) setY :( double) y
{
// You can add judgment statements or other algorithms here to achieve encapsulation effect.
_ Y = y;
}
-(Double) y
{
Return _ y;
}
// Set to get the x and y values at the same time. Why do I need to set it once? Because of the encapsulation idea: hiding the attributes and implementation details of an object
-(Void) setX :( double) x andY :( double) y
{
// Directly use the method for creating the object to call the settings, so you don't have to worry about how the methods are implemented, and the purpose of encapsulation is achieved.
[Self setX: x];
[Self setY: y];
}
// Write an object method to calculate the distance between the vertex object and other vertex objects.
-(Double) distanceWithOther :( Pointtest *) other
{
// Formula for calculating the distance between two points: (x1-x2) square + (y1-y2) Square Root
Double x1 = [self x];
Double x2 = [other x];
Double y1 = [self y];
Double y2 = [other y];
Double powX = x1-x2;
Double powY = y1-y2;
Double powSum = pow (powX, 2) + pow (powY, 2 );
Return sqrt (powSum); // o.0 is too detailed here
}
// Write a class method to calculate the distance between two vertex objects
+ (Double) distanceBetweenPoint1 :( Pointtest *) p1 andPoint2 :( Pointtest *) p2
{
// It is very important to understand it here. It is implemented using the object method here.
Return [p1 distanceWithOther: p2];
}
@ End
// Main Function
Int main ()
{
// Create a vertex (13, 10)
Pointtest * d1 = [Pointtest new];
[D1 setX: 13 andY: 10]; // you do not need to set a single
// Create a vertex (10, 14)
Pointtest * d2 = [Pointtest new];
[D2 setX: 10 andY: 14];
// Call the object Method
Double distance1 = [d1 distanceWithOther: d2];
// Call the Class Method
Double distance2 = [Pointtest distanceBetweenPoint1: d1 andPoint2: d2];
NSLog (@ "Object method calculated result % f, class method calculated result % f", distance1, distance2 );
Return 0;
}