iOS——OC——对OC中实例变量的可见度和方法的见解
一、实例变量的可见度 (-实例对象) 安全性:
可见度
|
特点
|
public(公有的) | 实例变量可以在类的外部和内部操作 |
protected(受保护的,默认的) | 实例变量只能在该类和其子类内操作 |
private(私有的)
|
实例对象只能在该类内访问 |
注意:在oc里面,所有的实例对象默认的可见度是受保护的,protected指的是本类和子类;
- 具有继承关系的两个类,子类可以拥有父类的一切(包括他所有的实例变量和方法),除了父类中的私有变量和私有方法。
- 实例变量指的是实例对象的成员变量
- 子类可以继承父类所有的方法
二、方法:
OC中的方法分两种:类方法和实例方法。
类方法:只能类使用,例如:+(id)alloc
注:类方法中不能使用实例变量 只能类名调用
便利构造器是类方法!
实例方法:只能对象使用,
注:实例方法可以实例方法调用要用关键字self,self代表的是类的实例(对象),在类方法中,self代表的是类本身。
例如:
-(void)sayHi
带参数方法的声明:
-/+(返回值类型)方法名:(参数类型)第一个参数名 (中间要有空格)第二个参数形容词:(参数类型)第二个参数名(形参名不要与成员变量名重复)
-(void)replaceObjectAtIndex:(NSUInteger) index withObject:(id)anObject
实现(方法调用):[对象 方法名];
类方法:+
三、类的组合使用
-(id)init 这个最简单的自定义初始化方法只能给实例变量设置默认值,不灵活。 -(id)initWithName: 自定义初始化方法,根据需求定义参数初始化值。
初始化方法的特点 :1. - ,2. instancetype /id 3. 一定以init开头;
实现初始化方法: } self = [super init]; 为了保证默认初始值完整
return self ;}
-(id)init 这个最简单的自定义初始化方法只能给实例变量设置默认值,不灵活。 -(id)initWithName: 自定义初始化方法,根据需求定义参数初始化值。
初始化方法的特点 :1. - ,2. instancetype /id 3. 一定以init开头;
实现初始化方法: } self = [super init]; 为了保证默认初始值完整
return self ;}
所有的的实例对象只初始化一次,只销毁一次。
例(1)
类接口:
#import <Foundation/Foundation.h>
@interface Squar : NSObject
{
float length;
float wide1;
}
-(float)area;
-(float)perimeter;
-(void)sendLength:(float)input1 sendWide :(float)input2;
@interface Squar : NSObject
{
float length;
float wide1;
}
-(float)area;
-(float)perimeter;
-(void)sendLength:(float)input1 sendWide :(float)input2;
@end
类实现:
#import "Squar.h"
@implementation Squar
-(float)area
{ float s = length * wide1;
return s;
}
-(float)perimeter
{ float c = 2*(length+wide1);
return c;
}
-(void)sendLength:(float)input1 sendWide:(float)input2
{ length = input1;
wide1 = input2;
}
@implementation Squar
-(float)area
{ float s = length * wide1;
return s;
}
-(float)perimeter
{ float c = 2*(length+wide1);
return c;
}
-(void)sendLength:(float)input1 sendWide:(float)input2
{ length = input1;
wide1 = input2;
}
@end
主程序:
#import <Foundation/Foundation.h>
#import "Squar.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Squar *squar = [[Squar alloc]init];
[squar sendLength:10.2 sendWide:5.2];
NSLog(@"矩形的面积是:%.2f",[squar area]);
NSLog(@"矩形的周长是:%.2f",[squar perimeter]);
}
return 0;
#import "Squar.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Squar *squar = [[Squar alloc]init];
[squar sendLength:10.2 sendWide:5.2];
NSLog(@"矩形的面积是:%.2f",[squar area]);
NSLog(@"矩形的周长是:%.2f",[squar perimeter]);
}
return 0;
}
运行结果:
2015-10-20 11:24:05.689 10.20 课堂练习[1412:48391] 矩形的面积是:53.04
2015-10-20 11:24:05.690 10.20 课堂练习[1412:48391] 矩形的周长是:30.80
例(2)
类接口:
#import <Foundation/Foundation.h>
@interface Student : NSObject
{
NSString *name;
NSString *sex;
int age;
int number;
NSString *classname;
NSString *teachername;
NSString *subject;
}
-(void)setname:(NSString*)inputname setsex:(NSString*)inputsex setage:(int)inputage setnumber:(int)inputnumber setclassname:(NSString*)inputclassname setteachername:(NSString*)inputteachername seasubject:(NSString*)inputsubject;
-(void)study;
-(void)dohomework;
-(void)question;
-(void)answer;
-(void)selfintroduction;
@interface Student : NSObject
{
NSString *name;
NSString *sex;
int age;
int number;
NSString *classname;
NSString *teachername;
NSString *subject;
}
-(void)setname:(NSString*)inputname setsex:(NSString*)inputsex setage:(int)inputage setnumber:(int)inputnumber setclassname:(NSString*)inputclassname setteachername:(NSString*)inputteachername seasubject:(NSString*)inputsubject;
-(void)study;
-(void)dohomework;
-(void)question;
-(void)answer;
-(void)selfintroduction;
@end
类实现:
#import "Student.h"
@implementation Student
-(void)setname:(NSString*)inputname setsex:(NSString*)inputsex setage:(int)inputage setnumber:(int)inputnumber setclassname:(NSString*)inputclassname setteachername:(NSString*)inputteachername seasubject:(NSString*)inputsubject
{ name = inputname;
sex = inputsex;
age = inputage;
number = inputnumber;
classname = inputclassname;
teachername = inputteachername;
subject = inputsubject;
}
-(void)study
{
NSLog(@"我在教室学习!");
}
-(void)dohomework
{
NSLog(@"我在家写作业!");
}
-(void)question
{
NSLog(@"我忘了那几个作业题了!");
}
-(void)answer
{
NSLog(@"同学告诉我是数学第9页1.2.3.5题");
}
-(void)selfintroduction
{
NSLog(@"我叫:%@,是一个%@生,今年%d岁了,我在%@班,任课老师是%@,我正在学习%@!",name,sex,age,classname,teachername,subject);
}
@implementation Student
-(void)setname:(NSString*)inputname setsex:(NSString*)inputsex setage:(int)inputage setnumber:(int)inputnumber setclassname:(NSString*)inputclassname setteachername:(NSString*)inputteachername seasubject:(NSString*)inputsubject
{ name = inputname;
sex = inputsex;
age = inputage;
number = inputnumber;
classname = inputclassname;
teachername = inputteachername;
subject = inputsubject;
}
-(void)study
{
NSLog(@"我在教室学习!");
}
-(void)dohomework
{
NSLog(@"我在家写作业!");
}
-(void)question
{
NSLog(@"我忘了那几个作业题了!");
}
-(void)answer
{
NSLog(@"同学告诉我是数学第9页1.2.3.5题");
}
-(void)selfintroduction
{
NSLog(@"我叫:%@,是一个%@生,今年%d岁了,我在%@班,任课老师是%@,我正在学习%@!",name,sex,age,classname,teachername,subject);
}
@end
主程序:
#import <Foundation/Foundation.h>
#import "Student.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Student *student = [[Student alloc]init];
#import "Student.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Student *student = [[Student alloc]init];
[student setname:@"张三" setsex:@"男" setage:20 setnumber:2015114 setclassname:@"一(三)" setteachername:@"杨老师" seasubject:@"数学"];
[student study];
[student dohomework];
[student question];
[student answer];
[student selfintroduction];
}
return 0;
[student dohomework];
[student question];
[student answer];
[student selfintroduction];
}
return 0;
}
运行结果:
2015-10-20 11:28:50.108 10.20.2[1474:50933] 我在教室学习!
2015-10-20 11:28:50.109 10.20.2[1474:50933] 我在家写作业!
2015-10-20 11:28:50.110 10.20.2[1474:50933] 我忘了那几个作业题了!
2015-10-20 11:28:50.110 10.20.2[1474:50933] 同学告诉我是数学第9页1.2.3.5题
2015-10-20 11:28:50.109 10.20.2[1474:50933] 我在家写作业!
2015-10-20 11:28:50.110 10.20.2[1474:50933] 我忘了那几个作业题了!
2015-10-20 11:28:50.110 10.20.2[1474:50933] 同学告诉我是数学第9页1.2.3.5题
2015-10-20 11:28:50.110 10.20.2[1474:50933] 我叫:张三,是一个男生,今年20岁了,我在一(三)班,任课老师是杨老师,我正在学习数学!
感谢您的访问!
若对您有帮助或有兴趣请关注博客:http://www.cnblogs.com/Rong-Shengcom/