zoukankan      html  css  js  c++  java
  • python:面向对象初级

    面向对象编程
    类的概念 : 具有相同属性和技能的一类事物
        人类 抽象
    对象 : 就是对一个类的具体的描述
        具体的人 具体

    使用面向对象的好处:
    使得代码之间的角色关系更加明确
    增强了代码的可扩展性
    规范了对象的属性和技能
    面向对象的特点:结局的不确定性

        self = {'name': name, 'sex':sex, 'hp': hp, 'ad': ad}
        def attack(dog): # 闭包
            # 人攻击狗
            print('%s攻击%s' % (self['name'], dog['name']))
            # 狗掉血,狗的血量-人的攻击力
            dog['hp'] -= self['ad']
        self['attack'] = attack
        return self
    
    def Dog(name,kind,hp,ad):
        # 狗模子
        self = {'name': name, 'kind':kind, 'hp': hp, 'ad': ad}
        def bite(person):
            print('%s咬了%s' % (self['name'], person['name']))
            # 人掉血,人的血量-狗的攻击力
            person['hp'] -= self['ad']
            if person['hp'] <= 0: print('game over,%s win' % self['name'])
        def bite2():pass
        self['bite'] = bite
        self['bite2
    

      

    创建一个类

    class Person:
        role = 'person'   # 静态属性
        def f1(self):     # 动态属性 方法(函数)  默认带一个参数self
    

      

    查看静态变量的第一种方式

    print(Person.__dict__)   # 内置的双下方法
    print(Person.__dict__['role'])
    Person.__dict__['role'] = 456  # 报错
    print(Person.__dict__['role'])
    

      

    查看静态变量的第二种方式

    print(Person.静态变量)   # 123 值
    print(Person.role)
    Person.静态变量 = 456
    Person.role = 456 print(Person.静态变量)#456 del Person.静态变量 print(Person.__dict__)

      

    类名

        引用静态变量
    1.类名.__dict__['静态变量名'] 可以查看,但是不能删改
    2.类名.静态变量名 直接就可以访问,可以删改
    删除一个静态变量 del 类名.静态变量名
    引用动态变量
    1.类名.方法名 查看这个方法的内存地址
    1.类名.方法名(实参) 调用了这个方法,必须传一个实参,这个实参传给了self
    创造一个对象 - 实例化
    产生一个实例(对象)的过程
    对象 = 类名()
    alex = Person() # 创造一个对象
    alex 是对象、实例
    Person是类
    对象 = 类名()
    class Person:
        role = 'person'   # 静态属性
        def __init__(self,name,sex,hp,ad):
            self.name = name     # 对象属性 属性
            self.sex = sex
            self.hp = hp
            self.ad = ad
        def attack(self):
            print('%s发起了一次攻击'%self.name)
    
    alex = Person('a_sb','不详',1,5)
    boss_jin = Person('金老板','女',20,50)
    
    alex.attack()      # Person.attack(alex)
    boss_jin.attack()  # Person.attack(boss_jin)
    

     

    实例化 :创造一个对象的过程:实例化

    实例化的过程:
    1.创造一个实例,将会作为一个实际参数 # python
    2.自动触发一个__init__的方法,并且把实例以参数的形式传递给__init__方法中的self形参
    3.执行完__init__方法之后,会将self自动返回给alex
    __init__方法 :初始化方法,给一个对象添加一些基础属性的方法,一般情况下是针对self的赋值
    对象
    在类的内部 self是本类的一个对象
    在类的外部,每一个对象都对应着一个名字,这个对象指向一个对象的内存空间
    属性的调用:
    对象名.属性名 第一种调用方法
    对象名.__dict__['属性名'] 第二种调用方法
    方法的调用 :
    类名.方法名(对象名) # 那么方法中的self参数就指向这个对象
    对象名.方法名() # 这样写 相当于 方法中的self参数直接指向这个对象







     面向对象:交互

    class Person:
        role = 'person'   # 静态属性
        def __init__(self,name,sex,hp,ad):
            self.name = name     # 对象属性 属性
            self.sex = sex
            self.hp = hp         #血量
            self.ad = ad         #攻击力
        def attack(self,d):
            d.hp -= self.ad
            print('%s攻击了%s,%s掉了%s点血'%(self.name,d.name,d.name,self.ad))
    
    class Dog:
        def __init__(self,name,kind,hp,ad):
            self.name = name
            self.kind = kind
            self.hp = hp
            self.ad = ad
        def bite(self,p):
            p.hp -= self.ad
            print('%s咬了%s一口,%s掉了%s点血' % (self.name, p.name, p.name, self.ad))
    
    alex = Person('a_sb','不详',1,5)
    boss_jin = Person('金老板','女',20,50)
    teddy = Dog('笨笨','teddy',50,10)
    teddy.bite(alex)
    print(alex.hp)
    boss_jin.attack(teddy)
    print(teddy.hp)
    

      



    命名空间和作用域
    class Person:
        role = 'person'   # 静态属性
        def __init__(self,name,sex,hp,ad):
            self.name = name     # 对象属性 属性
            self.sex = sex
            self.hp = hp
            self.ad = ad
        def attack(self):
            self.hobby = 'girl'
            print('%s发起了一次攻击'%self.name)
    
    alex = Person('a_sb','不详',1,5)
    alex.attack()   # Person.attack(alex)
    alex.age = 81
    # alex --> Person
    # Person实例化了alex
    print(alex.__dict__)
    

    alex.name #alex 指向我自己的内存空间 在自己的内存空间里找到name
    alex.attack #alex 先找自己的内存空间 再找到类对象指针 再根据类对象指针找到类 再通过类找到attack
    对象的内存空间里: 只存储对象的属性,而不存储方法和静态属性
    方法和静态属性都存储在类的内存空间中
    为了节省内存,让多个对象去共享类中的资源

     命名空间和作用域

    写一个类 完成一个功能 : 可以统计这个类有几个对象
    class Foo:
        count = 0
        def __init__(self):
            Foo.count += 1
    f1 = Foo()
    f2 = Foo()
    f3 = Foo()
    f4 = Foo()
    f5 = Foo()
    print(Foo.count)  #
    

      

    1.

    class Life:
        money = 10
        def __init__(self,name):
            self.name = name
        def work(self):
            print(self.name,'工作,赚了1000块钱')
            # print(self.__dict__)
            # print(Life.money)
            # print(self.money)
            self.money += 1000
    father = Life('father')#father 工作,赚了1000块钱/
    father.work()
    print(father.money)
    mother = Life('mother')
    mother.work()
    print(mother.money)
    print(father.money)
    # father.money = 10
    print(father.__dict__)#{'money': 1010, 'name': 'father'}
    print(Life.money)#

    在对象的空间中新创建了一个字典self.money += 1000
    
    

      

     2.

    class Live:
        money = [1]
        def __init__(self,name):
            self.name = name
        def work(self):
            print(self.name,'工作赚了1000块钱')
            # print(self.money)
            self.money[0] +=1000
    father2 = Live('father2')
    mother2 = Live('mother2')
    father2.work()
    mother2.work()
    print(mother2.__dict__)#{'name': 'mother2'}
    print(father2.money)
    print(mother2.money)#[2001]
    print(Live.money)#[2001]
    print(Live.__dict__['money'])
    
    
    
    
    由于传递的是可变类型的元素的改变,所以导致live.money的元素也发生改变
    

      3.

    class Live2:
        money= [0]
        def __init__(self,name):
            self.name = name
        def work(self):
            print(self.name,'工作赚了1000块钱')
            Live2.money = [Live2.money[0]+1000]
    
    father3 = Live2('father3')
    mother3 = Live2('mother3')
    mother3.work()
    father3.work()
    print(father3.__dict__)
    print(Live2.money)
    

      

    兑现属性是独有的,静态属性和方法是共享的
    对象使用名字,先找自己内存空间中的,再找类的内存空间的
    类名.静态变量;对于静态属性的修改,应该使用类名直接修改


    面向对象实例
    # 圆
    # 计算圆的周长 2 pi r
    # 计算圆的面积 pi r**2
    # 5个圆
    # 1,3,5,7,9
    from math import pi
    class Circle:
        def __init__(self,r):
            self.r = r
        def cal_area(self):
            '''
            计算圆面积的方法
            :return:返回值是float数据类型的面积
            '''
            return pi*self.r**2
        def cal_perimeter(self):
            '''
            计算圆周长的方法
            :return:返回值是float数据类型的周长
            '''
            return pi * self.r * 2
    for i in range(1,10,2):
        c1 = Circle(i)
        print(c1.cal_area())
        print(c1.cal_perimeter())
    

      

     

      




  • 相关阅读:
    迭代器生成器
    eval()
    【剑指offer】调整数组数字位置
    【剑指offer】二进制中1的个数
    【剑指offer】斐波那契数列非递归求解第N项
    【剑指offer】两个栈实现队列
    【剑指offer】逆序输出链表
    【剑指offer】字符串替换
    【剑指offer】规则二维数组查找
    Java转型
  • 原文地址:https://www.cnblogs.com/jinfanfu/p/8796951.html
Copyright © 2011-2022 走看看