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"静态方法"

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

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

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

    方法:
    1.类方法:
    2.静态方法:类里面,不用实例化,直接用,只是占位 Person.show0()
    3.特性方法:加一个装饰器(@property,不能有参数)per.info0 没有括号
    4.普通方法:
  • 相关阅读:
    P6329 【模板】点分树 | 震波
    Luogu P3350 [ZJOI2016]旅行者
    Luogu [ZJOI2015]幻想乡战略游戏
    斐波那契数列简单性质
    Luogu P2056 [ZJOI2007]捉迷藏
    Luogu P4127 [AHOI2009]同类分布
    A funny story in regard to a linux newbie
    Inside the c++ object module 阅读摘要
    java并发编程
    JVM执行引擎总结(读《深入理解JVM》) 早期编译优化 DCE for java
  • 原文地址:https://www.cnblogs.com/peiminer/p/9159725.html
Copyright © 2011-2022 走看看