zoukankan      html  css  js  c++  java
  • 一、初始Object-C

    一。OC概述

    特点:

    1没有包得概念

    2关键字以@开头

    3.拓展名 .m

    二。第一个OC

    1,分为2个文件。.m和.h文件

    2. .m文件用来实现类  .h用来定义声明类

    .h文件中得写法

    //@interface 代表声明一个类

    //:表示继承

    #import <Foundation/Foundation.h>

    @interface Student : NSObject{//成员变量定义在大括号中

        int age;

        int no;

    }

    //age get 方法

    //-代表动态方法,+代表静态方法

    - (int)age;

    -(int)no;

    - (void)setAge:(int)newAge;

    -(void)setAge:(int)newAge andNo:(int)newNo;

    @end

      .m文件中得写法

    #import "Student.h"

    //@implementation代表实现类

    @implementation Student

    - (int)age{

        NSLog(@"调用了get");

        return age;

    }

    -(int)no{

        return no;

    }

    - (void)setAge:(int)newAge andNo:(int)newNo{

        NSLog(@"调用了set");

        age = newAge;

        no = newNo;

    }

    @end

     3.调用类

    /创建一个Student对象;

            //1.调用一个静态方法来分配内存

            Student *stu = [[Student alloc] init];

           

            //调用一个动态方法init初始化

            //stu = [stu init];

           // [stu setAge:100];

            //int age =  [stu age];

            //NSLog(@"%i",age);    

            [stu setAge:17 andNo:23];

            NSLog(@"%i  %i",[stu age],[stu no]);

    //释放对象

            [stu release];

  • 相关阅读:
    leetcode236
    leetcode139
    leetcode56
    leetcode19
    2018-5-22-SublimeText-粘贴图片保存到本地
    2019-1-29-Sublime-Text-安装中文、英文字体
    2019-1-29-Sublime-Text-安装中文、英文字体
    2018-8-15-WPF-插拔触摸设备触摸失效
    2018-8-15-WPF-插拔触摸设备触摸失效
    2019-10-18-dotnet-修复找不到-System.ServiceProcess-定义
  • 原文地址:https://www.cnblogs.com/hqr9313/p/3513432.html
Copyright © 2011-2022 走看看