Learning Python (class instance attributes and attribute)

Programming languages ​​around instances (objects), function, and expand the scope (the life cycle of the object)

Python's import sys links or other similar process when the c language compiler, not a function of the same file, and how can we use it to

Python class with c ++ class is basically the same thing, is not protected Python permission, only private and public, many feel the friendly

Examples of ordinary members of the same attributes and c ++, the class attribute is similar to the static member (static variable), once initialized, accessed and modified by the class name, all instances of maintenance with a static member variables of the class

__init__ function similar to the configuration, self similar to this pointer

Substantially the same package inheritance, overloaded functions will first tone classes, subclasses is no modulation of the function when calling the parent class

class vehicle():
    def out(self):
        print("vehicle out.")

class car(vehicle):
    pass
    #def out(self):
        #print("car out.")

o1 = car()
o1.out()

class Student(object):
    count = 0
    def __init__(self,name):
        self.name = name
        Student.count += 1

if Student.count != 0:
    print('fail!')
else:
    bart = Student('Bart')
    if Student.count != 1:
        print('fail!')
    else:
        lisa = Student('Bart')
        if Student.count != 2:
            print('fail!')
        else:
            print('Students:', Student.count)
            print('success!')

Casually practiced according to the books, the feeling is amazing

Guess you like

Origin www.cnblogs.com/doulcl/p/11233620.html