类具有相同的属性和方法烦人对象的集合
对象:万物皆有对象(看的见摸得着都是对象)
类和对象的关系
Print(“ 内容”)
语法:class 类名
属性:形容词 身高、体重
方法“”
行为:动词
Class person:
Def eat():
Print(“正要吃饭”)
Def sleep():
Print(“正要睡觉”)
person.sleep():
person.eat():
”
定义一个类:一种概念,
创建对象:属性写在类外的情况,就是通过对象,属性,对象方法()的方式调用。
类的继承
类的多态
_str_
Return
属性相当于全局变量,每个方法都可以用
Get 获取某值/set设置某值。:
私有方法 def__test(): 只能在当前类中使用。
pritate私有、public公有、protected受保护、默认
公有方法
继承
多态
class person:
#初始化方法
def __init__(self, new_name, new_sex,new_money ):
#name属性
self.name=new_name
#sex属性
self.sex=new_sex
self.money=new_money
def shopping(self,xiaofei_money): #消费
self.money-=xiaofei_money
def eat(self,uname):
print("%s和%s正在吃饭..."%(self.name,uname))
def sleep(self):
print("%s正在睡觉..."%self.name)
def show(self):
print("我的名字是:{0},性别是{1}".format(self.name,self.sex))
def __str__(self):
return ("我的名字是:{0},性别是{1}".format(self.name,self.sex))
handaohong=person("韩道红","男",10000)
handaohong.eat("范冰冰")
handaohong.sleep()
handaohong.show()
print(handaohong)
handaohong.shopping(300)
handaohong.shopping(300)
handaohong.shopping(300)
handaohong.shopping(300)
handaohong.shopping(300)
print(handaohong.money)
wangyang=person("王洋","男",20000)
wangyang.eat("李冰冰")
wangyang.sleep()
wangyang.show()
print(wangyang)
class address:
def __init__(self,new_country,new_city):
self.country=new_country
self.city=new_city
def __str__(self):
return"国家:%s,城市:%s"%(self.country,self.city)
class student:
def __init__(self,sname,ssex,addr):
self.name=sname
self.sex=ssex
self.addr=addr
def __str__(self):
return "姓名: {0},性别:{1},地址 {2}".format(self.name,self.sex,self.addr)
china=address("中国","北京")
handaohong=student("梦想","男",china,)
print(handaohong)