0
Objective-C的新增功能,並具有此分配功能,可以使用類/方法在兩個不同課程中輸出學生的值。使用靜態變量來對輸出中的類進行求和,Objective-C
這是很容易的部分,我需要幫助的部分是如何聲明靜態變量並使用此變量將所有學生的值相加並輸出值。
我見過各種各樣的例子和線程,我真的想試試這個,但我無法弄清楚如何將這個概念應用到這個問題上。這很容易用另一種語言做,但不與ObjC一樣...
的代碼如下:
//
// main.m
// yadda yadda
//
// Created by yadda yadda on 8/26/14.
// Display the number of students in two different courses.
//
// Copyright (c) 2014 yadda.self. All rights reserved.
//
#import <Foundation/Foundation.h>
//---- @interface section ----
@interface Students: NSObject
-(void) setCourseOne: (int) a;
-(void) setCourseTwo: (int) b;
-(int) courseone;
-(int) coursetwo;
@end
//---- @implementation section ----
@implementation Students
{
int courseone;
int coursetwo;
}
-(void) setCourseOne:(int) a
{
courseone = a;
}
-(void) setCourseTwo:(int) b
{
coursetwo = b;
}
-(int) courseone
{
return courseone;
}
-(int) coursetwo
{
return coursetwo;
}
@end
//---- Program section ----
int main(int argc, const char * argv[])
{
@autoreleasepool {
Students *myStudents = [[Students alloc] init];
[myStudents setCourseOne: 8];
[myStudents setCourseTwo: 24];
NSLog(@"There are %i students in this class", [myStudents courseone]);
NSLog(@"There are %i students in this class", [myStudents coursetwo]);
//Third output statement for sum of all students
NSLog(@"There are %i students in all of the classes", [myStudents courseone]);
}
return 0;
}
偉大的這回答了問題的一部分,但我在哪裏申報的變量?在接口部分還是外部使其成爲全局? - 如果你願意的話,也請包括評論,更多的信息更好誠實地 – user3015009 2014-09-01 05:10:36
'int total'是聲明,'= ...'是它的初始化。這聽起來像你需要閱讀各種變量,聲明等。 – CRD 2014-09-01 05:52:53
這不是我的意思,但沒關係,我會弄清楚哈哈。感謝您的幫助。 – user3015009 2014-09-01 06:18:13