Python之路,day7-Python基础

os.system 输出命令结果到屏幕,返回命令执行状态
os.popen("dir").read()#会保存命令的执行结果输出

py2.7
commands
commands.getstatusoutput("dir")
#windows不好用
#linux可以

subprocess
==================================
面向对象

模板造对象

一.类的特性

1.封装
防止数据被随意修改
使外部程序不需要关注对象内部的构造(逻辑结构),只需要通过此对象对外提供的接口进行直接访问即可
 1 class Dog(object):
 2 
 3     def __init__(self,name,food):#构造函数,构造方法,==初始化方法
 4         self.NAME = name
 5         self.FOOD = food
 6         self.__getj = 'big'
 7         self.__test = '123'
 8     def sayhi(self):#类的方法(类的具体方法)
 9         print("hello,ni shuo de dui.",self.NAME)
10     def get_getj(self):
11         return self.__getj
12     def eat(self):
13         print("%s is eating %s"%(self.NAME,self.FOOD))
14     def __del__(self):
15         print("del...run....")
16 
17 
18 d = Dog("a1",'haochide')#Dog(d,"a1") #实例化后产生的对象叫实例(当前类的实例)
19 d2 = Dog("a2",'pi')
20 
21 d.sayhi()
22 d2.sayhi()
23 d.eat()
24 d2.eat()
25 print(d.get_getj())
26 
27 print(d._Dog__test)
28 
29 def shot2():
30     print("buhuia")
31 
32 d.sayhi = shot2
33 d.sayhi()
34 # print(d.test)

 


2.继承
通过父类---》子类的方式 实现不同角色的共同点和不同点 以最小代码量的方式实现

类 ----》实例化----》实例对象

__init__构造函数

self.name = name #属性,成员变量,字段

def sayhi() #方法,动态属性

公有属性
在类里定义的属性,任何对象都可以访问

私有属性
__private_attr_name = value

def get_heart(self) #对外部提供只读访问接口
return self.__heart

r1._Role__heart 强制访问私有属性


析构方法

继承

多继承
 1 class Person(object):
 2     def __init__(self,name,age):
 3         self.name = name
 4         self.age = age
 5         self.sex = 'male'
 6 
 7     def talk(self):
 8         print("person is talk",self.name)
 9 
10 
11 class BlackPerson(Person):
12     def __init__(self,name,age):
13         Person.__init__(self,name,age)
14         print(self.sex)
15 
16     def walk(self):
17         print("is walk")
18 
19 
20 
21 b = BlackPerson('liusijia',10)
22 b.talk()

 



新式类vs经典类
SchoolMember.__init__(self,name,age,sex) #经典类写法
super(Teacher,self).__init__(name,age,sex)#新式类写法


class Person(object):
super(Person)

class Person:#经典类
ParentClass.__init__
 1 class SchoolMember(object):
 2     '''学校成员基类'''
 3     member = 0
 4     def __init__(self,name,age,sex):
 5         self.name = name
 6         self.age = age
 7         self.sex = sex
 8         self.enroll()
 9     def enroll(self):
10         '''注册'''
11         print("just encolled a new school member %s"%self.name)
12         SchoolMember.member +=1
13 
14     def tell(self):
15         '''个人信息'''
16         print('------info:%s-----'%self.name)
17         for k,v in self.__dict__.items():
18             print('\t',k,v)
19     def __del__(self):
20         print("开除[%s]..."%self.name)
21         SchoolMember.member -=1
22 
23 class School(object):
24     '''学校类'''
25     def open_branch(self,addr):
26         print("openning branch in %s"%addr)
27         self.addr = ""
28 
29 class Teacher(SchoolMember,School):
30     '''讲师类'''
31     def __init__(self,name,age,sex,salary,course):
32         SchoolMember.__init__(self,name,age,sex)   #经典类写法
33         super(Teacher,self).__init__(name,age,sex)#新式类写法
34 
35         self.salary = salary
36         self.course = course
37         # self.enroll()
38 
39     def teaching(self):
40         print("Teacher[%s]is teaching[%s]"%(self.name,self.course))
41 
42 class Student(SchoolMember):
43     def __init__(self,name,age,sex,course,tuition):
44         SchoolMember.__init__(self,name,age,sex)
45         self.course = course
46         self.tuition = tuition
47         self.amount = 0
48 
49     def pay_tuition(self,amount):
50         print("student[%s]has just paied [%s]"%(self.name,amount))
51         self.amount += amount
52 
53 t1 = Teacher("a1",11,"F*M",3000,"python")
54 s1 = Student("a2",14,"NA","py",3000)
55 s2 = Student("a3",13,"NA","py",3000)
56 
57 print(SchoolMember.member)
58 
59 
60 del s1
61 print(SchoolMember.member)
62 
63 t1.tell()
64 s2.tell()

 


3.多态
静态方法

类方法

属性方法

posted on 2016-11-26 09:20  灵魂与梦想  阅读(210)  评论(0编辑  收藏  举报

导航