杨庆荣的博客

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

gnustep写一个类

点击 2107    创建时间 2012-10-30 22:34:25

<p> <strong>People.h</strong></p> <div class="code">

import &lt;Foundation/Foundation.h&gt;  <br />

@interface People : NSObject<br />
{<br />
    NSInteger age;<br />
    NSString *name;<br />
}<br />
-(NSString*)getName;<br />
-(void)setName:(NSString*)name2;<br />
-(void)setAge:(NSInteger)age2;<br />
-(NSInteger)getAge;<br />
@end</div>

<p> <strong>People.m</strong></p> <div class="code">

import "People.h"<br />

@implementation People<br />
<br />
- (NSString*) getName<br />
{<br />
   return name;<br />
}<br />
<br />
- (void) setName:(NSString*) name2<br />
{<br />
   name=name2;<br />
}<br />
-(void)setAge:(NSInteger)age2<br />
{<br />
   age =age2;<br />
}<br />
-(NSInteger)getAge<br />
{<br />
   return age;<br />
}<br />
@end</div>

<p> <strong>test.m</strong></p> <div class="code">

import &lt;Foundation/Foundation.h&gt;  <br />

#import "People.m"<br />
 <br />
int main(int arvc, char* argv[])  <br />
{  <br />
    NSString *domain = @"wudimei.com杨庆荣";<br />
    <br />
    NSLog(@"domain:%@ ,length:%d",[domain uppercaseString] , [domain length]);  <br />
    People *people= [[People alloc]init];//初始化自定义类People<br />
    NSString *name = [[NSString alloc]init];<br />
    name=@"yqr杨庆荣";<br />
    [people setName:name];<br />
    [people setAge:28];<br />
    NSLog(@"name:%@ , age: %d", [people getName] ,[people getAge]);<br />
    return 0;  <br />
}  <br />
<br />
//编译命令:gcc -o test test.m -I/GNUstep/System/Library/Headers/ -fconstant-string-class=NSConstantString -L/GNUstep/System/Library/Libraries -lobjc -lgnustep-base &amp;&amp; ./test.exe<br />
//文件存为utf-8格式<br />
 </div>

上一篇: android笔记:点击按钮显示文本
下一篇: gnustep:object-c继承