继承:
"Triangle.h"
#import <Foundation/Foundation.h>
@interface Triangle : NSObject
@property double a;
@property double b;
@property double c;
-(instancetype)initWithA:(double)a B:(double)b C:(double)c;
-(double)area;
@end
"Triangle.m"
#import "Triangle.h"
@implementation Triangle
-(instancetype)initWithA:(double)a B:(double)b C:(double)c
{
self=[super init];
if (self)
{ self.a=a;
self.b=b;
self.c=c;
}
return self;
}
-(double)area
{
double s1=(self.a+self.b+self.c)/2;
return sqrt(s1*(s1-self.a)*(s1-self.b)*(s1-self.c));
}
@end
"TriangleB.h"
#import "Triangle.h"
@interface TriangleB : Triangle
-(double)halfarea;
@end
"Triangle.m"
#import "TriangleB.h"
@implementation TriangleB
-(double)halfarea
{
return [self area]/2;
}
@end
"main.m"
#import <Foundation/Foundation.h>
#import "Triangle.h"
#import "TriangleB.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// Triangle *ta=[[Triangle alloc]init];
// ta.a=3.0;
// ta.b=4.0;
// ta.c=5.0;
Triangle *ta=[[Triangle alloc]initWithA:3.0 B:4.0 C:5.0];//类同上4句,初始化变量;
NSLog(@"%.2f\n",[ta area]);
// TriangleB *tb=[[TriangleB alloc]init];
// tb.a=8.0;
// tb.b=6.0;
// tb.c=10.0;
TriangleB *tb=[[TriangleB alloc]initWithA:8.0 B:6.0 C:10.0];
NSLog(@"%.2lf\n",[tb halfarea]);
NSLog(@"%.2lf\n",[tb area]);
}
return 0;
}
复合:
在OC的复合模式就是把其他对象作为自身的题部分,以提升自身的功能。
比如我现在要制作一台电脑,电脑需要CPU,显示器,鼠标和键盘等。这些东西的研发都是很复杂的过程。如果现在有成型的CPU等组件,就可以直接用这些组件攒一台电脑。复合模式就是这样。
所有我们在制作电脑前要先找到这些组件。这些组件并不需要我们来制作,有专业的公司提供这些成型的组件。
例子:
在上题基础上,先建立”Triangle.h“、”Triangle.m”、”TriangleB.h“、”TriangleB.m”文件,再建立"Computer.h"和“Computer.m"文件
"Computer.h"
#import <Foundation/Foundation.h>
#import "TriangleB.h"
@interface Computer : NSObject
@property Triangle *aa; //复合了Triangle类
@property TriangleB *bb; //复合了TriangleB类
-(double)threea; //自己的方法
-(double)foura; //自己的方法
-(instancetype)initWithA:(double)a B:(double)b C:(double)c;//自定义初始化方法
@end
"Computer.m"
#import "Computer.h"
@implementation Computer
-(instancetype)initWithA:(double)a B:(double)b C:(double)c//自定义初始化带参数
{ self=[super init];
if (self)
{
self.aa=[[Triangle alloc]initWithA:a B:b C:c];
self.bb=[[TriangleB alloc]initWithA:a B:b C:c];
}
return self;
}
-(instancetype)init //init初始化 不带参数
{ self=[super init];
if (self)
{
self.aa=[[Triangle alloc]init];
self.bb=[[TriangleB alloc]init];
}
return self;
}
-(double)threea
{
return [self.aa area]/3;
}
-(double)foura
{
return [self.bb halfarea]/2;
}
@end
"main.h"
//computer 复合(可以包含其他类,使用其他类的方法和变量,作为自己的一部分)
#import <Foundation/Foundation.h>
#import "Computer.h"
int main(int argc,const char *argv[])
{
Computer *a=[[Computer alloc]init];//对象的init方法
a.aa.a=3.0;
a.aa.b=4.0;
a.aa.c=5.0;
NSLog(@"%.2lf\n",[a.aa area]); //实际上是Triangle类的area方法
Computer *a1=[[Computer alloc]initWithA:3 B:4 C:5];//自定义对象的初始化方法
NSLog(@"%.2lf\n",[a1.aa area]);
Computer *b=[[Computer alloc]init];
b.bb.a=8.0;
b.bb.b=6.0;
b.bb.c=10.0;
NSLog(@"[b.bb area]=%.2lf\n [b.bb half]=%.2lf\n [b foura]=%.2lf\n",[b.bb area],[b.bb halfarea],[b foura]);//b.bb area是包含的a的area方法
//b.bb halfarea 是包含b的halfarea方法 b foura是自己的方法
Computer *b1=[[Computer alloc]initWithA:8 B:6 C:10];
NSLog(@"[b.bb area]=%.2lf\n [b.bb half]=%.2lf\n [b foura]=%.2lf\n",[b1.bb area],[b1.bb halfarea],[b1 foura]);
return 0;
}