第二十三天面向对象的第二天

1.使用面向对象编写求正方形面积和周长的程序:

class Square:
    def __init__(self,side_len):
        self.side_len=side_len
    def permeter(self):
        return self.side_len*4
    def area(self):
        return self.side_len*self.side_len
ret=Square(4)
ret1=ret.permeter()
print(ret1)
结果为
16
View Code

2类里面主要有两种属性:静态属性和动态属性,一般类里面定义的都是静态属性,函数里定义的大多都是动态属性

3.上一节可我们了解到可以使用类名.对象来查看类中的变量属性,但是不能进行修改可以使用下面方法进行修改:

class Money:
    money=100
    def __init(self):
        pass
Money.money=200  #通过此方法来对类里的属性进行修改
print(Money.money)
结果
200
View Code

4.也可以使用对象名对类里面的属性进行读取但是不能修改,如果进行修改是在其对象的字典里增加一个键值对:

class Money:
    money=100
    def __init(self):
        pass
Money.money=200  #通过此方法来对类里的属性进行修改
print(Money.money)
ret=Money()
print(ret.money) #通过此方法来对类里的属性进行查看
ret.money=300  
print(ret.money)#无法进行修改只能增添此对象里面的键值对
结果为

200
200
300
View Code

  4的内存空间执行过程如下:

 5.如果类中的属性是可变数据类型,那么在程序中会有什么不同的效果那:

class Course:
    language='chinese'
    def __init__(self,teacher,course_name,period,price):
        self.teacher=teacher
        self.name=course_name
        self.period=period
        self.price=price
    def func(self):
        pass
alex=Course('alex','python',5,14444)
small=Course('small','python',5,14444)
print(small.language)  #使用不可变数据类型,未修改之前调用的值都一样
print(alex.language)
结果为
chinese
chinese
View Code
class Course:
    language='chinese'
    def __init__(self,teacher,course_name,period,price):
        self.teacher=teacher
        self.name=course_name
        self.period=period
        self.price=price
    def func(self):
        pass
alex=Course('alex','python',5,14444)
small=Course('small','python',5,14444)
alex.language='English'#不会修改类属性里面的值而且此对象以后调用时优先调用此值
print(small.language)  #还是继续使用类属性里面的值
print(alex.language)#使用不可变数据类型,修改其中一个调用的值发生改变
结果为
chinese
English
View Code

  如果时可变数据类型:

class Course:
    language=['chinese']
    def __init__(self,teacher,course_name,period,price):
        self.teacher=teacher
        self.name=course_name
        self.period=period
        self.price=price
    def func(self):
        pass
alex=Course('alex','python',5,14444)
small=Course('small','python',5,14444)
alex.language[0]='English'#如果是可变数据类型,并且不是赋值,而是改变索引上的值,则类属性里的值发生改变
print(small.language) 
print(alex.language)

结果为
['English']
['English']
View Code

   这个执行过程如下:

6.总结对于可变数据类型修改是共享的,重新赋值时独立的。而不可变数据类型赋值就相当于独立。

7.模拟人生的程序:

class Person:
    monkey=0
mother=Person()
father=Person()
father.monkey+=1000
mother.monkey+=1000
print(Person.monkey)
结果为
0
View Code

  发现挣得钱没有体现出来,

class Person:
    monkey=0
    def work(self):
        Person.monkey+=1000
mother=Person()
father=Person()
mother.work()
father.work()
print(Person.monkey)
结果为
2000
View Code

8.创建一个类,没实例化一个对象就进行一次计数,最终所有的对象共享此数据:

class Count:
    count=0
    def __init__(self):
        Count.count+=1
f1=Count()
f2=Count()
print(f1.count)
print(f2.count)
f3=Count()
print(f1.count)
结果为
2
2
3
View Code

9.初始绑定方法:

def func():
    pass
print(func)#打到的时调用函数的内存地址
结果为
<function func at 0x0000026AED832EA0>
View Code
f1=Foo()
print(Foo.func)#这个打印的是Foo.fun的内存地址
print(f1.func)#由最后一个可知后面是类的对象然后前面意思为类对象里方法的调用
print(f1.func1)
print(f1)
结果为
<function Foo.func at 0x0000027BB2385620>
<bound method Foo.func of <__main__.Foo object at 0x0000027BB21E9080>>
<bound method Foo.func1 of <__main__.Foo object at 0x0000027BB21E9080>>
<__main__.Foo object at 0x0000027BB21E9080>
View Code

10包里面也有init为什么调用包之后就会执行init文件:

答:import一个包就相当于对类进行一次实例化,然后就相当于调用了类,就会执行init文件,

import time  #类的实例化
time.time()#相当于对对象进行调用方法
结果为
1582536792.7421083
View Code

11.给人狗大战中的人添加一个武器程序:

class Dog:
    def __init__(self,*args):
        self.name=args[0]
        self.blood=args[1]
        self.aggr=args[2]
        self.kind=args[3]
    def bite(self,person):
        person.blood-=self.aggr
class Person:
    def __init__(self,*args):
        self.name=args[0]
        self.blood=args[1]
        self.aggr=args[2]
        self.sex=args[3]
        self.money=0
    def attack(self,dog):
        dog.blood-=self.aggr
    def get_weapon(self,weapon):  #给人添加一个武器
        if self.money>=weapon.price:  #判断自己的钱是否满足武器的钱数
            self.money-=weapon.price #卖武器之后剩余的钱数
            self.weapon=weapon  #自己定义武器
            self.aggr+=weapon.aggr
        else:
            print('余额不足,请先充值')
class Weapon:
    def __init__(self,name,aggr,njd,price):
        self.name=name
        self.aggr=aggr
        self.njd=njd
        self.price=price
    def handl8(self,person):
        if self.njd>0:
            person.blood-=self.aggr*2
            self.njd-=1
alex=Person('alex',0.5,100,'不想')
jin=Dog('金老板',100,500,'teedy')
w=Weapon('打狗棒',100,3,998)
alex.money+=1000
alex.get_weapon(w)
print(alex.weapon)#输出人拿到武器后所有的属性
print(alex.aggr)
alex.attack(jin)
print(jin.blood)
alex.weapon.handl8(jin)
print(jin.blood)

结果
<__main__.Weapon object at 0x0000019A9262B470>
200
-100
-300
View Code

12.求圆环类或者圆形类的面积和周长:

from math import pi
class Circle:
    def __init__(self,*args):
        self.r=args[0]
    def permeters(self):
        return 2*pi*self.r
    def area(self):
        return pi*(self.r**2)#要用括号保证优先级
class Ring:
    def __init__(self,outside_r,inside_r):
        self.outside_c=Circle(outside_r)
        self.inside_c=Circle(inside_r)
    def area(self):
        return self.outside_c.area()-self.outside_c.area()
    def perimeter(self):
        return self.outside_c.permeters()+self.outside_c.permeters()
942.4777960769379
View Code

13.创建一个老师的类,老师也有生日,生日也可以是一个类使用组合:

class Birthday:
    def __init__(self,year,month,day):
        self.year=year
        self.month=month
        self.day=day
class Course:
    def __init__(self,course_name,period,price):
        self.name=course_name
        self.period=period
        self.price=price
class Teacher:
    def __init__(self,name,age,sex,birthday,course):
        self.name=name
        self.age=age
        self.sex=sex
        self.birthday=birthday
        self.course=course
b=Birthday(2019,11,4)
c=Course('python',6,10000)
ret=Teacher('alex',15,'',b,c)
print(ret.course.price)
结果为
10000
View Code
原文地址:https://www.cnblogs.com/ab461087603/p/12357852.html