# 班级每位同学自我介绍
# 1.字典遍历
# students={"name":"李柳","city":"北京"}
# for key in students:
# print(key)
# for value in students.values():
# print(value)
# for k,v in students.items():
# print(k)
# print(v)
# 2.建立类
# class Student():
# # 初始化属性 ==构造函数
# def __init__(self,name,city):
# self.name=name
# self.city=city
# print("My name is ",name,",My city is",city)
# print("My name is %s and conme from %s" %(name,city))
# def talk(self):
# print("hello,everyone!")
# # 实例化对象
# st1 = Student('李柳','北京')
# # 对象调用方法
# st1.talk()
# 3.类的继承与多态
# class object:
# def __init__(self,name):
# self.name = name
class Animal(object): #Animal类继承object类
def __init__(self,color):
self.color = color
def eat(self):
print("动物在吃食物!")
def run(self):
print("动物在赛跑")
class Cat(Animal): #继承Animal类
def eat(self):
print("猫在吃鱼!")
class Dog(Animal): #继承Animal类并定义自己的方法
def __init__(self,name,age,color):
super(Dog,self).__init__(color)
self.name=name
self.age=age
def eat(self):
print("狗在啃骨头!")
cat= Cat("黑色")
cat.eat()
dog = Dog("小黑",30,"黑色")
dog.eat()
dog.run()
print(dog.color)
animal=Animal("黑色")
animal.eat()
# 多态
def feed(obj):
obj.eat()
animal=Animal("黄色")
cat = Cat("蓝色")
dog=Dog("小黑",3,"黑色")
feed(cat)
feed(dog)
feed(animal)