OC语法 子类与父类
来源:互联网 发布:中国网络什么时候墙的 编辑:程序博客网 时间:2024/04/30 21:45
2014年08月10日15:54:35
1,子类拥有父类所有的东西,因此可以在子类里面直接使用父类声明的实体变量或方法而无需再次声明。
2,子类如果重写了父类里面的方法则子类的对象在调用时会调用子类里的,而不再是父类里的方法。
3,父类的对象不能调用子类的方法。
4,父类的指针可以指向子类的对象,这个指针调用方法时将调用子类的方法(如果重写了的话)而不是父类里的方法,而子类的指针却不可以指向父类的对象。
下面为例子:(Human.h/m和Studen.h/m未上传)其中Student:Human
//// main.m// Test//// Created by apple on 14-8-10.// Copyright (c) 2014年. All rights reserved.//#import <Foundation/Foundation.h>#import "Human.h"#import "Student.h"int main(int argc, const char * argv[]) { @autoreleasepool { Human *human = [[Human alloc]init]; Student *student = [[Student alloc]init]; Human *h1 = student; [student print]; [student setAge:20]; [student setHeigt:168]; NSLog(@"height= %f,age = %d,h1:age=%d",[student Height],[student Age],[h1 Age]); [human print]; NSLog(@"height= %f,age = %d",[human Height],[human Age]); } return 0;}输出如下:
2014-08-10 15:49:35.249 Test[29651:303] this is Student class:
2014-08-10 15:49:35.450 Test[29651:303] obtain height:use the method of Student!
2014-08-10 15:49:35.452 Test[29651:303] obtain age:use the method of Student!
2014-08-10 15:49:35.452 Test[29651:303] obtain age:use the method of Student!
2014-08-10 15:49:35.453 Test[29651:303] height= 168.000000,age = 20,h1:age=20
2014-08-10 15:49:35.453 Test[29651:303] this is Human class:
2014-08-10 15:49:35.454 Test[29651:303] obtain height:use the method of Human!
2014-08-10 15:49:35.455 Test[29651:303] obtain age:use the method of Human!
2014-08-10 15:49:35.455 Test[29651:303] height= 0.000000,age = 0
Program ended with exit code: 0
可以看出即使student对象对height和age设定了值,但NSLog(@"height= %f,age = %d",[human Height],[human Age]);
输出2014-08-10 15:49:35.454 Test[29651:303] obtain height:use the method of Human!
2014-08-10 15:49:35.455 Test[29651:303] obtain age:use the method of Human!
2014-08-10 15:49:35.455 Test[29651:303] height= 0.000000,age = 0
- OC语法 子类与父类
- JQuer选择父类元素与子类元素的语法
- OC中的父类和子类
- 子类与父类,
- 父类与子类
- 父类与子类
- 子类继承父类的语法规则
- oc是子类调用父类的方法
- OC中子类和父类的转化问题
- OC 中,子类覆盖父类属性 详解
- oc 子类调用父类的私有方法
- OC中继承子类对象调用方法机制 子类对象访问父类中的实例变量
- OC中继承子类对象调用方法机制 子类对象访问父类中的实例变量
- 继承 父类与子类
- 父类与子类2
- 5.1 子类与父类
- OC简介与基础语法
- Swift与OC语法对比
- Java编程思想
- UVA - 10574 Counting Rectangles
- swift代码生成navigationController
- 8/10
- 基于Golang的分布式服务端开发日志(1)
- OC语法 子类与父类
- 时序图
- iOS_20_微博自定义可动画切换的导航控制器
- C++中 模板Template的使用 (typename)
- 南阳 477(浮点型的精度问题)
- 从社区管理战壕中挖出的4条经验
- 【黑马程序员】Java异常处理
- Android:当焦点在EditText上时自动显示软件键盘
- 图像识别