欢迎光临散文网 会员登陆 & 注册

objective-c代码案例

2023-07-08 12:53 作者:自由的莱纳  | 我要投稿

以下是一个使用Objective-C编写的简单示例代码,用于展示如何创建一个简单的学生管理系统。该系统可以添加学生、显示学生列表以及按姓名进行搜索。请注意,这只是一个基本的示例,没有实际的数据存储或用户界面。 ```objective-c #import // 学生类 @interface Student : NSObject @property (nonatomic, strong) NSString *name; @property (nonatomic, strong) NSString *studentID; - (instancetype)initWithName:(NSString *)name studentID:(NSString *)studentID; @end @implementation Student - (instancetype)initWithName:(NSString *)name studentID:(NSString *)studentID {   self = [super init];   if (self) {     _name = name;     _studentID = studentID;   }   return self; } @end // 学生管理器类 @interface StudentManager : NSObject @property (nonatomic, strong) NSMutableArray *students; - (void)addStudent:(Student *)student; - (void)displayStudents; - (Student *)searchStudentByName:(NSString *)name; @end @implementation StudentManager - (instancetype)init {   self = [super init];   if (self) {     _students = [[NSMutableArray alloc] init];   }   return self; } - (void)addStudent:(Student *)student {   [self.students addObject:student]; } - (void)displayStudents {   for (Student *student in self.students) {     NSLog(@"Name: %@, ID: %@", student.name, student.studentID);   } } - (Student *)searchStudentByName:(NSString *)name {   for (Student *student in self.students) {     if ([student.name isEqualToString:name]) {       return student;     }   }   return nil; } @end // 主函数 int main(int argc, const char * argv[]) {   @autoreleasepool {     StudentManager *manager = [[StudentManager alloc] init];           // 添加学生     Student *student1 = [[Student alloc] initWithName:@"Alice" studentID:@"001"];     [manager addStudent:student1];           Student *student2 = [[Student alloc] initWithName:@"Bob" studentID:@"002"];     [manager addStudent:student2];           Student *student3 = [[Student alloc] initWithName:@"Charlie" studentID:@"003"];     [manager addStudent:student3];           // 显示学生列表     [manager displayStudents];           // 搜索学生     NSString *searchName = @"Bob";     Student *foundStudent = [manager searchStudentByName:searchName];     if (foundStudent) {       NSLog(@"Found student: Name: %@, ID: %@", foundStudent.name, foundStudent.studentID);     } else {       NSLog(@"Student with name %@ not found.", searchName);     }   }   return 0; } ``` 以上示例代码演示了如何创建学生类和学生管理器类。学生类具有姓名和学生ID属性,并且可以使用初始化方法进行初始化。学生管理器类具有一个学生数组,可以添加学生、显示学生列表以及按姓名搜索学生。在主函数中,我们创建了一个学生管理器对象,添加了几个学生,并演示了显示学生列表和按姓名搜索学生的功能。请注意,这只是一个简单的示例,实际的学生管理系统可能包含更多的功能和数据存储方式。 当然!以下是另一个Objective-C代码示例,用于演示如何使用Objective-C编写一个简单的图书馆管理系统。该系统可以添加图书、显示图书列表以及按标题进行搜索。 ```objective-c #import // 图书类 @interface Book : NSObject @property (nonatomic, strong) NSString *title; @property (nonatomic, strong) NSString *author; - (instancetype)initWithTitle:(NSString *)title author:(NSString *)author; @end @implementation Book - (instancetype)initWithTitle:(NSString *)title author:(NSString *)author {   self = [super init];   if (self) {     _title = title;     _author = author;   }   return self; } @end // 图书馆管理器类 @interface LibraryManager : NSObject @property (nonatomic, strong) NSMutableArray *books; - (void)addBook:(Book *)book; - (void)displayBooks; - (Book *)searchBookByTitle:(NSString *)title; @end @implementation LibraryManager - (instancetype)init {   self = [super init];   if (self) {     _books = [[NSMutableArray alloc] init];   }   return self; } - (void)addBook:(Book *)book {   [self.books addObject:book]; } - (void)displayBooks {   for (Book *book in self.books) {     NSLog(@"Title: %@, Author: %@", book.title, book.author);   } } - (Book *)searchBookByTitle:(NSString *)title {   for (Book *book in self.books) {     if ([book.title isEqualToString:title]) {       return book;     }   }   return nil; } @end // 主函数 int main(int argc, const char * argv[]) {   @autoreleasepool {     LibraryManager *libraryManager = [[LibraryManager alloc] init];           // 添加图书     Book *book1 = [[Book alloc] initWithTitle:@"Book 1" author:@"Author 1"];     [libraryManager addBook:book1];           Book *book2 = [[Book alloc] initWithTitle:@"Book 2" author:@"Author 2"];     [libraryManager addBook:book2];           Book *book3 = [[Book alloc] initWithTitle:@"Book 3" author:@"Author 3"];     [libraryManager addBook:book3];           // 显示图书列表     [libraryManager displayBooks];           // 搜索图书     NSString *searchTitle = @"Book 2";     Book *foundBook = [libraryManager searchBookByTitle:searchTitle];     if (foundBook) {       NSLog(@"Found book: Title: %@, Author: %@", foundBook.title, foundBook.author);     } else {       NSLog(@"Book with title %@ not found.", searchTitle);     }   }   return 0; } ``` 以上示例代码演示了如何创建图书类和图书馆管理器类。图书类具有标题和作者属性,并且可以使用初始化方法进行初始化。图书馆管理器类具有一个图书数组,可以添加图书、显示图书列表以及按标题搜索图书。在主函数中,我们创建了一个图书馆管理器对象,添加了几本图书,并演示了显示图书列表和按标题搜索图书的功能。 请注意,这只是一个简单的示例,实际的图书馆管理系统可能包含更多的功能和数据存储方式。此示例仅用于演示Objective-C语言的基本用法。

objective-c代码案例的评论 (共 条)

分享到微博请遵守国家法律