#import <Foundation/Foundation.h>
@interface Person : NSObject
{
NSString *personName;
NSInteger personAge;
}
- (id)initWithName:(NSString *)name andAge:(NSInteger)age;
- (void)print;
@end
@implementation Person
- (id)initWithName:(NSString *)name andAge:(NSInteger)age{
if(self =[super init])
{
personName = name;
personAge = age;
}
return self;
}
- (void)print{
NSLog(@"Name: %@", personName);
NSLog(@"Age: %ld", personAge);
}
@end
@interface Employee : Person
{
NSString *employeeEducation;
}
- (id)initWithName:(NSString *)name andAge:(NSInteger)age
andEducation:(NSString *)education;
- (void)print;
@end
@implementation Employee
- (id)initWithName:(NSString *)name andAge:(NSInteger)age
andEducation: (NSString *)education
{
personName = name;
personAge = age;
employeeEducation = education;
return self;
}
- (void)print
{
NSLog(@"Name: %@", personName);
NSLog(@"Age: %ld", personAge);
NSLog(@"Education: %@", employeeEducation);
}
@end
int main(int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Base class Person Object");
Person *person = [[Person alloc]initWithName:@"yunshouhu" andAge:5];
[person print];
NSLog(@"Inherited Class Employee Object");
Employee *employee = [[Employee alloc]initWithName:@"wjh"
andAge:15 andEducation:@"MBA"];
[employee print];
[pool drain];
return 0;
}objective-c之继承
最新推荐文章于 2024-10-04 09:22:56 发布
本文通过Objective-C实现Person和Employee类的定义与继承,演示了初始化方法与打印个人信息的过程。

3万+

被折叠的 条评论
为什么被折叠?



