定义类并执行类中的方法:
class 类名:
    def 方法名(self,arg):
        print(arg)
中间人 = 类名()
中间人.方法名(1)
 
1 如何创建类
    class 类名:
        pass
2 创建方法
    构造方法   __init__
        obj = 类('a1')
    普通方法
        obj = 类('xxx')
        obj.普通方法名()
3 面向对象的三大特性之一:封装
class Bar:
    def __init__(self,n,a,g):
        self.name = n
        self.age = a
        self.gender = g
        self.blood = 'o'
 
    def foo(self, name, age, gender):
        print(name, age, gender)
4 适用场景 :
    如果多个函数中有一些相同参数时,转换成面向对象
 
    class Bar:
        def __init__(self,n,a):
            self.name = n
            self.age = a
            self.xue = 'o'
    b1 = Bar('alex',123)
    b2 = Bar('eric',456)
 
class Bar:
    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')
 
 
5 面向对象三大特性之二:继承
    1 继承
        class 父类:
            pass
        class 子类(父类):
            pass
    2 重写
        防止执行父类中的方法
    3 self永远是执行该方法的调用者
    4
        super(子类,self).父类中的方法(...)
        父类名.父类中的方法(self,...)
 
    5 Python中支持多继承
        a. 左侧优先
        b. 一条道走到黑
        c. 同一个根时,根最后执行
6 面向对象三大特性之三:多态
  =======>>>>  Python 原生多态
    '''class foo:
        pass
    java里声明func函数,函数只接受foo类或者foo类子类的对象
    def func(foo arg):
        print(arg)
 
    obj = foo()
    obj = Son()
    func(obj)
    '''
 
 
 
 
===========================================================
 
 
 
练习:
    class Person:
        def__init__(self,n,a,g,f):
            self.name = n
            self.age = a
            self.gender = g
            self.fight = f
    y_n = input('是否创建角色?')
    if y_n == 'y':
        name = input('请输入名称')
        age = input('请输入年纪')
        ...
        role_list.append(Person(...))
========================面向对象中高级====================================
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 = 'China'
    def__init__(self,name,country):
        self.name = name
 
 
henan = Province('河南')
hebei = Province('河北')
 
Province.country
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
posted on 2021-01-23 19:06  VincentAdamNemessis  阅读(74)  评论(0编辑  收藏  举报