Object-Oriented Programming(1.2)

Bob Lee
3 min readDec 14, 2020
class Animal():
def __init__(self, name, aggr, hp):
self.name = name
self.aggr = aggr
self.hp = hp

def eat_pill(self):
print("%s eat pill to restore 100 hp"%self.name)
self.hp += 100


class Dog(Animal):
# "self" is initiated object of dog,rather than animal's
def __init__(self, name, aggr, hp, kind):
Animal.__init__(self, name, aggr, hp) # self is refer to dog
self.kind = kind # derived attribute

def bite(self, person):
person.hp -= self.aggr
print()


class Person(Animal):
def __init__(self, name, aggr, hp, sex, money):
Animal.__init__(self, name, aggr, hp)
self.sex = sex # derived attribute
self.money = money # derived attribute

def attach(self, dog): # derived method
dog.hp -= self.aggr

def get_weapon(self, weapon):
if self.money >= weapon.price:
self.money -= weapon.price
self.weapon = weapon
self.aggr += weapon.aggr
else:
print("Input Money")


class Weapon(object):
def __init__(self, name, aggr, njd, price):
self.name = name
self.aggr = aggr
self.njd = njd
self.price = price

def Big(self, person):
if self.njd > 0:
person.hp -= self.aggr * 2
self.njd -= 1
else:
print("You cannot use Big")


jin = Dog("Jin", 10, 500, "Teddy")
print(jin.name)

jin.eat_pill() # Dog don't have "eat_pill" method
print(jin.hp) # add blood

alex = Person("Alex", 100, 200, "M", "100")
alex.eat_pill()
print(alex.hp)


jin.bite(alex)
print(alex.hp)

General Introduction:

We have Three Class above: Animal, Person, Dog. Both Person and Dog have common attribute (hp, Attr & name) and common method (eat_pill), So we put all of them in the Animal.

Apart from common attribute, Person have unique attributes (Derived Attribute) such as “Gender”, “Money” and personal methods such as “attack”, “get_weapon”, while Dog have unique attributes (Derived Attribute) such as “kind” and personal methods such as “bite”.

This sample is aim for finding Derived Attribute/Method in inherit of OOP

Sample of Derived Attribute/Method

In the Dog Class, “Animal.__init__(self, name, aggr, hp)” was used to inherit attribute from Animal.

  1. the self after “__init__” is initiated variable of Dog, rather than Animal. We should rely on parent class to assign value of “name”, “aggr” and “hp”.
  2. the self after “Animal.__init__” is initiated variable of Dog, which can assign the input value when use Dog() ( Ex: Dog(“JIN”, 100, 1000, “Teddy”))
  3. self.kind can be seen as Derived Attribute, which is owned by Dog itself. Because dog may have different unique kinds. Based on the common attribute “name”, “aggr”, “hp”, “kind” can be seen as derived attribute which derived from Dog Class.
  4. It’s important to know that “eat_pill” is shown in “Dog” as well as “Animal”. According to OOP(1.1), The child class will priority execute function of itself, and, if such function doesn’t existed in child class, go to parent class to find function for executing.
  5. “Animal.eat_pill(self)” is used for calling common attribute of effect after eating pill for all of Animal( Both Person and Dog). Apart from adding hp, Dog itself can derive two teeth (Derived Method)
Test Case
  1. Call Dog class and print dog’s name
  2. There is eat_pill method in Dog class so it was priority executed in Dog class
  3. This is no eat_pill method in Person class, so alex was inherited this method from parent class
  4. This four chunk is aim for checking hp changes
Result of Test Case
  1. dog’s name is “Jin”. The name attribute will be inherited from parents attribute
  2. 600 is hp changes after Jin eat pill and “2” is teeth number (Derived attribute)
  3. 300 is hp changes after Alex eat pill, there’s no Derived attribute

Conclusion:

# 父类中没有的属性在子类出现: 派生属性
# 父类中没有的方法在子类出现,派生方法
# 只要子类对象调用子类中有的名字,优先用子类。如果子类没有,报错

--

--