Python: Object-oriented (class)

Source: Internet
Author: User

#!usr/bin/env python
#-*-Coding:utf-8-*-

__author__ = "Samson"
# # #类变量与实例变量
Class Role:
n = 123# class variable, stored in the class, not stored in the instance, can be used to save space
def __init__ (self, name, role, weapon, Life_value = +, Money = 15000):
#构造函数
#在实例化时做一些类的初始化工作
Self.name = name# instance variable (static property), stored in class
Self.role = Role
Self.weapon = Weapon
Self.life_value = Life_value
Self.__money = money# Private property, accessible only within the class

Def shot (self): #类的方法, features (dynamic properties)
Print ("Shooting ...")

def got_shot (self):
Print ("%s:ah...,i has got shot ..."%self.name)

def __show_money (self): #类的私有函数, can only be accessed within the class
Print ("money:%s"% Self.__money)

def buy_gun (self, gun_name):
Print ("%s just bought%s"% (Self.name, gun_name))

def __del__ (self): #析构函数, executed when an instance is released, destroyed, and usually used to do some finishing work, such as closing some databases, closing some open files
Print ("Execution destructor")

Print (Role)

R1 = Role ("Samson", "Police", "AK47") #把一个类变成一个具体对象的过程叫实例化
R1.got_shot () #Role. Got_shot (R1)
R1.bullet_prove = True #给类添加属性, this is possible

r2 = Role ("R2", "police", "AK47") #把一个类变成一个具体对象的过程叫实例化
R2.got_shot () #Role. Got_shot (R1)
Print (R1.weapon)
Del r1.weapon# Delete Properties

R1.N = 145# is equivalent to a new instance variable in R1 without affecting the class variable
Print (R1.N, R2.N) #r2中的n值不变

(not to be continued)












Python: Object-oriented (class)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.