第二天接触OC的“类”、“对象”、“方法”

今天讲的内容我没听太明白,感觉没有昨天那么清晰,这样总结一下,也算是归纳归纳,完了还能再看看。。。

今天讲了两个编程,结果晚上吃完晚饭一回来就忘了。。。坑

第一个是创建一个类,并实现它


      首先创建一个新的文件,我将他命名为“ car ”,然后在新建的接口文件里面,定义一些该“ 类 ”事物的一些特征,例如车的话,有它的重量、价格、颜色、车名等等

但是这里会有一个点需要注意:

      以下程序在 Car.h 文件中。     

      @public、@private、@protected       

      如:@interface  Car :NSObject {

         @public

                NSString   *color;

                float             weight;

          @private      

                 float             price;

          @public

                 NSString    *name;

  }

                  - (void)carwithspeed:(float)weight andpirection:(int)mama;

     上述程序的主要功能是做一个声明。


     接下来第二步的程序是在  Car.m  中实现,主要完成的功能是实现上一个声明。下面的程序很简单有一个模式:

          - (void)carwithspeed(方法名) : ( float )weight(参数名1) anddirection(方法名) : ( int )mama(参数名2){

             NSLog(@"carwithspeed:%.2f pirection:%d",weight,mama);

   }

               这个模式就是“ 将该 '  - (void)  '方式复制下来,然后粘贴上  Car.m   去掉分号, 加一个大括号  里面的内容只有:
          NSlog(@" 任意名称 + :+ %d / f   任意名称+ :+%d / f , 参数名1,参数名2  "); ”


       最后一步是调用这个类方法,这个程序在 Viewcontroller.m 或者 Appdelegate.m  里面实现都可以完成。
因为这两个文件里面都有可执行的程序,所以不管哪里,只要能被执行,那么就能调用。
两个可执行的代码分别为:

第一个:

               - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

                   //    int age;

                       return YES

               }

第二个:

               //程序现实前调用

               - (void)viewDidLoad {

                     [super viewDidLoad];

               }

   最后一步的代码也较为简单: [car01  carwithspeed  203.2837784   anddirection  2323 ];        


@implementation ViewController


//程序现实前调用

- (void)viewDidLoad {

    [super viewDidLoad];

    Car *car01 = [[Car alloc] init];

    car01->weight= 0;

    car01->name = @"QQ";

    [car01 run];

    [car01  carwithspeed 203.2837784  anddirection 2323];        

}


第二个是用代码编写一面国旗

 我编的是一面德国国旗,世界杯太牛了

    德国国旗的编码没一种颜色只有4行:

@implementation ViewController

            

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    UIView *redview = [[UIView alloc]init];

    redview.frame = CGRectMake(0, 60, 130, 30);

    redview.backgroundColor = [UIColor redColor];

    [self.view addSubview:redview];

    

    UIView *blackview = [[UIView alloc]init];

    blackview.frame = CGRectMake(0, 30, 130, 30);

    blackview.backgroundColor = [UIColor blackColor];

    [self.view addSubview:blackview];

    

    UIView *yellowview = [[UIView alloc]init];

    yellowview.frame = CGRectMake(0, 90, 130, 30);

    yellowview.backgroundColor = [UIColor yellowColor];

    [self.view addSubview:yellowview];

    

    UIView *redview1=[[UIView alloc]init];

    redview1.frame=CGRectMake( 180, 104, 130, 16);

    redview1.backgroundColor=[UIColor redColor];

    [self.view  addSubview:redview1];

    

    UIView *whiteview1=[[UIView alloc]init];

    whiteview1.frame=CGRectMake(180, 15, 130, 10);

    whiteview1.backgroundColor=[UIColor whiteColor];

    [self.view addSubview:whiteview1];

    

    UIView *yellowview1=[[UIView alloc]init];

    yellowview1.frame=CGRectMake(180, 45, 130, 43);

    yellowview1.backgroundColor=[UIColor yellowColor];

    [self.view addSubview:yellowview1];

    

    UIView *blueview=[[UIView alloc]init];

    blueview.frame=CGRectMake(180, 88, 130, 16);

    blueview.backgroundColor=[UIColor blueColor];

    [self.view addSubview:blueview];

}




































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值