objectクラスの作成とクラスの使用
クラス構文は重要です.
student.h
student.m
感慨:コードはできるだけコピーしたり貼り付けたりしないでください.さもないと手書きで間違います
student.h
//
// Student.h
// stringTest
//
// Created by wen jing on 12-8-5.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Student : NSObject
{
int no;
int age;
NSString *name;
}
@property int no;
@property int age;
@property NSString *name;
- (void) aetstudent:(NSString*)namee;
@end
student.m
//
// Student.m
// stringTest
//
// Created by wen jing on 12-8-5.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "Student.h"
@implementation Student
@synthesize no;
@synthesize age;
@synthesize name;
- (void) aetstudent:(NSString *)namee{
name = namee;
}
@end
//
// Print.h
// stringTest
//
// Created by wen jing on 12-8-5.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Print : NSObject//Objective-c NSObject
{
NSString *caption;
NSString *photographer;
}
// Objective-C 2.0 , get/set
@property (nonatomic, copy) NSString *caption;
@property (nonatomic, copy) NSString *photographer;
// Objective-C 2.0 , get/set
+ (NSString*)printName;
@end
//
// Print.m
// stringTest
//
// Created by wen jing on 12-8-5.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "Print.h"
@implementation Print
@synthesize photographer;
@synthesize caption;
//
+ (NSString*)printName
{
return (@"Print Result");
}
@end
//
// main.m
// stringTest
//
// Created by wen jing on 12-8-4.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Print.h"
#import "Student.h"
int main (int argc, const char * argv[])
{
@autoreleasepool {
//
NSLog(@"ClassName = \"%@\"", [Print printName]);
//
Print* p = [[Print alloc] init];
//
[p setCaption:@"MyCaption"];
[p setPhotographer:@"MyPhotographer"];
//
NSLog(@"Caption = \"%@\"", [p caption]);
NSLog(@"Photographer = \"%@\"", [p photographer]);
Student *student=[[Student alloc] init];
[student setNo:2];
[student setName:@"MrJing"];
NSLog(@"student no=%i",[student no]);
NSLog(@"student name=%@",[student name]);
[student aetstudent:@"Mrwang"];
NSLog(@"student name=%@",[student name]);
[student release];
[p release];
/*
log:
2012-08-05 01:59:25.775 stringTest[1088:903] ClassName = "Print Result"
2012-08-05 01:59:25.788 stringTest[1088:903] Caption = "MyCaption"
2012-08-05 01:59:25.793 stringTest[1088:903] Photographer = "MyPhotographer"
2012-08-05 01:59:25.798 stringTest[1088:903] student no=2
2012-08-05 01:59:25.801 stringTest[1088:903] student name=MrJing
2012-08-05 01:59:25.805 stringTest[1088:903] student name=Mrwang
*/
}
return 0;
}
感慨:コードはできるだけコピーしたり貼り付けたりしないでください.さもないと手書きで間違います