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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/snailchangquan/article/details/96849802

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))

猜你喜欢

转载自blog.csdn.net/snailchangquan/article/details/96849802