zoukankan      html  css  js  c++  java
  • 面向对象-属性查找

    """
    在现实世界中:
    对象1:王二丫
        特征:
            学校='luffycity'
            名字=王二丫
            性别=女
            年龄=18
        技能:
            吃饭
            学习
    
    对象2:张铁蛋
        特征:
            学校='luffycity'
            名字=李三胖
            性别=男
            年龄=38
        技能:
            吃饭
            学习
    
    对象3:
        特征:张铁蛋
            学校='luffycity'
            名字=张铁蛋
            性别=男
            年龄=48
        技能:
            吃饭
            学习
    
    """
    
    
    class LuffyCity:
        school = 'student'
    
        def __init__(self, name, sex, age):
            self.Name = name
            self.Sex = sex
            self.Age = age
    
        def learn(self,x):
            print('%s is learning %s' % (self.Name, x))
    
        def eat(self):
            print('%s is eatting' % self.Name)
    
    
    stu1 = LuffyCity('李二丫', '', 18)
    stu2 = LuffyCity('李三胖', '', 38)
    stu3 = LuffyCity('张铁蛋', '', 48)
    对象是特指与技能的结合体
    类是 一系类对象的相似的特征与相似的技能的结合体
    print(LuffyCity)
    print(stu1.__dict__)
     类中的数据属性:对象所特有的;id相同 显示是共享的
    print(LuffyCity.school, id(LuffyCity.school))
    print(stu1.school, id(stu1.school))
    print(stu2.school, id(stu2.school))
    print(stu3.school, id(stu3.school))
    类中的函数属性:是绑定给对象使用。绑定不同对象是不同的绑定方法。对象调用绑定方法,会把对象本身当做第一个传入,传给self。
    print(LuffyCity.learn)  # ---> <function LuffyCity.learn at 0x102df4378>
    # print(LuffyCity.learn(stu1))  # ---> 李二丫 is learning
    # print(LuffyCity.learn(stu2))
    # print(LuffyCity.learn(stu3))
    
    # print(stu1.learn)  # ----> 绑定函数<bound method LuffyCity.learn of <__main__.LuffyCity object at 0x102dfe1d0>>
    # print(stu1.learn(1))  # --->李二丫 is learning 1
    # print(stu2.learn(2))
    # print(stu3.learn(3))
    
    LuffyCity.x = 'from luffycity class' #增加一个变量x
    print(stu1.__dict__) # 查看stu1 并无x
    print(stu1.x)  # --->from luffycity class
  • 相关阅读:
    237. Delete Node in a Linked List
    430. Flatten a Multilevel Doubly Linked List
    707. Design Linked List
    83. Remove Duplicates from Sorted List
    160. Intersection of Two Linked Lists
    426. Convert Binary Search Tree to Sorted Doubly Linked List
    142. Linked List Cycle II
    类之间的关系
    初始化块
    明确类和对象
  • 原文地址:https://www.cnblogs.com/hexiaorui123/p/10201355.html
Copyright © 2011-2022 走看看