(以下代码全部调试通过)
#类与对象:函数写在类的里面的时候叫方法,在外面叫函数,写法是一样的
#类是抽象的,对象是具体的
class Students:#定义学生类 有冒号表示类有内容
#定义属性
name = ''
age = 0
gander = ''
grand = 0
#定义学生的方法
def learn(self):
print('学习')
def play(self):
print('玩耍')
stu1 = Students()
stu1.name = 'aa'
stu1.age = 16
print(stu1.name)
print(stu1.age)
stu1.learn()
#构造函数
class Students:
#初始化属性
def __init__(self,name,age,gander,grand):
self.name = name
self.age = age
self.gander = gander
self.grand = grand
#定义学生的方法
def learn(self):
print('学习')
def play(self):
print('玩耍')
stu1 = Students('aa',12,'male',3)
print(stu1.age)
stu1.learn()
#创建电脑类
class Computers:
def __init__(self,brand,price,color,size):
self.brand = brand
self.price = price
self.color = color
self.size = size
def printf(self):
print('打印')
def install(self):
print('安装软件')
#def install(self,chip):
#print(self.brand+'安装软件'+chip)
comp1 = Computers('huawei',4999,'white',14)
print(comp1.brand)
comp1.printf()
comp1.install()
#类变量与实例变量,实例化
class Computers:
price = 3000
def __init__(self,brand,color,size):
self.brand = brand
#self.price = price
self.color = color
self.size = size
def printf(self):
print('打印')
def install(self):
print('安装软件')
#def install(self,chip):
#print(self.brand+'安装软件'+chip)
comp1 = Computers('huawei','white',14)
print(comp1.price)
comp1.printf()
comp1.install()
#私有化属性之类的封装
class Cards:
def __init__(self,account,password,balance):
self.account = account
self.password = password
self.__balance = balance
def deposit(self):
print('进行存款')
def getBalance(self,account,password):
if self.account == account and self.password == password:
return self.__balance
user1 = Cards('aaa','000000',8888)
p = user1.getBalance('aaa','000000')
print(p)
#类的继承
#动物类
class Animal:
def __init__(self,gender,color):
self.gender = gender
self.color = color
def eat(self):
print('动物要吃饭')
def play(self):
print('动物要玩耍')
class Cat(Animal):
pass
#实例化对象
bosi = Cat('male','white')
bosi.play()
#创建Person类
class Person:
def __init__(self,name,age,gender):
self.name = name
self.age = age
self.gender = gender
def personInfo(self):
print(self.name,self.age,self.gender)
user1 = Person('aa','16','male')
p = user1.personInfo()
#创建Farmers农民类
class Person:
def __init__(self,name,age,gender):
self.name = name
self.age = age
self.gender = gender
def personInfo(self):
print('姓名:'+self.name,'年龄:'+self.age,'性别:'+self.gender)
#定义农民类
class Farmers(Person):
def __init__(self,job,place):
self.job = job
self.place = place
def personInfo(self):
print('我是农民,我的工作是:'+self.job+'\n我的工作地点是:'+self.place)
#定义科学家类
class Scientists(Person):
def InventionM(self):
print('袁隆平发明了杂交水稻')
f = Farmers('种地','田野')
f.personInfo()
s = Scientists('袁隆平','90','male')
s.personInfo()
s.InventionM()
#抽象类
#为什么要创建抽象类?强制要求子类做某件事
#创建父类
class Animals:
def __init__(self,name,color):
self.name = name
self.color = color
#定义普通方法
def eat(self):
print('寻找食物')
#创建子类-猫类
class cats(Animals):
def eat(self):
print('吃鱼')
#创建子类-狗类
class dogs(Animals):
def eat(self):
print('吃骨头')
#实例化
d = dogs('大黄','black')
d.eat()
import abc
class Animals(metaclass=abc.ABCMeta):
#普通方法
def __init__(self,name,color):
self.name = name
self.color = color
#抽象方法:
@abc.abstractmethod
def eat(self):pass
#创建子类-狗类
class dogs(Animals):
def eat(self):
print('吃骨头')
#实例化
d = dogs('大黄','black')
d.eat()
python之面向对象
最新推荐文章于 2024-08-29 10:04:09 发布