为了账号安全,请及时绑定邮箱和手机立即绑定
  • class Animal():

        pass


    dog = Animal()

    cat = Animal()


    dog.name='wangcai'

    cat.name='mimi'

    dog.age=5

    cat.age=3


    print(dog.name,dog.age)

    print(cat.name,cat.age)

    查看全部
  • class Animal():

        pass


    dog = Animal()

    cat = Animal()


    print(dog)

    print(cat)

    查看全部
  • class Animal:
        __count = 0
        def __init__(self,name):
            self.name = name
            Animal.__count += 1 #类的实例数量
    dog =  Animal("wangwang")
    cat = Animal("mimi")
    bird = Animal("jiji")
    print(Animal.__count)


    In the given Python code, the line Animal.count += 1 is using the class name Animal rather than self for incrementing the count variable. This is because count is a class variable, shared by all instances of the Animal class.

    When you use Animal.count, you are directly accessing the class variable and incrementing its value. This means that every time a new instance of the Animal class is created, the count variable is incremented for the entire class, not just for the specific instance.

    On the other hand, if you were to use self.count += 1 in the __init__ method, it would create an instance variable count specific to each instance of the class. In that case, each instance would have its own separate count value.


    class Animal:
        count = 0
        def __init__(self,name):
            self.name = name
            self.count += 1
    dog =  Animal("wangwang")
    cat = Animal("mimi")
    bird = Animal("jiji")
    print(dog.count)#独立计算每个实例都个数,都是1

    print(Animal.count)#类变量为0

    查看全部
  • class Animal:
        __count = 0
        
        def __init__(self,name):
            self.name = name
            Animal.__count +=1
        
        @classmethod
        def get_count(cls):
            return cls.__count
            
    dog = Animal("wangwang")
    cat = Animal("mimi")
    print(Animal.get_count())

    查看全部
  • class Animal():
        def __init__(self,age,name,location):
            self.__age = age
            self.__name = name
            self.__location = location
        def get_name(self):#通过定义实例方法来操作私有属性
            return self.__name
        def get_age(self):#通过定义实例方法来操作私有属性
            return self.__age
        def get_location(self):#通过定义实例方法来操作私有属性
            return self.__location
    dog = Animal(1,"sisi","Shanghai")
    bird = Animal(12, "gigi", "Anhui")
    cat = Animal(3, "mimi", "Canada")
    print(dog.get_age())
    print(bird.get_name())
    print(cat.get_location())

    查看全部
  • 在外部访问私有属性将会抛出异常,提示没有这个属性。
    虽然私有属性无法从外部访问,但是,从类的内部是可以访问的。私有属性是为了保护类或实例属性不被外部污染而设计的。


    # 类私有属性
    class Animal(object):
       __localtion = 'Asia'

    print(Animal.__localtion)

    Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
    AttributeError: type object 'Animal' has no attribute '__localtion'


    # 实例私有属性
    class Animal(object):
       def __init__(self, name, age, localtion):
           self.name = name
           self.age = age
           self.__localtion = localtion

    dog = Animal('wangwang', 1, 'GuangDong')
    print(dog.name) # ==> wangwang
    print(dog.age) # ==> 1
    print(dog.__localtion)

    Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
    AttributeError: 'Animal' object has no attribute '__localtion'

    查看全部
  • '''请定义一个动物类

    抽象出名字、年龄两个属性

    并实例化两个实例dog, cat。

    '''

    class Animal:
        def __init__(self,name,age):
            self.name = name
            self.age = age
    dog = Animal("wangwang",2)
    cat = Animal("cici",3)
    print(dog.name)
    print(cat.age)


    查看全部
  • '''请定义一个动物类

    并创建出两个实例dog, cat

    分别赋予不同的名字和年龄

    并打印出来'''


    class Animal:
        def __init__(self,name,age):
            self.name = name
            self.age = age
    dog = Animal("chichi", 2)
    cat = Animal("mike", 6)
    print(dog)
    print(cat)

    查看全部
  • '''请练习定义一个动物类

    并创建出两个实例dog, cat

    打印实例

    再比较两个实例是否相等'''
    class Animal:pass
    dog = Animal()
    cat = Animal()
    print(dog)
    print(cat)
    print(dog == cat)

    查看全部
  • mark

    查看全部
  • class Student(Person):
       def __init__(self, name, gender, score):
           super(Student, self).__init__(name, gender)

    查看全部
    0 采集 收起 来源:Python继承类

    2023-07-28

  • 实例属性的优先级高于类属性

    外部无妨访问私有属性(__),可以通过定义实例属性来访问

    查看全部
  • reduce(函数,list,初始化值)

    查看全部
    1. 不是纯函数式编程:允许有变量

    2. 支持高阶函数:函数可以作为变量

    3. 支持闭包:可以返回函数

    4. 支持匿名函数。


    查看全部
  • functools.partial(原函数,改变默认参数)

    查看全部
    0 采集 收起 来源:Python的偏函数

    2023-07-13

举报

0/150
提交
取消
课程须知
本课程是Python入门的后续课程 1、掌握Python编程的基础知识 2、掌握Python函数的编写 3、对面向对象编程有所了解更佳
老师告诉你能学到什么?
1、什么是函数式编程 2、Python的函数式编程特点 3、Python的模块 4、Python面向对象编程 5、Python强大的定制类

微信扫码,参与3人拼团

意见反馈 帮助中心 APP下载
官方微信
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!