Mark Pilkington . , , Objective-C, Java.
Objective-C
@interface Parent : NSObject
+ (int)foo;
+ (int)bar;
- (void)printMyFoo;
@end
@interface Child : Parent
+ (int)bar;
@end
@implementation Parent
+ (int)foo {
return [self bar];
}
+ (int)bar {
return 0;
}
- (void)printMyFoo {
NSLog(@"%d", [[self class] foo]);
}
@end
@implementation Child
+ (int)bar {
return 1;
}
@end
, printMyFoo , , +[Parent foo] bar :
id parent = [[Parent alloc] init];
id child = [[Child alloc] init];
[parent printMyFoo];
[child printMyFoo];
Java
class Parent {
static int foo() { return bar(); }
static int bar() { return 0; }
void printMyFoo() { System.out.println(foo()); }
}
class Child extends Parent {
static int bar() { return 1; }
}
, printMyFoo() " ", , Parent.foo() Parent.bar() Child.bar():
Parent parent = new Parent();
Child child = new Child();
parent.printMyFoo();
child.printMyFoo();