• python:面向对象(类)


    #!usr/bin/env python
    # -*- coding:utf-8 -*-

    __author__ = "Samson"
    ###类变量与实例变量
    class Role:
    n = 123#类变量,存放在类中,不存放在实例中,可用于节省空间
    def __init__(self, name, role, weapon, life_value = 100, money = 15000):
    #构造函数
    #在实例化时做一些类的初始化工作
    self.name = name#实例变量(静态属性),存放在类中
    self.role = role
    self.weapon = weapon
    self.life_value = life_value
    self.__money = money#私有属性,仅能在类的内部访问

    def shot(self):#类的方法,功能(动态属性)
    print("shooting...")

    def got_shot(self):
    print("%s:ah...,I have got shot..." %self.name)

    def __show_money(self):#类的私有函数,仅能在类的内部访问
    print("money:%s" % self.__money)

    def buy_gun(self, gun_name):
    print("%s just bought %s" % (self.name, gun_name))

    def __del__(self):#析构函数、在实例释放、销毁的时候执行的,通常用于做一些收尾工作,如关闭一些数据库,关闭打开的一些文件
    print("执行析构函数")

    print(Role)

    r1 = Role("samson", "police", "AK47")#把一个类变成一个具体对象的过程叫实例化
    r1.got_shot()#Role.got_shot(r1)
    r1.bullet_prove = True #给类添加属性,这是可以的

    r2 = Role("r2", "police", "AK47")#把一个类变成一个具体对象的过程叫实例化
    r2.got_shot()#Role.got_shot(r1)
    print(r1.weapon)
    del r1.weapon#删除属性

    r1.n = 145#相当于r1中新增了一个实例变量,不影响类变量
    print(r1.n, r2.n)#r2中的n值不变



    #!usr/bin/env python
    # -*- coding:utf-8 -*-

    __author__ = "Samson"
    #继承
    #class People:#经典类
    class People(object):#新式类
    def __init__(self,name,age):
    self.name = name
    self.age = age

    def eat(self):
    print("%s is eating..." % self.name)

    def talk(self):
    print("%s is talking..." % self.name)

    def sleep(self):
    print("%s is sleeping..." % self.name)


    class Relation(object):
    def make_friends(self,obj):
    print("%s is making friends with %s" %(self.name,obj.name))
    class Man(People,Relation):#多继承
    def __init__(self,name,age,money):
    People.__init__(self, name, age)#经典类写法,与super(Man,self).__init__(name,age) 新式类写法一样
    self.money = money
    print("%s has %s dollars" %(self.name,self.money))
    def shopping(self):
    print("%s is shopping..." % self.name)

    def sleep(self):#重构父类方法
    People.sleep(self)
    print("man is sleeping...")

    m1 = Man("samson",23,10)
    m1.eat()
    m1.shopping()
    m1.sleep()

    m2 = Man("sun",24,20)
    m1.make_friends(m2)
    注意,关于经典类与新式类的继承顺序:python2中经典类是按深度优先继承(D的继承顺序为先B后A,再C),新式类中为广度优先(D的继承顺序为先B后C,再A);python3中经典类与新式类都是按广度优先来继承的


    #!usr/bin/env python
    # -*- coding:utf-8 -*-

    __author__ = "Samson"

    #封装可以隐藏实现细节,使得代码模块化;继承可以拓展已存在的代码块(类);它们的目的都是为了--代码重用。而多态则是为了实现另一个目的--接口重用!多态的作用,就是为了类在继承和派生的时候,保证使用"家谱"中任一类的实例的某一属性时的正确调用,简单点就是一种接口,多种实现
    #Python不支持多态,但可以间接实现
    #通过python模拟的多态
    class Animal:
    def __init__(self,name):
    self.name = name

    def talk(self):
    raise NotImplementedError("Subclass must implement abstract method")#抛出异常

    @staticmethod
    def animal_talk(obj):
    obj.talk()

    class Cat(Animal):
    def talk(self):
    print("Meow!")

    class Dog(Animal):
    def talk(self):
    print("Woof! Woof!")

    d = Dog("wangcai")
    c = Cat("xiaohei")

    Animal.animal_talk(d)
    Animal.animal_talk(c)















  • 相关阅读:
    AtCoder Beginner Contest 205
    Codeforces Round #725 (Div. 3)
    Educational Codeforces Round 110 (Rated for Div. 2)【A
    Codeforces Round #722 (Div. 2)
    AtCoder Beginner Contest 203(Sponsored by Panasonic)
    AISing Programming Contest 2021(AtCoder Beginner Contest 202)
    PTA 520 钻石争霸赛 2021
    Educational Codeforces Round 109 (Rated for Div. 2)【ABCD】
    AtCoder Beginner Contest 200 E
    Educational Codeforces Round 108 (Rated for Div. 2)【ABCD】
  • 原文地址:https://www.cnblogs.com/cansun/p/8216091.html
Copyright © 2020-2023  润新知