super()。method()和self.method()之间有什么区别?

时间:2018-06-02 16:29:42

标签: python python-3.x

使用self.method()self.method()之间有什么区别,当我们从父类继承某些东西时,为什么要使用其中一种而不是另一种呢?

我唯一想到的是,使用静态方法,显然不可能调用super()。至于其他一切,我无法提出使用- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self; [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) { // Enable or disable features based on authorization. NSUserDefaults *storage = [NSUserDefaults standardUserDefaults]; if(granted == YES){ [storage setBool:YES forKey:@"permission granted"]; [storage setBool:YES forKey:@"alert permission granted"]; [storage setBool:YES forKey:@"sound permission granted"]; }else{ NSLog(@"No permission granted"); [storage setBool:NO forKey:@"permission granted"]; }; }]; } #pragma mark UNNotificationCenter setup UNNotificationAction *acceptAction = [UNNotificationAction actionWithIdentifier:@"ACCEPT_IDENTIFIER" title:NSLocalizedString(@"Continue notifications", nil) options:UNNotificationActionOptionAuthenticationRequired]; UNNotificationAction *declineAction = [UNNotificationAction actionWithIdentifier:@"DECLINE_IDENTIFIER" title:NSLocalizedString(@"Stop notifications", nil) options:UNNotificationActionOptionAuthenticationRequired]; UNNotificationAction *doNotDisturbAction = [UNNotificationAction actionWithIdentifier:@"DO_NOT_DISTURB_IDENTIFIER" title:NSLocalizedString(@"Start Do Not Disturb", nil) options:UNNotificationActionOptionAuthenticationRequired]; NSArray *actions = [NSArray arrayWithObjects:acceptAction, declineAction, doNotDisturbAction, nil]; // NSArray *intentIdentifiers = [NSArray arrayWithObjects:@"none", nil]; UNNotificationCategory *invite = [UNNotificationCategory categoryWithIdentifier:@"com.nelsoncapes.localNotification" actions:actions intentIdentifiers: @[] options:UNNotificationCategoryOptionNone]; NSSet *categories = [NSSet setWithObjects:invite, nil]; [center setNotificationCategories:categories]; [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) { // Enable or disable features based on authorization. }]; #pragma mark UNNotification received in background - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler{ [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { NSLog(@"notification settings were changed"); NSUserDefaults *storage = [NSUserDefaults standardUserDefaults]; [storage setBool:YES forKey:KEventLoggerEventNotificationSettingsChanged]; [storage synchronize]; if (settings.authorizationStatus != UNAuthorizationStatusAuthorized) { // Notifications not allowed NSLog(@"notification settings were changed"); item.eventDescription = KEventLoggerEventNotificationsNotAllowed; // check settings for alert and sound } // UNNotificationSetting alertSetting = settings.alertSetting; if(settings.alertSetting == UNNotificationSettingEnabled){ [storage setBool:YES forKey:@"alert permission granted"]; item.eventDescription = KEventLoggerEventAlertsAreAllowed; }else{[storage setBool:NO forKey:@"alert permission granted"]; item.eventDescription = KEventLoggerEventAlertsAreNotAllowed; } if (settings.soundSetting == UNNotificationSettingEnabled){ [storage setBool:YES forKey:@"sound permission granted"]; item.eventDescription = KEventLoggerEventSoundsAreAllowed; }else {[storage setBool:NO forKey:@"sound permission granted"]; item.eventDescription = KEventLoggerEventSoundsAreNotAllowed; } }]; NSLog(@"appdelegate - center didReceiveNotificationResponse"); UNNotification *notification = response.notification; if([actionIdentifier isEqual:@"com.apple.UNNotificationDefaultActionIdentifier"] || [actionIdentifier isEqual:@"com.apple.UNNotificationDismissActionIdentifier"]){ }else{ BOOL accept = [actionIdentifier isEqual:@"ACCEPT_IDENTIFIER"]; BOOL stop = [actionIdentifier isEqual:@"DECLINE_IDENTIFIER"]; BOOL doNotDisturb = [actionIdentifier isEqual:@"DO_NOT_DISTURB_IDENTIFIER"]; if (accept){NSLog(@"accept"); [self handleAcceptActionWithNotification:notification]; } else if (stop){NSLog(@"stop"); [self handleDeclineActionWithNotification:notification]; } else if(doNotDisturb) {NSLog(@"do not disturb"); [self handleDoNotDisturbActionWithNotification:notification]; }; } 的理由。

当某人选择一个而不是另一个问题时,是否会有人提出一个虚拟的例子并解释原因,或者这只是常规事情?

2 个答案:

答案 0 :(得分:7)

-(UIViewController *) getVisibleViewContoller { UIViewController *rootViewController = UIApplication.sharedApplication.keyWindow.rootViewController; if (!rootViewController) { return nil; } if ([rootViewController isKindOfClass:[UITabBarController class]]) { UITabBarController *tabbarVC = (UITabBarController *) rootViewController; UIViewController *selectedVC = tabbarVC.selectedViewController; if (selectedVC) { if (![selectedVC isKindOfClass:[UINavigationController class]]) { return selectedVC; } rootViewController = selectedVC; } } if ([rootViewController isKindOfClass:[UINavigationController class]]) { UINavigationController *navigationVC = (UINavigationController *) rootViewController; if (navigationVC.topViewController) { return navigationVC.topViewController; } return navigationVC.viewControllers.lastObject; } return rootViewController; } 将调用super().method()的父类实现,即使该子项已定义了自己的实现。您可以阅读documentation for super以获得更深入的解释。

method

对于像class Parent: def foo(self): print("Parent implementation") class Child(Parent): def foo(self): print("Child implementation") def parent(self): super().foo() def child(self): self.foo() c = Child() c.parent() # Parent implementation c.child() # Child implementation 这样的单数继承类,Child与更明确的super().foo()相同。如果是多重继承,Parent.foo(self)将根据Method Resolution Order, or MRO确定要使用的super定义。

另一个激励性的例子:如果我们继承foo并编写Child的另一个实现,则调用哪个方法?

foo

答案 1 :(得分:2)

  

<强>自

self ,主要用作类的实例方法的第一个参数,始终表示类的调用对象/实例。

  

<强>超级()

super()是指父类的对象。它在方法覆盖的情况下很有用,这适用于包括C ++,Java等在内的众多编程语言。在Java中, super()用于调用父类的构造函数。

请看下面的小例子。

class TopClass(object):
    def __init__(self, name, age):
        self.name = name;
        self.age = age;

    def print_details(self):
        print("Details:-")
        print("Name: ", self.name)
        print("Age: ", self.age)
        self.method()

    def method(self):
        print("Inside method of TopClass")

class BottomClass(TopClass):
    def method(self):
        print("Inside method of BottomClass")    

    def self_caller(self):
         self.method()

    def super_caller(self):
         parent = super()
         print(parent)
         parent.method()

child = BottomClass ("Ryan Holding", 26)
child.print_details()

"""
Details:-
Name:  Ryan Holding
Age:  26
Inside method of BottomClass
"""

parent = TopClass("Rishikesh Agrawani", 26)
parent.print_details()

"""
Details:-
Name:  Rishikesh Agrawani
Age:  26
Inside method of TopClass
"""

child.self_caller()
child.super_caller()

"""
Inside method of BottomClass
<super: <class 'BottomClass'>, <BottomClass object>>
Inside method of TopClass
"""