Python中类的使用(1简单了解类)


:是一直数据类型,本身不占内存空间,跟number,string,boolean等类似
用类创建实例化对象(变量),对象占内存空间
格式:
class 类名(父类列表):
属性
行为
self 代表类的实例,不是类
哪个对象调用方法,那么该方法中的self就代表对象,self不是关
键字,换成其他标识符也都可以
类名:self.__class__
# object:  基础类,超类,所有类的父类,没有合适的父类都可以写object
class Person(object):
#定义属性(定义变量)
name=""
age=0
height=0
weight=0

#定义方法(定义函数)
#注意:方法的参数必须以selff当第一个参数
#self代表类的实例(某个对象)
    def run(self):
print("run")
print(self.__class__) #类名<class '__main__.Person'>
x=self.__class__("sam",11,120,30)
print(x) #<__main__.Person object at 0x008C4190>
def eat(self, food):
print("eat" + food)
def say(self):
print("Hello!My name is %s,I am %d years old "%(self.name,self.age))
def play(a): #self不是关键字,换成(a)其他标识符也都可以
print("play")
def __init__(self,name,age,height,weight):#构造函数
self.name=name
self.age=age
self.height=height
self.weight=weight
per1=Person("lilei",18,168,70)
per1.say()
per1.play()
per1.run()
 
posted @ 2019-03-04 15:40  飞飞阿  阅读(1232)  评论(0编辑  收藏  举报