Class ==> instantiating ==> instance objects
__init__ constructor function
Self.name = Name # property, member variable
def sayhi () # method, dynamic property
def get_heart (self):
Return Self.__heart # provides an external access interface, but the external can only get its value and cannot change its value.
Property
- Public properties
- When all objects belonging to this class have access to properties called public properties (that is, global properties, defined in a class, not defined in a method < including construction method >) ? No, the global variables in Java can also be privatized, Just see if there is any private modification).
- Private properties
- __private_attr_name = value
- Enforce access to private property method: Instance Object _ Class name __ Property name
#!/usr/bin/python#-*-coding:utf-8-*-classRole (object): Nationality="JP" def __init__(Self, name, role, weapon, life_value=100, money=15000): Self.name=name Self.role=role Self.weapon=Weapon Self.life_value= Life_value#Public PropertiesSelf.money =Money self .__heart="Normal" #private property, not visible externally (equivalent to Private in Java) defShot (self):Print("%s is shooting"%self.name) self.__heart="Dead" Print(self.)__heart)#private properties can be accessed internally defGet_heart (self):returnSelf.__heart #provides an external access interface, but the external can only get its value and cannot change its value. defGot_shot (self):Print("ah ... I got shot ...") defBuy_gun (Self, gun_name):Print("just bought%s"%gun_name) Self.weapon= Gun_name#that way, if you buy any new weapons, the value of the property weapon will be updated synchronously .R1= Role ('Alex','Police','AK47')#generate a roler2 = Role ('Jack','Terrorist','B22')#generate a roler1.shot () R2.buy_gun ('AK47')Print(R2.weapon)#Add self.weapon = Gun_name in the Buy_gun () method so that if a new weapon is purchased, the value of the property weapon can be updated#print (R1.get_heart ())Print(R1._role__heart)#externally forcibly accessing a private property#There are two ways to change the public properties of a classRole.nationality ="US" #Method 1. The nationality value of all objects will be changed.R1.natiocnality ="Thailand" #Method 2. Only the nationality value of the object R1 will be changed.defShot2 (self):#In the class, the method is called directly, and the instance object is automatically passed into the method. Methods outside the class, you need to pass the instance into the method yourself. Print("my own Shot method") R1.shot=shot2r1.shot (R1)#The call is actually the Shot2 () method. Because the method Shot2 () is not in the class, you need to pass in the parameter yourself.class
Object-oriented features
- Packaging
- Main features of object and class concepts
- Encapsulate objective things into abstract classes, and classes can take their own data and methods to allow only trusted classes or objects to operate and to block untrusted information.
- Inherited
- You can use all of the features of the current class and extend these features without rewriting the original class.
- New classes created through inheritance are called "subclasses" or "derived classes."
- The inherited class is called the base class. "Parent class" or "Super class".
- The process of inheritance is from the general to the special process.
Python Object-oriented (ii)