完善版 #import <Foundation/Foundation.h> @interface Score : NSObject { @public int CScore; int OCScore; int IOSScore; } - (int) compareCScoreWithOther:(Score *) score; - (int) compareSumScoreWithOther:(Score *)score; @end
@implementation Score - (int) compareCScoreWithOther:(Score *) score { return CScore - score->CScore; } - (int) compareSumScoreWithOther:(Score *)score { int sum1 = CScore + OCScore + IOSScore; int sum2 = score->CScore + score->OCScore + score->IOSScore; return sum1 - sum2; } @end
@interface Student : NSObject { @public char* name; char* number; Score* score; } - (int) compareCScoreWithOther:(Student*) student; - (int) compareSumScoreWithOther:(Student*) student; @end
@implementation Student - (int) compareCScoreWithOther:(Student*) student { return [score compareCScoreWithOther:student->score]; } - (int) compareSumScoreWithOther:(Student*) student { return [score compareSumScoreWithOther:student->score]; } @end
int main() { Student * stu1 = [Student new]; Student * stu2 = [Student new]; Score * score1 = [Score new]; Score * score2 = [Score new];
score1->CScore = 100; score2->CScore = 200; stu1->score = score1; stu2->score = score2; //stu1->score->CScore = 100;//第二层是结构体可以,如果是对象,好像只能间接赋值,如上 //stu2->score->CScore = 200; int a = [stu1 compareCScoreWithOther:stu2]; int b = [stu1 compareSumScoreWithOther:stu2]; NSLog(@"%d",a); return 0; }
|