Python implementation of the abstract class Hackerrank Day 13: Abstract Classes

Defined in an abstract class abstract method, without implementing features 
subclass inherits an abstract class, but must define abstract methods specific functions

Hackerrank Example Code

from abc import ABCMeta, abstractmethod
class Book(object, metaclass=ABCMeta):  #########
    def __init__(self,title,author):
        self.title=title
        self.author=author   
    @abstractmethod #########
    def display(): pass #########

#Write MyBook class
class MyBook(Book):
    def __init__(self,title,author,price):
        super().__init__(title,author) ########
        self.price=price
    def display(self): #######
        print('Title: '+self.title+'\nAuthor: '+self.author+'\nPrice: '+ str(self.price))


title=input()
author=input()
price=int(input())
new_novel=MyBook(title,author,price)
new_novel.display()

Sample Input

The following input from stdin is handled by the locked stub code in your editor:

The Alchemist
Paulo Coelho
248

Sample Output

The following output is printed by your display() method:

Title: The Alchemist
Author: Paulo Coelho
Price: 248
Published 128 original articles · won praise 90 · views 4870

Guess you like

Origin blog.csdn.net/weixin_45405128/article/details/103928628