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

        def __init__(self, name, gender, **kw):

            self.name = name

            self.gender = gender

            for k, v in kw.items():

                setattr(self, k, v)


    p = Person('Bob','Male',age=18,course='Python')

    print(p.age)

    print(p.course)

    查看全部
  • class Person(object):

        def __init__(self, name, gender):

            self.name = name

            self.gender = gender


    class Student(Person):

        def __init__(self, name, gender, score):

            super(Student, self).__init__(name, gender)

            self.score = score


    class Teacher(Person):

        def __init__(self, name, gender, course):

            super(Teacher, self).__init__(name, gender)

            self.course = course


    class SkillMixin(object):

        def __init__():

            pass


    class BasketballMixin(SkillMixin):

        def __init__():

            pass


    class FootballMixin(SkillMixin):

        def __init__():

            pass


    class Student_Basketball(Student,BasketballMixin):

        def __init__(self,name,gender,score):

            super(Student_Basketball,self).__init__(name,gender,score)

    class Teacher_Football(Teacher,FootballMixin):

        def __init__(self,name,gender,course):

            super(Teacher_Football,self).__init__(name,gender,course)

    查看全部
    0 采集 收起 来源:Python中的多态

    2023-10-10

  • class Person(object):

        def __init__(self, name, gender):

            self.name = name

            self.gender = gender


    class Student(Person):

        def __init__(self, name, gender, score):

            super(Student, self).__init__(name, gender)

            self.score = score


    class Teacher(Person):

        def __init__(self, name, gender, course):

            super(Teacher, self).__init__(name, gender)

            self.course = course


    p = Person('Tim', 'Male')

    s = Student('Bob', 'Male', 88)

    t = Teacher('Alice', 'Female', 'English')


    print(isinstance(t,Person))

    print(isinstance(t,Student))

    print(isinstance(t,Teacher))

    查看全部
    0 采集 收起 来源:Python判断类型

    2023-10-10

  • class Person(object):

        def __init__(self, name, gender):

            self.name = name

            self.gender = gender

    class Teacher(Person):

        def __init__(self,name,gender,subject):

            super(Teacher,self).__init__(self,name,gender)

            self.subject = subject

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

    2023-10-10

  • class Animal():

        __count = 0

        @classmethod

        def get_count(cls):

            return cls.__count

        def __init__(self, name, age, localtion):

            self.__name = name

            self.__age = age

            self.__localtion = localtion

            Animal.__count += 1

        def get_name(self):

            return self.__name

        def get_age(self):

            return self.__age

        def get_localtion(self):

            return self.__localtion

        def set_name(self,name):

            self.__name = name

            return

        def set_age(self,age):

            self.__age = age

            return

        def set_localtion(self,localtion):

            self.__localtion = localtion

            return

        def print_info(self):

            print('The name is : ' + self.__name)

            print('The age is :' + str(self.__age))

            print('The localtion is : ' + self.__localtion)

       

    dog = Animal('wangcai', 3, 'Beijing')

    print(Animal.get_count())

    dog.print_info()

    查看全部
  • class Animal():

        def __init__(self, name, age, localtion):

            self.__name = name

            self.__age = age

            self.__localtion = localtion

        def get_name(self):

            return self.__name

        def get_age(self):

            return self.__age

        def get_localtion(self):

            return self.__localtion

        def set_name(self,name):

            self.__name = name

            return

        def set_age(self,age):

            self.__age = age

            return

        def set_localtion(self,localtion):

            self.__localtion = localtion

            return

        def print_info(self):

            print('The name is : ' + self.__name)

            print('The age is :' + str(self.__age))

            print('The localtion is : ' + self.__localtion)

       

    dog=Animal('wangcai',3,'Beijing')

    dog.print_info()

    查看全部
  • class Animal():

        __count = 0

        __age = 0

        def __init__(self,name,age):

            self.name = name

            self.__age = age

            self.__count += 1

    dog=Animal('wangcai',3)

    print(dog.age)

    查看全部
  • class Animal():

        __count = 0

        def __init__(self,name,age):

            self.name = name

            self.age = age

            Animal.__count += 1

    dog=Animal('wangcai',3)

    print(Animal.__count)

    print(dog.__count)

    查看全部
  • class Animal():

        _count = 0

        def __init__(self,name,age):

            self.name = name

            self.age = age

            Animal._count += 1

    dog=Animal()

    print(Animal._count)

    print(dog._count)

    查看全部
  • class Animal():

        count = 0

        def __init__(self,name,age):

            self.name = name

            self.age = age

            Animal.count += 1


    dog = Animal('wangcai',5)

    print(dog.count)

    cat = Animal('mimi',3)

    print(dog.count,cat.count)


    print(dog.name,dog.age)

    print(cat.name,cat.age)

    查看全部
    0 采集 收起 来源:Python类属性

    2023-10-10

  • class Animal():

        def __init__(self,name,age):

            self.name = name

            self.age = age


    dog = Animal('wangcai',5)

    cat = Animal('mimi',3)


    print(dog.name,dog.age)

    print(cat.name,cat.age)

    查看全部
  • 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())

    查看全部

举报

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

微信扫码,参与3人拼团

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

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