Notes on Python basic learning (4)
There are three basic characteristics of object-oriented
- Encapsulation: abstract and encapsulate objective things into objects, that is, attributes, methods and events are integrated into a whole
- Inheritance: allows you to use the functions of existing classes and extend them without rewriting the original classes
- Polymorphism: the derived class has all the non private data and behaviors of the base class and all the data or behaviors defined by the new class itself, that is, the subclass has two valid types: the type of the subclass and the type of the inherited base class. The ability of objects to represent multiple types is called polymorphism (Python does not need to overload)
Concept of class and object:
Class and variable are two core concepts of object-oriented programming
Class: a general term for a group of things with the same characteristics (attributes) or behaviors (Methods), abstract, such as the general hero in the glory of the king
Object: a concrete existence created by class, which can be used directly, such as Li Bai in hero
Class design:
1. Type: meet the big hump naming method (each word is capitalized, and there is no underline between words)
2. Attribute: what are the characteristics of things
3. Method: what kind of behavior does things have
Determination of properties and methods:
1. The description of object features can be defined as attributes
2. The behavior (verb) of an object, usually defined as a method
Class definition and creation object:
Format of class definition:
To create an object format:
Instance properties:
The properties defined by "self. Variable name" are called instance properties, also known as instance variables. Each instance contains a separate copy of the class
Initialization (usually in the \\\\\\\\\
Other functions can be accessed through self: self. Instance variable = value
Create an object instance and access it through the object: obp1. Instance variable name = value
Example:
class Person: def __init__(self,name,age): self.name = name self.age = age def say_hi(self): print('Hello!My name is%s'%(self.name)) person = Person('HHS','21') person.say_hi() print('This year{0}'.format(person.age))
Output:
Hello!My name is HHS
21 this year
Class properties:
Python also allows you to declare variables belonging to the class itself, that is, class variables, static properties
Class properties belong to the whole class, not part of a specific instance, but a copy shared by all instances
Initialization (generally in class body): class variable name = initial value
Class access: class name. Class variable name = value
Example:
class Person:
count = 0
name = "HHS"
#test
Person.count += 1
print(Person.count) #1
print(Person.name) #HHS
p1 = Person()
p2 = Person()
print((p1.name,p2.name)) #('HHS', 'HHS')
Person.name = 'Guangzhou College of South China University of Technology'
print((p1.name,p2.name)) #('Guangzhou College of South China University of technology ','Guangzhou College of South China University of technology')
p2.name = 'Computer science and technology'
print((p1.name,p2.name)) #('Guangzhou College of South China University of technology ',' computer science and technology ')
p2.count += 1
print((Person.count,p1.count,p2.count)) #(1, 1, 2)
Explain:
If a class property is accessed by 'obj. Property name', it belongs to the instance property of the instance. Although the class property can be accessed by an object instance, it is easy to cause confusion. Therefore, it is recommended not to use this method, but to use the standard access method: class name. Class variable name
Private and public attributes:
Python class members have no access control restrictions and are different from other object-oriented languages
Generally, it is agreed that the attributes that start with two underscores and do not end with two underscores are private and others are public
Private properties cannot be accessed directly, but they can be accessed in methods
class A:
__name = 'class A'
def get_name():
print(A.__name)
#test
A.get_name() #class A
A.__name #AttributeError: type object 'A' has no attribute '__name'
Special properties:
Methods that start and end with double underscores are called special methods
Common special methods:
Property Dictionary of object
Object. Class the class to which the object belongs
Base tuple of class
Base class of class
Class. Name the name of the class
Class. MRO method search order, base class order
Class. Subclass sequence
class Person:
def __init__(self,name,age):
self.name = name
self.age = age
def say_hi(self):
print('Hello!My name is%s'%(self.name))
person = Person('HHS','21')
print(person.__dict__) #object.__dict__ Output:{'name': 'HHS', 'age': '21'}
print(person.__class__) #object.__class__ Output:<class '__main__.Person'>
print(Person.__bases__) #class.__bases__ Output:(<class 'object'>,)
print(Person.__base__) #class.__base__ Output:<class 'object'>
print(Person.__name__) #class.__name__ Output: Person
print(Person.__mro__) #class.__mro__ Output:(<class '__main__.Person'>, <class 'object'>)
print(Person.__subclasses__()) #class.__subclasses__() Output:[]
Continuous updating