Object Oriented 1
Object-Oriented
Concepts Object-oriented is the use of classes and objects to create various models to describe the real world and to make programs simpler.
class class
A class is an abstraction of a class of objects with the same properties. The class defines that these objects have non-properties and methods.
object
An object is an instantiated instance of a class. A class must be instantiated before it can be called in a program. A class can have multiple attributes, and each object can also have different attributes.
Encapsulation is encapsulated
in a class The assignment of data and internal calls are transparent to external users, which makes the class become a container, which contains the properties and methods of the class.
Prevent data from being modified at will,
so that external programs do not need to pay attention to the internal structure of the object, and only need to directly access the interface provided by the object to
inherit inheritance
A class can derive subclasses, and the properties and methods defined in this class are automatically subclassed Inheritance
Through the parent class->subclass approach, the differences and commonalities of different roles are realized with minimal coding.
Polymorphism Polymorphism Different subclasses are derived from
a base class, and each subclass inherits the same method name while being aware of the parent The methods of a class have different implementations, which are multiple states of the same thing.
Defining classes and objects
Class definitions
1 class person(object):
2 #person class name, object is a fixed format
3 def __init__(self,name):
4 #The constructor of the class, the parameters of the object are passed in here. self means the object itself
5 self.name = name
6
7 def hello(self):
8 #Since self is equal to the object itself, the constructor has passed all parameters to self. So the methods of the class can directly call the properties of the class. There will be no local non-quantitative constructors that cannot be accessed by other methods
9 print("hello world,i'm %s"%self.name)
10
11 obj1 = person('zsq') #Instance of the class, that is Object
12 obj1.hello() #Object calling method Private properties of the
class The properties
of the object are stored in the object, and the methods of the object are stored in the class. Similar to the public attributes of the class introduced below
1 __author__ = 'zhang'
2
3 class person(object):
4 def __init__(self,name): self.name = name
5 self.__heart = 'normal' #private attribute, object Not callable. The method of the class can be called, and the operation cannot be called externally, but can be called internally
6 def hello(self):
7 print("hello world,i'm %s"%self.name)
8
9 def get_heart(self): #Method for accessing private attributes, so that the outside can access but not modify
10 return self.__heart
11
12 obj1 = person('zsq')
13 obj1.hello()
14 print(obj1.get_heart())#Access private attributes through class methods
15 print(obj1._person__heart) #Force access to private attributes Public attributes of
classes The public attributes
of objects are default It is in the reference class. When operating a public property through an object, it actually creates a new variable with the same name as the public property locally in the object, and has nothing to do with the original public property. It's just that when accessing, first look for the corresponding variable name locally, and if you can't find it, go to the class to find it. Deleting the object's own public properties will restore the values of the public properties in the class.
1 class person(object):
2 nationality = "CHINA"
3 def __init__(self,name):
4 self.name = name
5
6 obj1 = person('zsq')
7 obj2 = person('zsq1')
8 print(obj1.nationality)
9 print(obj2.nationality)
10 print('-------------')
11 person.nationality = "CN"
12 print(obj1.nationality)
13 print(obj2.nationality)
14 obj1.nationality = "USA"
15 print('-------------')
16 print(obj1.nationality)
17 print(obj2.nationality)
18 print('-------------')
19 del obj1.nationality
20 print(obj1.nationality)
21 print(obj2.nationality)
将类方法修改为私有方法
1 class person(object):
2 nationality = "CHINA"
3 def __init__(self,name):
4 self.name = name
5 def get_name(self):
6 print("get name is %s"%self.name)
7
8 obj1 = person('zsq')
9 obj2 = person('zsq1')
10
11 def get_name2(self):
12 print("this is private fun get name %s"%self.name)
13 obj2.get_name = get_name2
14 obj1.get_name()
15 obj2.get_name(obj2)
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326370975&siteId=291194637
Recommended
Ranking