Object_C类,属性,方法和指针

来源:互联网 发布:2017淘宝最新引流方法 编辑:程序博客网 时间:2024/04/30 01:01

2012年12.23

am:OC语法

一:类,属性,方法,对象

二:How to use Xcode creat OC class

三:How to use class(调用类中的方法)

一.类,属性,方法,对象

  类 class 具有相同特性和行为的一些事物抽象的总称。

类的声明和实现,在OC中一般用两个文件描述类:

1》.h:头文件 ,类的声明文件用于声明成员变量、方法。类的声明使用关键字@interface@end

注意:.h中的方法只是做一个声明,并不对方法进行实现。也就是说,只是说明一下方法名、方法的返回值类型、方法接收的参数类型而已,并不会编写方法内部的代码。

2.m:类的实现文件,用于实现.h中声明的方法。类的实现使用关键字@implementation@end

2.类的方法

1》方法的声明和实现,都必须以+或者 -开头

  • +表示方法(静态方法)+方法中不能对属性进行访问,不能赋值,不能使用属性。
  • -表示对象方法(动态方法)

2》在.h中声明的所有方法作用域都是public类型,不能更改

3.类的属性

4.类的对象

对象(instance object)  类的实例化,一个类的个

体。

二.how to use Xcode creat OC class

 如何新建一个People:

     右键main.m —》new file — iOS  Cocoa Touch —>Objective C class —>People subclass of NSObject

1》main.m中

#import <Foundation/Foundation.h>


//1 导入People类的头文件,.....

#import "People.h"


int main(int argc,const char * argv[])

{

 @autoreleasepool {      

        // insert code here...

       NSLog(@"Hello, World!");       

       NSLog(@"123");       

//        int a;

//        a = 1;       

//      2 创建对象

//      allocate 分配

//      在内存中分配一块空间,用来存放People类的一个对象,对象名是laosun,以后使用laosun对象名,指的就是People的对象。       

//      [类名  alloc];        

//       * 指针 laosun 变量名---- 对象名

//      类名 *对象名

//       [] 调用

        People *laosun = [Peoplealloc];      

//      类名 *对象名 = [类名 alloc];       

//      3  -eat  用对象调用  

//      [对象名 -方法名];

        [laosuneat];

        [laosuneat];     

//        People *laosun1 = [People alloc];

//        [laosun1 eat];     

//      [类名  +方法名];

        [Peopleeat];  

    }

   return 0;

}

2》People.h中

#import <Foundation/Foundation.h>


//interface 接口

//@interface 关键字作用:用来声明(定义)类的

//People 类名

// 继承  子类继承父类

// NSObject 父类 顶级父类 祖宗类 基类(base class


//声明一个类,类名为People,继承于基类NSObject

@interface People :NSObject

{

//  属性声明的区域   

//  声明一个年龄的属性  

//  .h文件中 只对属性进行声明,无法赋值。

   int age;//默认值是 0   

//  bool 布尔类型 YES/NO 1/0

//  声明一个布尔类型的变量sex,代表性别属性。

   bool sex;//NO  0    

//  身高 m

   float height;//0.0   

//  体重 kg

   float weight;//0.0    

//  总结 属性即实例变量(instance variable)   int float bool 基本数据类型    

}

//方法声明的区域

//C function

//void eat(){ }

//OC method

//- 方法的类型  实例方法(instance method) 将来要用对象 调用

//(返回值类型)  void 无返回值

//eat 方法名

// 语句结束 代表方法声明的结束

- (void)eat;

//类方法(class method) (静态方法) 将来用 调用

+ (void)eat;

//@end 关键字 类声明的结束  @interface对应

@end

3People.m

//"" 自己的类的头文件


//导入了People.h,意思就是将来在此文件中,如果使用People关键字,则代表的是People类。

#import "People.h"


//使用一个类,必须先声明。


//@implementation 实现

@implementation People


// 属性的赋值需要放在方法中完成

- (void)eat

{

//  ++ 运算符自身加1,再赋给自身

//  --

    age ++;//age = age + 1;

   sex = YES;

    height +=0.1;//height = height + 1;

    weight = weight + 1;

    

    

//  布尔类型也可以使用 %d占位符 输出

//  1代表YES 0代表NO

    NSLog(@"吃了一顿饭,age:%d sex:%d height:%f weight:%f",age,sex,height,weight);

    

}


+ (void)eat

{

    

//  类方法中不能访问实例变量

//    age = 20;

    NSLog(@"this is a class method");

}


//@end People实现的结束

@end

PM: 

1 >>方法

.带一个参数的方法

.带二个参数

main.m

#import "People.h"

#import <Foundation/Foundation.h>


int main(int argc,const char * argv[])

{


    @autoreleasepool {

        

        // insert code here...

        NSLog(@"Hello, World!");

        

       People *p = [Peoplealloc];

        

       float resultValue = [p getPerimeterForCircle:5];

       NSLog(@"resultValue --- %f",resultValue);

        

        

       People *p1 = [Peoplealloc];

       int resultValue1 = [p1 getChickenWithFirst:3 withSecond:6];

       NSLog(@"resultValue1 --- %d",resultValue1);

        

        

        

    }

   return 0;

}

