python-面向对象

1、如何创建类
class 类名:
pass

class bar:
    pass


2、创建方法
构造方法,__init__(self,arg)
obj = 类('a1')
普通方法
obj = 类(‘xxx’)
obj.普通方法名()

class bar:
    #构造方法
    def __init__(self,n):
        self.name = n
    #普通方法
    def foo(self):
        print(self.name)
#生成一个叫obj对象
obj=bar('noube')
#obj对象调用类里面人方法
obj.foo() 

#执行结果
noube

3、面向对象三大特性之一:封装

class Bar:
def __init__(self, n,a):
  self.name = n
  self.age = a
  self.xue = 'o'

b1 = Bar('alex', 123)

b2 = Bar('eric', 456)



4、适用场景:
如果多个函数中有一些相同参数时,转换成面向对象

class DataBaseHelper:

def __init__(self, ip, port, username, pwd):
  self.ip = ip
  self.port = port
  self.username = username
  self.pwd = pwd

def add(self,content):
  # 利用self中封装的用户名、密码等 链接数据
  print('content')
  # 关闭数据链接

def delete(self,content):
  # 利用self中封装的用户名、密码等 链接数据
  print('content')
  # 关闭数据链接

def update(self,content):
  # 利用self中封装的用户名、密码等 链接数据
  print('content')
  # 关闭数据链接

def get(self,content):
  # 利用self中封装的用户名、密码等 链接数据
  print('content')
  # 关闭数据链接

s1 = DataBaseHelper('1.1.1.1',3306, 'alex', 'sb')

 1 class DataBaseHelper:
 2     def __init__(self, ip,port, username, pwd):
 3         self.ip = ip
 4         self.port = port
 5         self.username = username
 6         self.pwd = pwd
 7 
 8     def add(self, content):
 9         # 利用self中封装的用户名、密码等 链接数据
10         print(content)
11         # 关闭数据链接
12 
13     def delete(self, content):
14         # 利用self中封装的用户名、密码等 链接数据
15         print(content)
16         # 关闭数据链接
17 
18     def update(self, content):
19         # 利用self中封装的用户名、密码等 链接数据
20         print(content)
21         # 关闭数据链接
22 
23     def get(self, content):
24         # 利用self中封装的用户名、密码等 链接数据
25         print(content)
26         # 关闭数据链接
27 
28 s1 = DataBaseHelper('1.1.1.1', 1521,'noube', 'sb')
29 s1.add('add')
30 s1.delete('del')
31 s1.update('update')
32 s1.get('get')
33 print(s1)
34 print(s1.ip,s1.port,s1.username)
35 
36 ############执行结果################
37 add
38 del
39 update
40 get
41 <__main__.DataBaseHelper object at 0x7f0173035da0>
42 1.1.1.1 1521 noube
封装

5、面向对象三大特性之二:继承

1、继承

class 父类:#基类
pass

class 子类(父类): #派生类(基类)
pass

2、重写

防止执行父类中的方法

3、self永远是执行该方法的调用者

4、方法中调用方法

  super(子类, self).父类中的方法(...)
  父类名.父类中的方法(self,...)

 1 #继承
 2 class grandfather():
 3     def __init__(self,name):
 4         self.name = name
 5         self.foo1()     #此处的self是obj
 6 
 7     def foo(self):
 8         print('grandfather foo')
 9     def foo3(self):
10         print('grandfather foo3')
11 
12 class father(grandfather):
13     def foo(self):
14         print('father foo')
15         grandfather.foo(self)   #用  父类.父类的方法(self) 的格式调用父类方法
16     def foo2(self):
17         print('father foo2')
18 
19 class son(father):
20     def foo(self):      #重写 子类中有父类中也有,则执行子类中的方法
21         print('son foo')
22         super(son,self).foo() #用super方法调用父类中的方法
23     def foo1(self):
24         print('son foo1')
25 
26 obj=son('noube')
27 obj.foo()
28 obj.foo2()
29 obj.foo3()
30 ############执行结果##############
31 son foo1
32 son foo
33 father foo
34 grandfather foo
35 father foo2
36 grandfather foo3
继承


