OC第三课

来源:互联网 发布:me352ll a支持什么网络 编辑:程序博客网 时间:2024/05/22 09:04

方法的声明与事项,都是必须以+或者-开头

+表示类方法(静态方法)

-表示对象方法(动态方法)

声明所有的变量和方法都是在.h头文件中,都是public类型


变量的作用域:

@public  全局都可以访问

@protected 只能在类内部和子类中被访问

@private 只能在类内部被访问

变量的定义必须在类的{ }中


默认是protected;


类的对象  自动释放机制

Student *stu=[[[Student alloc] initWithAge:15 andNo:2] autorelease];跟下面的效果一样,只是内部自动释放

利用系统的方法创建对象,都是自动释放内存。


Student *stu=[[Studnet alloc] initWithAge:15 andNo:2];

[stu release];

如果方法直接写在.m文件中,就代表是私有方法。

//谁调用方法,self就指向谁

- 静态方法   对象

+动态方法   类


Student *stu=[[Student alloc] init];    相当于Student * stu=[Studnet new];   //调用默认的构造方法

但是调用自己重新写的构造方法就不行,必须

Student *sut=[[Studnet alloc]initWithage:100];

原创粉丝点击