》》People.h中   

#import <Foundation/Foundation.h>


@interface People :NSObject

{

   int age;

   bool sex;

   float weight;

   float height;

    

//    int ageForLaosun;

}

//- (void)eat;

//+ (void)eat;


//求圆的周长的方法 L = 2 * pi * r


//: 代表方法有参数而且:是方法名的一部分

//方法名 getPerimeterForCircle:

//float  参数类型

//radius 参数名


//带一个参数 有返回值的方法

- (float)getPerimeterForCircle:(float)radius;


//带两个参数的方法

//求两个数的乘积


//方法名 getChickenWithFirst:withSecond:

//带两个参数,方法名会被分割成两部分,在每个参数的前面都是方法名的组成成分。

- (int)getChickenWithFirst:(int)value1 withSecond:(int)value2;


//多个参数方法写法:

//-/+ (返回值类型)方法名部分1(参数1类型)参数1方法名部分2(参数2类型)参数2 ...



//练习:返回值类型为bool类型的方法

@end

》》People.m

#import "People.h"


@implementation People

- (float)getPerimeterForCircle:(float)radius

{

   float resultValue = 2 * 3.14 * radius;

   return resultValue;

}

- (int)getChickenWithFirst:(int)value1 withSecond:(int)value2

{

   return value1 * value2;

}

@en

2 数据类型 

int bool float char short long double Byte long long 

》》main.m

//

//  main.m

//  AboutTypeTest

//

//  Created by laosun on 14/12/23.

//  Copyright (c) 2014 zhiyou. All rights reserved.

//

#import <Foundation/Foundation.h>


int main(int argc,const char * argv[])

{


    @autoreleasepool {

        

        // insert code here...

        NSLog(@"Hello, World!");

        

        

//      @"字符串"

       char c = ' ';

       NSLog(@"ASC -- %d",c);

       NSLog(@"-----%c",c);

        

//      字符组成了字符串

        

//      OC   

       long long money =10000000000000;

        

//      %lld  %qu  long long类型的占位符

       NSLog(@"money -- %qu",money);        

    }

   return 0;

}

》》People.h中

//

//  People.h

//  AboutTypeTest

//

//  Created by laosun on 14/12/23.

//  Copyright (c) 2014 zhiyou. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface People :NSObject

{

    

//  Byte 整数 0 ~ 255

   int age;

   bool sex;

   float weight;

//  基本 数据类型

//  bool char(字符型) Byte(字节型)1个字节)

//  short(短整型) 2个字节)

//  int float(浮点型 单精度) (4个字节)

//  double(浮点型 双精度) long(长整型 32bit OS 4个字节,64bit OS 8个字节)  long longOC (8个字节)

   

//    character 字符

    

//  字节是计算机中最小的存储单位


//  1PB = 1024TB

//  1TB = 1024GB

//  1GB = 1024MB

//  1MB = 1024KB

//  1KB = 1024B

//  1B  = 8b  bit

//  一个字节 8个二进制数组成

    

//  0000 0000 ~ 1111 1111

// 无符号 unsigned

//  0 ~ 255

// 有符号 signed  负数

//  -128 ~ 127

//  -2^7  ~ 2^7 - 1

//    -1-128 总计 128 个数字

//    1127 总计 127 个数字

//    最后加上0

//    全部 256 个数字

//    

//    1个字节,也就是8byte,所能表示的最大信息量就是2^8,恰好也就是256    

//  int 4字节 32bit

//  -2^31 ~ 2^31 - 1 

//  类型决定范围,选择类型时,由你想表示的范围来决定使用什么类型。  

}

@end

》》People.m

#import "People.h"


@implementation People

@end

3 指针 pointer 

screenshot.png

NSString *name

》》main.m

//

//  main.m

//  AboutPointerTest

//

//  Created by laosun on 14/12/23.

//  Copyright (c) 2014 zhiyou. All rights reserved.

//


#import <Foundation/Foundation.h>

#import "People.h"


int main(int argc,const char * argv[])

{

    @autoreleasepool {

        // insert code here...

       NSLog(@"Hello, World!”);     

//      p 指针变量

//      * 代表用来定义指针

//      p指针保存People对象在内存中 首地址

       People *p = [Peoplealloc];

//        NSString *str;

//      %@ 用来输出对象

        NSLog(@"p指针保存的内存地址对应的对象为 %@",p);

//      <People: 0x1001051b0> 

//      %p 用来输出指针保存的内存地址

        NSLog(@"p指针保存的内存地址为 %p",p);

        [psetName];    

    }

   return 0;

}

>>People.h

#import <Foundation/Foundation.h>

@interface People :NSObject

{

//  基本数据类型

//    int bool float char。。。 

//  NSString 字符串  

//  alt + 单击 NSString  查看系统的API文档

//  API  Access Program Interface 访问程序接口

//  SDK  Software Development Kit

//  软件开发工具包  

//  类型

   NSString *name;

}

//- (void)eat;

- (void)setName;

@end

>>>People.m

#import "People.h"

@implementation People

- (void)setName

{

   name = @"老孙";

    NSLog(@"---->> %@",name);

}

@end


                              






0 0