• 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.普通方法:一般的在类里面的没有什么特点的方法
  • 相关阅读:
    Nhibernate初学
    TSQL笔记
    Java是剑客飘逸;.NET是刀客霸道 (一) 【转载】
    在datagrid中求和(vb.net,c#)
    Java牢骚之我见(转载)
    Java是剑客飘逸;.NET是刀客霸道 (二) 【转载】
    可可西里观后感(转)保护藏羚羊
    .net快速入门方法,转csdn
    手工添加“显示桌面”快捷方式
    过年128>24
  • 原文地址:https://www.cnblogs.com/peiminer/p/9159730.html
Copyright © 2020-2023  润新知