最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

objective-c代碼案例

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

以下是一個使用Objective-C編寫的簡單示例代碼,用于展示如何創(chuàng)建一個簡單的學(xué)生管理系統(tǒng)。該系統(tǒng)可以添加學(xué)生、顯示學(xué)生列表以及按姓名進(jìn)行搜索。請注意,這只是一個基本的示例,沒有實際的數(shù)據(jù)存儲或用戶界面。 ```objective-c #import // 學(xué)生類 @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 // 學(xué)生管理器類 @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 // 主函數(shù) int main(int argc, const char * argv[]) { ??@autoreleasepool { ????StudentManager *manager = [[StudentManager alloc] init]; ????? ????// 添加學(xué)生 ????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]; ????? ????// 顯示學(xué)生列表 ????[manager displayStudents]; ????? ????// 搜索學(xué)生 ????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; } ``` 以上示例代碼演示了如何創(chuàng)建學(xué)生類和學(xué)生管理器類。學(xué)生類具有姓名和學(xué)生ID屬性,并且可以使用初始化方法進(jìn)行初始化。學(xué)生管理器類具有一個學(xué)生數(shù)組,可以添加學(xué)生、顯示學(xué)生列表以及按姓名搜索學(xué)生。在主函數(shù)中,我們創(chuàng)建了一個學(xué)生管理器對象,添加了幾個學(xué)生,并演示了顯示學(xué)生列表和按姓名搜索學(xué)生的功能。請注意,這只是一個簡單的示例,實際的學(xué)生管理系統(tǒng)可能包含更多的功能和數(shù)據(jù)存儲方式。 當(dāng)然!以下是另一個Objective-C代碼示例,用于演示如何使用Objective-C編寫一個簡單的圖書館管理系統(tǒng)。該系統(tǒng)可以添加圖書、顯示圖書列表以及按標(biāo)題進(jìn)行搜索。 ```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 // 主函數(shù) 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; } ``` 以上示例代碼演示了如何創(chuàng)建圖書類和圖書館管理器類。圖書類具有標(biāo)題和作者屬性,并且可以使用初始化方法進(jìn)行初始化。圖書館管理器類具有一個圖書數(shù)組,可以添加圖書、顯示圖書列表以及按標(biāo)題搜索圖書。在主函數(shù)中,我們創(chuàng)建了一個圖書館管理器對象,添加了幾本圖書,并演示了顯示圖書列表和按標(biāo)題搜索圖書的功能。 請注意,這只是一個簡單的示例,實際的圖書館管理系統(tǒng)可能包含更多的功能和數(shù)據(jù)存儲方式。此示例僅用于演示Objective-C語言的基本用法。

objective-c代碼案例的評論 (共 條)

分享到微博請遵守國家法律
广东省| 比如县| 全州县| 霍城县| 虎林市| 雅江县| 家居| 裕民县| 天峻县| 全州县| 肥西县| 武陟县| 卓尼县| 陆良县| 靖江市| 大宁县| 阿城市| 遵义县| 天镇县| 盈江县| 保定市| 合山市| 衡南县| 隆回县| 都兰县| 通渭县| 皮山县| 梅州市| 镇沅| 丽水市| 弋阳县| 中山市| 密山市| 兴隆县| 墨江| 永仁县| 张家川| 凤冈县| 高淳县| 山阴县| 黄骅市|