class Dog:
# Class Attribute 类属性
species = ‘mammal’
# Initializer / Instance Attributes 初始化实例属性
def __init__(self,name, age):
self.name = name
self.age= age
# Instantiate the Dog object 实例化
philo = Dog("Philo", 5)
mikey = Dog("Mikey", 6)
# Access the instance attributes 获得实例属性
print("{} is {} and {} is{}.".format(
philo.name, philo.age, mikey.name, mikey.age))
# Is Philo a mammal? 是哺乳动物吗?
if philo.species== "mammal":
print("{0} is a {1}!".format(philo.name, philo.species))
问题:•Createa Class called student, it should contain attributes: name,age.
创建一个叫做student的类,它需要有name和age两个属性
•Createan instance by your name and initialize the name and age:
创建一个实例,用你的名字和年龄初始化
•Example: Jackey = student(‘jackey’, 18)•Createa method called study and an attribute called learning, every time study is run, you switch learning between False and True.
创建一个叫做study的方法和一个叫做learning的属性,每次执行study,learning的值会在True和False之间转换。
•Createa new Class called graduated,it inherits from student class.
创建一个叫做graduated的类,从student中继承
•Createan instance by your name and initialize the name and age:
用你的名字实例化,并且初始化name和age
•Example: Jackey = graduated(‘jackey’, 22)••Addamethodcall birthday,every time you run birthday method, youchange you age. 增加一个叫做birthday的方法,每次执行后,你的age会加一
# Class Attribute 类属性
species = ‘mammal’
# Initializer / Instance Attributes 初始化实例属性
def __init__(self,name, age):
self.name = name
self.age= age
# Instantiate the Dog object 实例化
philo = Dog("Philo", 5)
mikey = Dog("Mikey", 6)
# Access the instance attributes 获得实例属性
print("{} is {} and {} is{}.".format(
philo.name, philo.age, mikey.name, mikey.age))
# Is Philo a mammal? 是哺乳动物吗?
if philo.species== "mammal":
print("{0} is a {1}!".format(philo.name, philo.species))
问题:•Createa Class called student, it should contain attributes: name,age.
创建一个叫做student的类,它需要有name和age两个属性
•Createan instance by your name and initialize the name and age:
创建一个实例,用你的名字和年龄初始化
•Example: Jackey = student(‘jackey’, 18)•Createa method called study and an attribute called learning, every time study is run, you switch learning between False and True.
创建一个叫做study的方法和一个叫做learning的属性,每次执行study,learning的值会在True和False之间转换。
•Createa new Class called graduated,it inherits from student class.
创建一个叫做graduated的类,从student中继承
•Createan instance by your name and initialize the name and age:
用你的名字实例化,并且初始化name和age
•Example: Jackey = graduated(‘jackey’, 22)••Addamethodcall birthday,every time you run birthday method, youchange you age. 增加一个叫做birthday的方法,每次执行后,你的age会加一