Python 7 Levels, L5, Unit 54, Object Oriented Advanced

Powered By EmbedPress

Exercise 1:

class Card():
    def __init__(self,num,password,ban):
        self.num = num
        self.password = password
        self.__ban = ban

    def getBan(self,num,password):
        if num == self.num and password == self.password:
            return self.__ban
        else:
            print("Wrong password")


card = Card("1001","123456",500)
print(card._Card__ban)
#print(card.getBan("1001","123456"))

Exercise 2:

class Animal(object):#inherite from object
    def __init__(self,color):
        self.color = color

    def sing(self):
        print("Animal is singing")

class Cat(Animal):
    pass

class Dog(Animal):
    def __init__(self,name,age,color):
        #Animal.__init__(self,color)#call init of its parent class
        super(Dog,self).__init__(color)
        self.name = name
        self.age = age

    def sing(self):
        print("Wof wof singing")
        #Animal.sing(self)
        super().sing()

dog = Dog("little black",3,"black")
dog.sing()

Exercise 3:

class Animal(object):
    def __init__(self, name):
        self.name = name
    def sing(self):
        print("Animal is singing")

class Dog(Animal):
    def sing(self):
        print("Wof wof singing")

class Cat(Animal):
    def sing(self):
        print("Mew mew singing")

def testSing(obj):
    obj.sing()

#a = Animal("little black")
a = Dog("little black")
testSing(a)

dahan1999

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts