The study of Class and Instance in Python(20170918)
how to define a Class
class class_name(object):
pass
define a Class
class Person(object):
def __init__(self, name, gender):
self.name = name
self.gender = gender
def print_info(self):
print('%s: %s' % (self.name, self.gender))
def get_gender(self):
if self.gender == 'male':
return 'gay'
elif self.gender == 'female':
return 'LGBT'
else:
return 'Ladyboy'
person_1 = Person(‘yuhanyu’, ‘male’)
person_2 = Person(‘amu’, ‘female’)
person_3 = Person(‘Tailan’, ‘DoKnow’)
print(person_1.print_info())
print(person_2.print_info())
print(person_1.get_gender())
print(person_2.get_gender())
print(person_3.get_gender())
person_1.gender = ‘female’
print(person_1.get_gender())