python_面向对象_类_设计模式

本文深入探讨了Python中的面向对象编程,包括类的定义、初始化、实例化、super调用基类方法,以及设计模式如简单工厂模式、工厂方法模式和单例模式的实现。通过具体的代码示例,讲解了不同模式的优缺点及其应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

01-------------类-------------------

class Person(object):
    age = 10;
    def __init__(self, age):
        if age is not None:
            self.age = age

    def speak(self):
        print("hello")

person1 = Person(30)
print(person1.age)

person2 = Person(None)
print(person2.age)

------------------new------------------------------

class Dog(object):

    def __new__(cls):
        print("new")
        return object.__new__(cls)

    def __init__(self):
        print("init")

    def speak(self):
        print("speak")

    def __del__(self):
        print("delete")

dog = Dog();
dog.speak()

---------------------------super----------------------

class Fish(object):
    def __init__(self, name):
        self.name = name

    def swim(self):
        print("{0}游泳很快".format(self.name))

    def __del__(self):
        print("delete")

class GoldFish(Fish):
    def __init__(self, name):
        super(GoldFish, self).__init__(name)

    def run(self):
        super(GoldFish, self).swim()

fish = GoldFish("金鱼")

 

2.设计模式###########简单工厂##############

#接口差异明显,分开实现操作简单
class Pay(object):
    def __init__(self, money):
        self.money = money

    def pay(self):
        print("snail金融:支付金额为{0}".format(self.money))


class WeiXin(object):
    def __init__(self, money):
        self.money = money

    def pay(self):
        print("支付金额为{0}".format(self.money))


class Zhifubao(object):
    def __init__(self, money):
        self.money = money

    def pay(self):
        print("支付金额为{0}".format(self.money))


channel = input("请选择支付方式:")
money = float(input("请输入支付金额:"))
if channel == "weixin":
    WeiXin(money).pay()
elif channel == "zhifubao":
    Zhifubao(money).pay()
else:
    Pay(money).pay()

###################工厂方法######################

优势:不需要去关注具体类是如何实现的,用的时候导入类即可。

class Pay(object):
    def __init__(self, money):
        self.money = money

    def pay(self):
        print("snail金融:支付金额为{0}".format(self.money))


class WeiXin(object):
    def __init__(self, money):
        self.money = money

    def pay(self):
        print("微信支付金额为{0}".format(self.money))


class Zhifubao(object):
    def __init__(self, money):
        self.money = money

    def pay(self):
        print("支付宝支付金额为{0}".format(self.money))


class PayFactory(object):
    def create(self, money):
        return Pay(money)


class WeiXinFactory(object):
    def create(self, money):
        return WeiXin(money)


class ZhifubaoFactory(object):
    def create(self, money):
        return Zhifubao(money)

------

from method import PayFactory
factory = PayFactory()
pay = factory.create(100)
pay.pay()

from method import WeiXinFactory
factory = WeiXinFactory()
pay = factory.create(1000)
pay.pay()


from method import ZhifubaoFactory
factory = ZhifubaoFactory()
pay = factory.create(10000)
pay.pay()

------反射抽象工厂

model =  input("请输入模块名称")
classA  = input("请输入类名称")
money = input("请输入钱数")

model = __import__(model)
object = getattr(model, classA)(money)
print(dir(model))
print(object)
object.pay()

==============单例模式==============

class SingleTone(object):
    def __new__(cls, *args, **kw):
        if not hasattr(cls, '_instance'):
            cls._instance = super(SingleTone, cls).__new__(cls, )
        return cls._instance

single1 =  SingleTone()
single2 = SingleTone()
print(id(single1))
print(id(single2))

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值