zoukankan      html  css  js  c++  java
  • python 类的介绍实例

    使用面向对象的优点:

    1.能够更好的设计软件架构

    2.维护软件模块

    3.易于架构和组件的重用

    类的定义:

    构造函数:初始化用,写不写都可以,默认为空

    类属性:属于类的对象

    方法属性:不属于类的对象

    私有方法: 只能自己的类中用,别人不能调用

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


    class Person(object):
    '''使用'''
    country = 'China'

    def __init__(self, name, age):
    print "start"
    self.age = age
    self.name = name

    def __del__(self):
    print ' end '

    def getName(self):
    monica = 'manica'
    print monica
    return self.name

    def setName(self, name):
    self.name = name

    def getAge(self):
    return self.age

    def setAge(self, age):
    self.age = age

    def info(self):
    return "name is {0},age is {1},country is {2}".format(self.name, self.age, self.country)

    def __show(self):
    print "private function"

    def show(self):
    self.__show()

    @property
    def info0(self):
    return "name is {0},age is {1},country is {2}".format(self.name, self.age, self.country)

        @staticmethod
    def show0():
    print u"静态方法"

    @classmethod
    def cm(cls):
    print u"这是类方法"


    per = Person('monica', 26)
    print per.getAge()
    print per.getName()
    print per.info()
    print per.country
    print per.show()
    print per.info0
    Person.show0()
    Person.cm()


    per.setAge('25')
    print per.getAge()

    # 说明这个类是做什么的
    print Person.__doc__

    方法:
    1.类方法:用@classmethod
    2.静态方法:类里面,不用实例化,直接用,只是占位 Person.show0()
    3.特性方法:加一个装饰器(@property,不能有参数)per.info0 没有括号
    4.普通方法:一般的在类里面的没有什么特点的方法
  • 相关阅读:
    linux signal之初学篇
    Eclipse 每次打开workspace目录记录位置?
    [Clojure] A Room-Escape game, playing with telnet and pure-text commands
    孔雀翎----《Programming C# 》中国版 文章4版
    js一些编写的函数
    KVM,QEMU核心分析
    apche commons项目简介
    java基础—网络编程———建立聊天的形式
    学习算法
    css+html+js实现多级下拉和弹出菜单
  • 原文地址:https://www.cnblogs.com/peiminer/p/9159730.html
Copyright © 2011-2022 走看看