杨庆荣的博客

工作之余接单:公司产品展示网站建设、电商网站建设、小程序、app开发。+86 13714715608

gnustep:object-c继承

点击 27461    创建时间 2012-10-30 22:46:27

People.h

 #import <Foundation/Foundation.h>   
 @interface People : NSObject  
 {  
    NSInteger age;  
    NSString *name;  
 }  
 -(NSString*)getName;  
 -(void)setName:(NSString*)name2;  
 -(void)setAge:(NSInteger)age2;  
 -(NSInteger)getAge;  
 @end

People.m

 @implementation People: NSObject  

  - (NSString*) getName  
  {  
      return name;  
  }  

  - (void) setName:(NSString*) name2  
  {  
      name=name2;  
  }  
  -(void)setAge:(NSInteger)age2  
  {  
      age =age2;  
  }  
  -(NSInteger)getAge  
  {  
      return age;  
  }  
 @end``` ### Teacher.h

```objc 
 #import <Foundation/Foundation.h>   
 @interface Teacher : People  
 {  
    NSString *major;  
 }  
 -(NSString*)getMajor;  
 -(void)setMajor:(NSString*)major2;  
 @end

Teacher.m

 #import "Teacher.h"  
 @implementation Teacher:People  
  -(NSString*)getMajor  
  {  
      return major;  
  }  
  -(void)setMajor:(NSString*)major2  
  {  
      major=major2;  
  }  
 @end

test.m

 #import <Foundation/Foundation.h>   
 #import "People.m"  
 #import "Teacher.m"  

 int main(int arvc, char* argv[])   
 {   
    NSString *domain = @"wudimei.com杨庆荣";  

    NSLog(@"domain:%@ ,length:%d",[domain uppercaseString] , [domain length]);  

    People *people= [[People alloc]init];//初始化自定义类People  
    NSString *name = [[NSString alloc]init];  
    name=@"yqr杨庆荣";  
    [people setName:name];  
    [people setAge:28];  
    NSLog(@"name:%@ , age: %d", [people getName] ,[people getAge]);  

    Teacher *teacher = [[Teacher alloc]init]; //Teacher类继承自People类  
    NSString *major = [[NSString alloc]initWithString:@"math"];  
    [teacher setMajor:major];  
    [teacher setAge:29];  
    NSLog(@"name:%@ , age: %d", [teacher getMajor] ,[teacher getAge]);  
    return 0;   
 }   

输出结果是:

 rong@rong-PC ~ $ gcc -o test test.m -I/GNUstep/System/Library/Headers/ -fconstant-string-class =NSConstantString -L/GNUstep/System/Library/Libraries -lobjc -lgnustep-base &&  
 ./test.exe  
 2012-10-30 22:42:14.158 test[4604] autorelease called without pool for object (0  
 x5dac68) of class GSUnicodeBufferString in thread <NSThread: 0x59b458>  
 2012-10-30 22:42:14.189 test[4604] domain:WUDIMEI.COM鏉ㄥ簡鑽?,length:14  
 2012-10-30 22:42:14.205 test[4604] name:yqr鏉ㄥ簡鑽?, age: 28  
 2012-10-30 22:42:14.220 test[4604] name:math , age: 29 

上一篇: gnustep写一个类
下一篇: php5.4.8 连接mysql比php5.2.6要慢