Python Programming - Object Oriented Programming: Classes

understand object orientation

The basic principle is that a computer program consists of a number of units or objects that can function as subroutines

The key idea is that data and operations on data are encapsulated together to form an interdependent and indivisible whole, the object

Python object-oriented features

Full support for basic object-oriented features such as encapsulation, inheritance, polymorphism, and overriding or overriding of base class methods

In python, the concept of object is very broad, everything can be called an object

Object properties that are represented as variables when a class is created are called data members or member properties

Object behavior expressed in functional form is called member function or member method

one type

1. Definition and use of classes

# 定义类
class Car:
    def info(self):
        print("this is a car")

#Use the class to instantiate the object 
#Access the data members or member methods in it through "Object.Method" 
car = Car()
car.info()

out:
this is a car

#Use isinstance to test whether an object is an instance of a class 
isinstance(car,Car)

out:
True

#Keyword pass, similar to an empty statement 
# Occupation, reserved function space 
class Car:
     def info(self):
         print ( " this is a car " )
     def color(self):
         pass

2. Class members and instance members

Instance attributes, generally defined in the constructor __init__, must be prefixed with self when defined and used

Class attributes, data members defined outside of all methods in the class

In the main program (or outside the class), instance properties belong to the instance (object) and can only be accessed by the object name,

The class attribute belongs to the class and can be accessed by the class name or the object name

--------------------------------------------------------------------

In Python, you can dynamically add members to classes and members, reflecting Python's dynamic typing

--------------------------------------------------------------------

class Car:
    price = 10000       #Define class attribute def __init__ (self,c):    #Constructor self.color = 
        c    
#Define instance attribute car1 = Car( " red " )
     

car2 = Car("blue")
print(car1.color,Car.price)

Car.price = 11100   #Modify the class attribute 
Car.name = ' bird '   #Add the class attribute 
car1.color = " yellow "   #Modify the instance attribute 
print (car2.color, Car.price, Car.name)

There is a difference between functions and methods. Methods generally refer to functions bound to feature instances.

 1 # 动态为对象增加成员方法
 2 class Demo:
 3     pass
 4 t=Demo()
 5 def test(self,v)
 6     self.value = v
 7 
 8 t.test = test    # 增加成员方法 test
 9 t.test(t,20)
10 
11 print(t.value)
12 
13 out:
14 20

3. 私有成员与共有成员

python并不提供对私有成员严格的访问保护机制,

在定义类的属性时,如果属性名以两个下划线 __ 开头,表示私有属性

私有属性在类的外部不能直接访问,需要通过调用对象的公有成员方法来访问

公有属性可以公开使用,既可以在类的内部访问,也可以在外部程序中使用

----------------------------------------------------

使用下划线作为变量名和方法名前缀和后缀,表示类的特殊成员

_xxx 保护成员,只有类对象和子类对象才能访问

__xxx__ 系统定义的特殊成员

__xxx 类中的私有成员

# 特殊成员定义与访问
class Fruit:
    def __init__(self):
        self.__color='red'                  # 私有成员
        self.__type__='rectangle'           # 特殊成员
        self.price = 1                      # 公有成员

apple = Fruit()
print(apple.price)  # 对象公有数据成员的值

out:
1
apple.price = 2   # 修改对象公开数据成员的值
print(apple.price)

out:
2

apple.__type__ = 'Triangle'   # 修改特殊成员的值    
print(apple.__type__)    

out:
Triangle

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324788805&siteId=291194637