current position:Home>Python learning notes - the fifth bullet * class & object oriented

Python learning notes - the fifth bullet * class & object oriented

2022-02-01 07:03:26 goonwalk

「 This is my participation 11 The fourth of the yuegengwen challenge 16 God , Check out the activity details :2021 One last more challenge 」.

object-oriented programming ——object oriented programming, abbreviation OOP, It's a design idea .OOP Take the object as the basic unit of the program , An object contains data and functions that manipulate data .Python Classes in provide all the basic functions of object-oriented programs : Class inheritance mechanism allows multiple base classes , Derived classes can override any method in the base class , Method can call a method with the same name in the base class . Objects can contain any number and type of data . class : Represents a description of a collection of types , attribute :

Some features of the class itself , Such as ( One of the ) name , Attributes such as height and weight . The specific value of the attribute will vary according to each person .

Method : The behavior that a class can achieve , How to eat , Walking, etc .

1. The definition of a class

class classname[( Parent class name )]:# The parent class name is optional , If a parent class is defined , Then the subclass inherits the properties and methods of the parent class [ Member functions and member variables ]

2. Class object

Class objects support two operations : Property references and instantiations .

Property references use and Python All attributes in reference to the same standard and method

After the class object is created , All names in the class namespace are valid property names .

image.png Many classes tend to create objects with initial states . Therefore, the class may define a class named init() Special method ( Construction method ),init() Method can have parameters , Parameters through init() Passed to the instantiation operation of the class :


3. Class method

Inside the class , Use def Keyword can define a method for a class , Different from general function definition , Class method must contain parameters self, And it's the first parameter :


copyright notice
author[goonwalk],Please bring the original link to reprint, thank you.

Random recommended