# Write Python3 code here
class car():
# init method or constructor
def __init__(self, model, color):
self.model = model
self.color = color
def show(self):
print("Model is", self.model )
print("color is", self.color )
# both objects have different self which
# contain their attributes
audi = car("audi a4", "blue")
ferrari = car("ferrari 488", "green")
audi.show() # same output as car.show(audi)
ferrari.show() # same output as car.show(ferrari)
# Behind the scene, in every instance method
# call, python sends the instances also with
# that method call like car.show(audi)
Output:
Model is audi a4
color is blue
Model is ferrari 488
color is green
--------------------------------------------
#zoo.py
class Animal:
def greating(self, name):
print ("Hello.., I am %s"%name)
------------------
from "file zoo.py" import "类 Animal"
#call 类
pig = Animal()
#使用类的函数代入运算
pig.greating("Pig Torna")
>>>Hello.., I am Pig Torna
class Animal(object): # 父类, father class
def __init__(self, name):
self.name = name
def greet(self):
print ('Hello, I am %s.' % self.name)
class Dog(Animal): # 子类,inherited from fater class Animal
def greet(self):
print ('WangWang.., I am %s.' % self.name)
class Cat(Animal): # 子类,inherited from fater class Animal
def greet(self):
print ('MiaoMiao.., I am %s._.' % self.name)
def hello(“dog或cat”): # Function to call a class
dog或cat.greet() # call a method in class
--------------------------------------------
>>> hello(Dog("dog croos") # pass Name into 子类 child class
WangWang.., I am dog croos.
>>> hello(Cat("cat nancy"))
MiaoMiao.., I am cat nancy._.
menu_father.py
class meat():
def __init__(self, animal_name): # 定义变量到父类
self.name = animal_name
def meatdescription(self, meat_name, color):
print("The meat of %s is called %s, with color %s"% (self.name,
meat_name,
color))
menu_child.py
from lib.menu_father import meat
class specialMeat(meat):
def meatdescription(self, meat_name, color):
if self.name == "pig" or meat_name == "pork":
ans = input("Waring! as NO pork related should be ordered for Muslim."
"checked and continue?")
if ans == "y":
print("Please select other option")
else:
print("Error, please check again")
else:
print("The meat of %s is called %s, with color %s" % (self.name,
meat_name,
color))
runbook.py
from lib.menu_child import specialMeat
order1 = specialMeat("pigs")
order1.meatdescription("pork", "pink")
class JD():
def __init__(self, title, salary, bouns):
self.Title = title
self.TC = salary+"+"+bouns
def candidate(self, candi_name):
print ("Candidate %s got offered %s Position with TC %s"%(candi_name,self.Title,self.TC))
#>>> IT=JD("techlead","100k", "10%")
#>>> IT.candidate("WU")
#Candidate WU got offered techlead Position with TC 100k10%