zoukankan      html  css  js  c++  java
  • 实现Square类,让其继承自Rectangle类,并在Square类增添新属性和方法,在2的基础上,在Square类中重写Rectangle类中的初始化和打印方法

    实现Square类,让其继承自Rectangle类,并在Square类增添新属性和方法,在2的基础上,在Square类中重写Rectangle类中的初始化和打印方法

     1 #import <Foundation/Foundation.h>
     2 
     3 @interface Rectangle : NSObject{
     4     int width;
     5     int height;
     6 }
     7 @property int width,height;
     8 -(int) area;
     9 -(int) perimeter;
    10 -(void)setWidth:(int)w andHeight:(int)h;
    11 -(void)print;
    12 
    13 @end
    14 
    15 
    16 #import <Foundation/Foundation.h>
    17 
    18 @interface Rectangle : NSObject{
    19     int width;
    20     int height;
    21 }
    22 @property int width,height;
    23 -(int) area;
    24 -(int) perimeter;
    25 -(void)setWidth:(int)w andHeight:(int)h;
    26 -(void)print;
    27 
    28 @end
    29 
    30 #import <Foundation/Foundation.h>
    31 #import "Rectangle.h"
    32 @interface Square : Rectangle
    33 
    34 -(void) setSide:(int) s;
    35 -(int)side;
    36 -(int)area;
    37 -(int)perimeter;
    38 -(void)print;
    39 
    40 @en
    41 
    42 #import "Square.h"
    43 
    44 @implementation Square
    45 
    46 -(void) setSide:(int) s{
    47     [self setWidth:s andHeight:s];
    48 }
    49 
    50 -(int) side{
    51     return width;
    52 }
    53 
    54 -(int)area{
    55     return width*width;
    56 }
    57 
    58 -(int)perimeter{
    59     return 2*(width+height);
    60 }
    61 
    62 -(void) print{
    63     NSLog(@"side  %d",width);
    64 }
    65 
    66 @end
  • 相关阅读:
    Django model 常用方法记录
    程序员的注意事项
    硬件天使的使用
    你是否应该成为一名全栈工程师?
    web技术
    6个处理上面代码异味的重构方法(手法)
    git 命定
    ie console报错
    apache 省略index.php访问
    myisam和innodb的区别
  • 原文地址:https://www.cnblogs.com/wsq724439564/p/3272916.html
Copyright © 2011-2022 走看看