objectc老师上课例题程序Word格式文档下载.docx
《objectc老师上课例题程序Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《objectc老师上课例题程序Word格式文档下载.docx(36页珍藏版)》请在冰点文库上搜索。
//rectangle=[[Rectanglealloc]init];
rectangle=[Rectanglenew];
//开辟空间并初始化
//[rectanglesetLength:
1.23];
//[rectanglesetWidth:
2.34];
[rectanglesetWidth:
1.23andLength:
[rectangleperemeter];
[rectanglearea];
}
return0;
}
16号例三
//Rectangle.h
//Test3
@interfaceRectangle:
NSObject
@propertydoublelength,width;
-(void)peremeter;
-(void)area;
@end
//Rectangle.m
@implementationRectangle
@synthesizelength,width;
-(void)peremeter{
NSLog(@"
周长为:
%g"
2*(length+width));
-(void)area{
面积为:
length*width);
rectangle=[[Rectanglealloc]init];
[rectanglesetLength:
2.2];
17号例一
//Circle.h
//Test1
//Createdbyuser16on13-11-17.
@interfaceCircle:
@propertydoubleradius;
-(void)perimeter;
//Circle.m
Circle.h"
@implementationCircle
@synthesizeradius;
-(void)perimeter{
2*3.14*radius);
3.14*radius*radius);
Circle*circle;
circle=[[Circlealloc]init];
[circlesetRadius:
1.2];
[circleperimeter];
[circlearea];
17号例二
//Rectangle.h
@classXYPoint;
-(void)setLength:
(double)landWidth:
(double)w;
-(void)setPoint:
(XYPoint*)point;
-(XYPoint*)getPoint;
XYPoint*xyPoint;
(double)landWidth:
(double)w{
length=l;
width=w;
-(void)perimeter{
2*(length+width));
(XYPoint*)point{
xyPoint=point;
-(XYPoint*)getPoint{
returnxyPoint;
@end
//Square.h
@interfaceSquare:
Rectangle
-(void)setSide:
(double)s;
//Square.m
Square.h"
@implementationSquare
-(void)setSide:
(double)s{
[selfsetLength:
sandWidth:
s];
重写后的周长:
4*[selflength]);
Square*square;
square=[[Squarealloc]init];
[squaresetSide:
4];
[squareperimeter];
[squarearea];
//Rectangle.m
17号例三
//XYPoint.h
@interfaceXYPoint:
@propertyintx,y;
-(void)setX:
(int)xValandSetY:
(int)yVal;
//XYPoint.m
XYPoint.h"
@implementationXYPoint
@synthesizex,y;
(int)xValandSetY:
(int)yVal{
x=xVal;
y=yVal;
XYPoint*point;
point=[[XYPointalloc]init];
[pointsetX:
25andSetY:
25];
[rectanglesetPoint:
point];
矩形的原点为(%i,%i)"
[[rectanglegetPoint]x],rectangle.getPoint.y);
17号例四
//People.h
//Test4
@interfacePeople:
-(void)catch:
(id)obj;
//People.m
People.h"
@classDog;
@classCat;
@implementationPeople
(id)obj{
//Cat.h
@interfaceCat:
-(void)yell;
//Cat.m
Cat.h"
@implementationCat
-(void)yell{
喵喵喵。
。
"
);
//Dog.h
@interfaceDog:
//Dog.m
Dog.h"
@implementationDog
旺旺旺。
Dog*dog=[[Dogalloc]init];
Cat*cat=[[Catalloc]init];
People*p=[[Peoplealloc]init];
[pcatch:
dog];
[dogyell];
[catyell];
iddataVal;
dataVal=dog;
[dataValyell];
dataVal=cat;
18号例一
//Createdbyuser16on13-11-18.
-(XYPoint*)initPointSetX:
(int)xValandSetY:
(int)xValandY:
-(NSString*)description;
//XYPoint.m
(int)yVal{
self=[superinit];
if(self){
returnself;
-(NSString*)description{
return[NSStringstringWithFormat:
@"
点为:
(%i,%i)"
x,y];
point=[XYPointalloc];
point=[pointinit];
XYPoint*point2;
point2=[[XYPointalloc]initPointSetX:
12andSetY:
point2.x,[point2y]);
18号例二
//XYPoint.h
//Te