python类的介绍1
来源:互联网 发布:软件著作权 评职称 编辑:程序博客网 时间:2024/05/02 07:41
#!/usr/bin/python
#-*- coding:utf8 -*-
class Student:
name = ""
sex = "F"
def __init__(self):
self.name = "Hello" #默认的是Hello
def setName(self, name):
self.name = name
def getName(self):
return self.name
@classmethod #表示是类方法 1
def setSex(self,sex):
self.sex = sex
def getSex(self):
return self.sex
method = classmethod(getSex) #类方法的第二种声明
class SmallStudent(Student): #表示集成Student
"""
This is a small student's class
"""
age = 0
def __init__(self):
self.age = 10;
@staticmethod
def setAge(age):
SmallStudent.age = age #please pay attention
def getAge():
return SmallStudent.age #***
agemethod = staticmethod(getAge)
#内置类
class Fruit:
class Apple:
def getColor(self):
return "red"
class Pear:
def getColor(self):
return "yellow"
if __name__ == "__main__":
stu = Student()
stu.name = "Jack"
#stu.setName("Jack")
print stu.getName()
smallStu = SmallStudent()
print SmallStudent.__bases__ #输出父类组成的元组
print smallStu.__dict__ #输出属性组成的字典
print smallStu.__module__ #输出类的模块名
print smallStu.__doc__ #输出文档介绍
#类方法
Student.setSex("M")
print Student.method()
SmallStudent.setAge('12')
print SmallStudent.agemethod()
f = Fruit()
app = f.Apple()
pea = f.Pear()
print app.getColor()
print pea.getColor()
#第二种调用方式
app1 = Fruit.Apple()
#-*- coding:utf8 -*-
class Student:
name = ""
sex = "F"
def __init__(self):
self.name = "Hello" #默认的是Hello
def setName(self, name):
self.name = name
def getName(self):
return self.name
@classmethod #表示是类方法 1
def setSex(self,sex):
self.sex = sex
def getSex(self):
return self.sex
method = classmethod(getSex) #类方法的第二种声明
class SmallStudent(Student): #表示集成Student
"""
This is a small student's class
"""
age = 0
def __init__(self):
self.age = 10;
@staticmethod
def setAge(age):
SmallStudent.age = age #please pay attention
def getAge():
return SmallStudent.age #***
agemethod = staticmethod(getAge)
#内置类
class Fruit:
class Apple:
def getColor(self):
return "red"
class Pear:
def getColor(self):
return "yellow"
if __name__ == "__main__":
stu = Student()
stu.name = "Jack"
#stu.setName("Jack")
print stu.getName()
smallStu = SmallStudent()
print SmallStudent.__bases__ #输出父类组成的元组
print smallStu.__dict__ #输出属性组成的字典
print smallStu.__module__ #输出类的模块名
print smallStu.__doc__ #输出文档介绍
#类方法
Student.setSex("M")
print Student.method()
SmallStudent.setAge('12')
print SmallStudent.agemethod()
f = Fruit()
app = f.Apple()
pea = f.Pear()
print app.getColor()
print pea.getColor()
#第二种调用方式
app1 = Fruit.Apple()
print app1.getColor()
QQ交流群: 204944806
0 0
- python类的介绍1
- 关于Python类的介绍
- python类的简单介绍
- python中元类的介绍
- Python:class类的简单介绍
- Python简单的介绍
- Python的详细介绍
- python的isinstance介绍
- python的zeroMQ介绍
- Python编程的介绍
- python assert的介绍
- 1。Python的介绍(记录学习Python)
- Python 类属性介绍
- Python 类介绍
- python的lambda函数介绍
- python的lambda函数介绍
- Python的GUI简单介绍
- python的id()函数介绍
- gdi+ 快速绘制透明窗口
- Linux内核的示意图 [摘抄]
- linux后台开发必经之书
- 希尔排序 小讲
- zero与usb-skeleton结合测试
- python类的介绍1
- 手机号码和电话号码的正则表达式
- NYOJ 题目676 小明的求助(快速幂)
- Linux环境下USB的原理、驱动和配置
- Haproxy+Keepalived+Jboss集群实施架构一例
- 观察者模式
- 编程算法 - 组合数 代码(C)
- 如何解决IE7和IE8的兼容性问题
- HAProxy+Hive构建高可用数据挖掘集群