def read_peopledb(): connect() from database import connection global rowsp, people people = [] cursor_people = connection.cursor() cursor_people.execute("SELECT first_name, last_name, age FROM person") rowsp = cursor_people.fetchall() for row in rowsp: person = Person(row[0], row[1], row[2]) people.append(person) cursor_people.close() connection.close()
#coding=utf-8 ''' Created on 2016年7月5日 @author: kongwentao ''' from class_person import Person swaroop = Person('Swaroop') swaroop.selfPop() swaroop.personPop() Person.population += 5 print swaroop.population,'------------' # swaroop.population += 10 Person.population += 20 print swaroop.population print Person.population swaroop.ni = 10 print 'swaroop.ni:',swaroop.ni print '--------------------------------------' kalam = Person('Abdul Kalam') kalam.sayHi() kalam.selfPop() kalam.personPop() kalam.population += 1 print kalam.population
def __init__(self, name, age, stress, work): Person.__init__(self, name, age, stress) self.work = work
def giveraise(self, percent, bouns=0.1): #定制类 # self.pay = self.pay * (1.0 + percent + bouns) Person.giveraise(self, percent + bouns) #推荐使用这种方法,可以完美继承
def __init__(self, name, age, pay): Person.__init__(self, name, age, pay, 'manager')
from class_person import Person # zline = Person('Lin Zhang',30,15000,'network') # # print(zline.name,zline.pay) #验证导入的类 class Manager(Person): #继承类 def __init__(self, name, age, pay): Person.__init__(self, name, age, pay, 'manager') def giveraise(self, percent, bouns=0.1): #定制类 # self.pay = self.pay * (1.0 + percent + bouns) Person.giveraise(self, percent + bouns) #推荐使用这种方法,可以完美继承 # self.giveraise(percent + bouns) #写法与上面一样 # self.pay *= (1.0 + percent + bouns) if __name__ == '__main__': tom = Manager(name='Tom Doe', age=50, pay=50000) #基于映射关系传入数据 bob = Person('Bob Smith', 42, 30000, 'networker') #基于位置传入数据 print(tom.getlastname()) print(bob.getlastname()) tom.giveraise(0.1) print(tom.pay) bob.giveraise(0.1) print(bob.pay) # tom.giveraise(.20) # print(tom.pay)
from class_person import Person per_list = [] c = 0 while c < 5: name, age = input('Please enter the details [name,age]:').split(',') age = int(age) if name.upper() == 'CHAITANYA': chait = Person(name, age) else: pr = Person(name, age) per_list.append(pr) c += 1 for p in per_list: #print(p) if p.age < chait.age: diff = (chait.age - p.age) print(f'{p.name} is younger than {chait.name} by {diff} years') if p.age > chait.age: diff2 = (p.age - chait.age) print(f'{p.name} is older than {chait.name} by {diff2} years') if p.age == chait.age: print(f'{p.name} and {chait.name} are of same age')
#!/usr/bin/python3 # -*- coding=utf-8 -*- from class_person import Person from class_manger import Manager bob = Person('Bob Smith', 42, 30000, 'networker') sue = Person('Sue Jones', 45, 20000, 'hardware') tom = Manager(name='Tom Doe',age=50,pay=50000) db = [bob,sue,tom] for obj in db: obj.giveraise(.20) ##所有的人都加 20%的工资 for obj in db: print(obj.getlastname(),'=>',obj.pay)