Object-oriented data mining Python foundation

Preface

Before we have learned the basic data types, functions and file reading and writing of Python, then we will take a look at Python's object-oriented programming. If you have learned an object-oriented programming language similar to Java, compare it with Python Object-oriented will feel very easy. As a scripting language, Python is more concise and convenient than Java in object-oriented, and has a lot of constraints.

Create object

Like other languages, Python uses the class keyword to declare a class. The name of the class uses camel case with the first letter capitalized.

class Animal:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def run(self):
        print(self.name + " is running")

__init__()Method is essential, this is to instantiate the object

As shown in the code above, the __init__()methods in the Pyhton class are indispensable, which are initialized to instantiate objects, a bit similar to the constructor in Java. __init__()Self is indispensable in the method, and must be placed first, it is a reference to itself.

Create instance

Python uses the following methods to instantiate objects 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)

Access properties and methods

Attributes and methods in Python can be directly accessed

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()

Add default values ​​to attributes

Python can set default values ​​for attributes, so you don't need to pass in parameters during initialization.

class Animal:
    def __init__(self, age):
        self.name = 'Lily'
        self.age = age

    def run(self):
        print(self.name + " is running")

Modify the value of the attribute

Pyhton can modify the value of an attribute in two ways. First, you can modify the attribute value through direct access.

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()

The result is shown below

You can also change the property value through methods, similar to the setter 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()

Inherit the parent class

Inheritance is also possible in Python, but instead of using the extends keyword, parentheses are used directly. Which super().__init__(name, age)links the child class with the 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)

Rewrite method

The rewriting method in Python is relatively simple. If the subclass has a function with the same name as the parent class, the instance of the subclass directly calls the function of the subclass when the function is called.

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()

At last

Likes is the biggest support. For more articles and information, please follow the WeChat public account QStack.

Guess you like

Origin blog.csdn.net/QStack/article/details/109754001