preface

We have already learned the basic data types, functions and file reading and writing in Python. Next, we will look at object-oriented programming in Python. If you have learned object-oriented programming language like Java, you will feel very easy to learn compared to Object-oriented programming in Python. Python is much simpler and more convenient in object-oriented terms than Java, with far fewer constraints.

Create an object

Python, like other languages, uses the class keyword to declare a class. The class name is humped and begins with a capital letter.

class Animal:

    def __init__(self, name, age):

        self.name = name

        self.age = age



    def run(self):

        print(self.name + " is running")

Copy the code

__init__()Methods are essential in order to instantiate objects

As shown in the code above, the __init__() method in the Pyhton class is essential for instantiating object initialization, somewhat similar to constructors in Java. Self is essential in the __init__() method and must come first, being a reference to itself.

Create an instance

Python instantiates objects in the following way, without using the new keyword.

class Animal:

    def __init__(self, name, age):

        self.name = name

        self.age = age



    def run(self):

        print(self.name + " is running")





dog = Animal('lily', 2)

Copy the code

Access properties and methods

Both properties and methods are directly accessible in Python

class Animal:

    def __init__(self, name, age):

        self.name = name

        self.age = age



    def run(self):

        print(self.name + " is running")





dog = Animal('lily', 2)

print(dog.age)

print(dog.name)

dog.run()



Copy the code

Add default values to properties

Python can set default values for properties so that no arguments are passed during initialization.

class Animal:

    def __init__(self, age):

        self.name = 'Lily'

        self.age = age



    def run(self):

        print(self.name + " is running")

Copy the code

Modifies the value of an attribute

Pyhton can modify the value of a property in two ways, first by directly accessing it.

class Animal:

    def __init__(self, name, age):

        self.name = name

        self.age = age



    def run(self):

        print(self.name + " is running")





dog = Animal('lily', 2)

dog.name = 'Harry'

dog.age = 3

print(dog.age)

print(dog.name)

dog.run()

Copy the code

The result is shown below


Property values can also be changed through methods, similar to setters in Java.

class Animal:

    def __init__(self, name, age):

        self.name = name

        self.age = age



    def run(self):

        print(self.name + " is running")



    def update_name(self, name):

        self.name = name





Dog = Animal('Harry', 2)

Dog.update_name('Lily')

Dog.run()

Copy the code

Inherit the parent class

Inheritance is also possible in Python, but instead of using the extends keyword, parentheses are used directly. Super ().__init__(name, age) ties the subclass to its parent class.

class Animal:

    def __init__(self, name, age):

        self.name = name

        self.age = age



    def run(self):

        print(self.name + " is running")





class Dog(Animal):

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

        super().__init__(name, age)

        self.leg = 4





Harry = Dog("Harry", 2, 4)

print(Harry.leg)

Copy the code

Overriding methods

Overriding is relatively simple in Python. If a subclass has a function with the same name as its parent, the subclass’s instance calls the function directly.

class Animal:

    def __init__(self, name, age):

        self.name = name

        self.age = age



    def run(self):

        print(self.name + " is running")



    def update_name(self, name):

        self.name = name





class Dog(Animal):

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

        super().__init__(name, age)

        self.leg = 4



    def run(self):

        print("Dog is running")





Harry = Dog("Harry", 2, 4)

Harry.run()

Copy the code

The last

Like is the biggest support, more articles and information can follow the wechat public number QStack.