黑马程序员技术交流社区
标题:
面试题不会做 怎么办
[打印本页]
作者:
wukai
时间:
2015-3-21 23:41
标题:
面试题不会做 怎么办
10、 定义一个学生类,需要有姓名,年龄,考试成绩三个成员属性,创建5个对象,属性可以任意值。(Objective-C)
1) 不使用@property,手动编写他们的访问器方法(getter和setter),注意内存管理(手动管理内存)
2) 增加一个便利构造器(快速构造器)
3) 使用NSLog输出学生对象时,输出信息格式为:My Name Is XXX Age Is XXX Score Is XXX
4) 对5个学生对象按照成绩—》年龄—》姓名优先级排序(成绩相同按照年龄排序,成绩年龄相同按照姓名排序(5个学生的属性值自己随便设定,姓名不考虑中文,按26个大小字母排序))
作者:
bibabo88
时间:
2015-3-22 01:39
好高深的题。
作者:
hero200521296bj
时间:
2015-3-22 07:01
这个题的难度确实不小
作者:
qianlongwuyong
时间:
2015-3-22 07:07
加入基础班
作者:
2119391569
时间:
2015-3-22 07:43
看到这个题,光题目就有点晕啊!
作者:
完美世界
时间:
2015-3-22 14:58
这个题目我在入学考试做过。
#import <Foundation/Foundation.h>
@interface Student : NSObject
{
NSString *_name; // 姓名
int _age; // 年龄
double _score; // 考试成绩
}
// _name的setter和getter
- (void)setName:(NSString *)name;
- (NSString *)name;
// _age的setter和getter
- (void)setAge:(int)age;
- (int)age;
// _score的setter和getter
- (void)setScore:(double)score;
- (double)score;
// 不带参数的快速构造器
+ (Student *)student;
// 带参数的快速构造器
+ (Student *)studentWithName:(NSString *)name andAge:(int)age andScore:(double)score;
// 与其他Student对象比较大小的方法
- (NSInteger)comperWithOtherStudent:(Student *)stu;
@end
复制代码
#import "Student.h"
@implementation Student
// _name的setter和getter
- (void)setName:(NSString *)name
{
if (name != _name)
{
[_name release];
_name = [name retain];
}
}
- (NSString *)name
{
return _name;
}
// _age的setter和getter
- (void)setAge:(int)age
{
_age = age;
}
- (int)age
{
return _age;
}
// _score的setter和getter
- (void)setScore:(double)score
{
_score = score;
}
- (double)score
{
return _score;
}
- (void)dealloc
{
[_name release];
NSLog(@"姓名是:%@的Student对象被销毁。", _name);
[super dealloc];
}
// 不带参数的快速构造器
+ (Student *)student
{
return [[[self alloc] init] autorelease];
}
// 带参数的快速构造器
+ (Student *)studentWithName:(NSString *)name andAge:(int)age andScore:(double)score
{
Student *stu = [Student student];
stu.name = name;
stu.score = score;
stu.age = age;
return stu;
}
// 重写description方法
- (NSString *)description
{
return [NSString stringWithFormat:@"My Name Is %@,Age Is %d Score Is %f", _name, _age, _score];
}
// 与其他Student对象比较大小的方法(升序)
- (NSInteger)comperWithOtherStudent:(Student *)stu
{
// 比较分数大小
if (self.score > stu.score)
{
return NSOrderedDescending;
}
else if (self.score < stu.score)
{
return NSOrderedAscending;
}
else if (self.score == stu.score)
{
// 如果分数相同比较年龄大小
if (self.age > stu.age)
{
return NSOrderedDescending;
}
else if (self.age < stu.age)
{
return NSOrderedAscending;
}
else if (self.age == stu.age)
{
// 如果分数与年龄都相同比较名字的首字母
if ([self.name characterAtIndex:0] > [stu.name characterAtIndex:0])
{
return NSOrderedDescending;
}
else if ([self.name characterAtIndex:0] < [stu.name characterAtIndex:0])
{
return NSOrderedAscending;
}
else
{
return NSOrderedSame;
}
}
}
return -1;
}
@end
复制代码
#import <Foundation/Foundation.h>
#import "Student.h"
int main(int argc, const char * argv[])
{
// 创建一个自动释放池
@autoreleasepool
{
// 创建5个学生对象
Student *stu1 = [Student studentWithName:@"jack" andAge:30 andScore:50];
Student *stu2 = [Student studentWithName:@"lose" andAge:19 andScore:70];
Student *stu3 = [Student studentWithName:@"anna" andAge:25 andScore:90];
Student *stu4 = [Student studentWithName:@"fan" andAge:15 andScore:30];
Student *stu5 = [Student studentWithName:@"tom" andAge:15 andScore:85];
// 输出学生对象
NSLog(@"%@", stu1);
// 将5个学生对象添加到一个数组中
NSArray *students = [NSArray arrayWithObjects:stu1, stu2, stu3, stu4, stu5, nil];
// 对数组进行排序
NSArray *array = [students sortedArrayUsingSelector:@selector(comperWithOtherStudent:)];
// 输出排序后的数组
NSLog(@"%@", array);
}
return 0;
}
复制代码
作者:
hero200521296bj
时间:
2015-3-22 20:32
面试除了这个题,还有啥题啊
作者:
静好
时间:
2015-3-23 12:42
在一个程序里实现这些内容么,是都不能使用@property么
作者:
雨漪
时间:
2015-3-23 13:16
别被题目吓到了,其实很基础啊
作者:
chock江
时间:
2015-3-23 13:26
这道题目不好做的原因,你学完了基础视频还要附加了解下排序方法,基础视频并没有讲
作者:
futaoheima
时间:
2015-3-23 17:06
幸亏进了基础班,真不会
作者:
锵锵来跳鱼
时间:
2015-3-23 19:27
楼主面试几分啊,出结果了么
作者:
童冀
时间:
2015-3-23 19:39
雨漪 发表于 2015-3-23 13:16
别被题目吓到了,其实很基础啊
很基础....
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2