5、Python中支持多继承

a. 左侧优先
b. 一条道走到黑
c. 同一个根时,根最后执行


6、面向对象三大特性之三:多态
====> python   原生多态

# Java
string v = 'alex'

def func(string arg):
print(arg)

func('alex')
func(123)

# Python
v = 'alex'

def func(arg):
print(arg)


func(1)
func('alex')



==================================================================

练习:

class Person:

def __init__(self,n,a,g,f):

self.name = n
self.age =a
self.gender =g
self.fight = f


role_list = []

y_n = input('是否创建角色?')
if y_n == 'y':
name = input('请输入名称:')
age = input('请输入名称:')
...
role_list.append(Person(....))

# role_list,1,2


=========================  ==============================


class Foo:

def __init__(self, name):
# 普通字段
self.name = name

# 普通方法
def show(self):
print(self.name)

obj = Foo('alex')
obj.name
obj.show()


类成员:
# 字段
- 普通字段,保存在对象中,执行只能通过对象访问
- 静态字段,保存在类中, 执行 可以通过对象访问 也可以通过类访问

# 方法
- 普通方法,保存在类中,由对象来调用,self=》对象
- 静态方法,保存在类中,由类直接调用
- 类方法,保存在类中,由类直接调用,cls=》当前类

######## 应用场景:
如果对象中需要保存一些值,执行某功能时,需要使用对象中的值 -> 普通方法
不需要任何对象中的值,静态方法


# 属性,特性
- 不伦不类



中国的所有省份,用面向对象知识表示?

class Province:
# 静态字段,属于类
country = '中国'


def __init__(self, name):
# 普通字段,属于对象
self.name = name

henan = Province('河南')
henan.name
henan.name = "河南南"


#hebei = Province('河北')

# Province.country

 1 class Province:
 2     # 静态字段,属于类
 3     country = '中国'
 4 
 5     def __init__(self, name):
 6         # 普通字段,属于对象
 7         self.name = name
 8 
 9 
10 henan = Province('河南')
11 hebei = Province('河北')
12 
13 print(Province.country)
14 # print(Province.name) # 类不可以直接调用普通字段   AttributeError: type object 'Province' has no attribute 'name'
15 
16 hebei = Province('河北')
17 hebei.name
18 print(hebei.country)
19 print(hebei.name)
20 print(hebei)
21 
22 ############执行结果###############
23 
24 中国
25 中国
26 河北
27 <__main__.Province object at 0x7fb47c4c4cf8>
普通字段,静态字段
class bar:
    def foo1(self):
        print('abc')
    @staticmethod
    def foo2(name,age):
        print(name,age)
    @classmethod
    def foo3(cls):
        print(cls)
    @property
    def foo4(self):
        print('property')
        return 1


obj=bar()
obj.foo1()
bar.foo1(obj)
bar.foo2('abc',12)
bar.foo3()
p=obj.foo4
print(p)
#############执行结果####################
abc
abc
abc 12
<class '__main__.bar'>
property
1
普通方法,静态方法,类方法

练习

输入页码,分页显示内容

 1 class pages():
 2     def __init__(self,p,count):
 3         try:
 4             p = int(p)
 5         except Exception as e:
 6             p = 1
 7         if p<=0 or p>count:
 8             p = 1
 9 
10         self.page = p
11 
12     # @property
13     def start(self):
14         value=(self.page-1)*10
15         return value
16     # @property
17     def end(self):
18         value=self.page*10
19         return value
20 
21 l = []
22 for i in range(1000):
23     l.append(i)
24 count = len(l)
25 print(count)
26 
27 while True:
28     n = input(' please input page:')
29     obj=pages(n,count)
30     print(l[obj.start():obj.end()])
分页显示

  

原文地址:https://www.cnblogs.com/noube/p/5893820.html