#!/usr/bin/env python#-*- conding:utf-8 -*-# class Person(object):# def __init__(self,name,age):# self.name = name# self.age = age## def getName(self):# return self.name## def getAge(self):# return self.age## def setName(self, name):# self.name=name## def setAge(self,age):# self.age=age## def info(self):# return "name{0},age{1}".format(self.name,self.age)# per = Person("Teacher",24)# per.info()# print(per.info())"""首先一個類,無論是否寫了構造函數,它都是有構造函數的一個類,能夠有多個構造函數,建議一個類只有一個構造函數構造函數1.初始化屬性"""# class Person1(object):# #動態方法# def __init__(self,*args,**kwargs):# self.args = args# self.kwargs = kwargs## def info(self):# # print("信息:",self.kwargs)# print("信息:",self.args)## per1 = Person1(name="Teacher",age=24)# per1 = Person1("Teacher",26)# per1.info()# per1.info()"""析構函數對象實例化-->構造函數-->對象調用方法-->代碼跳轉到具體的方法-->執行方法的代碼塊-->最後執行析構函數"""# class Person(object):# def __init__(self):# print("我是構造方法")## def __del__(self):# print("我是析構方法")## def info(self):# print("我是方法")## per = Person()# per.info()#"""普通方法"""# class Person2(object):# def conn(self,user,passwd,host,port):# pass## def f1(self,*args,**kwargs):# # self.kwargs = kwargs# # self.args = args# pass## def info(self):# print("我是普通方法")## per = Person2()# per.conn("root",123456,"localhost",3306)# per.info()"""特性方法:不能有形式參數"""# class Person(object):## @property #裝飾器# def getUserID(self):# pass## per = Person()# per.getUserID #不加(),加()報錯# from selenium import webdriver# driver = webdriver.Firefox()# driver.find_element_by_id("kw").text"""靜態方法:能夠直接使用類名來調用方法,屬於類實例對象也能夠調用方法,但不建議使用,畫蛇添足!"""# class MySQL(object):# @staticmethod# # 方法參數不用加self# def conn(user):# pass## # MySQL.conn("Teacher")# sql = MySQL()# sql.conn("Teacher")"""類的方法:直接使用類名來調用"""# class MySQL(object):# @classmethod# # 方法參數不用加self# def conn(cls):# pass## # MySQL.conn("Teacher")# MySQL.conn()"""屬於類: 類屬性 靜態方法 類方法屬於對象: 實例屬性 普通方法 特性方法""""""類的繼承:重複使用已經存在的數據和行爲,減小重複編寫代碼,子類繼承父類的實例屬性和方法""""""類屬性的繼承"""# class Person(object):# China = "中國"# class UsaPerson(Person):# pass# Usa = UsaPerson()# Usa.China# print(Usa.China)"""實例屬性的繼承與繼承的兩種寫法"""# class Fruit(object):# def __init__(self,name):# self.name = name"""子類因爲業務的需求,須要繼承父類的實例屬性"""# class Apple(Fruit):# def __init__(self,name,brand,color):# # super(Apple,self).__init__(name)# Fruit.__init__(self,name)# self.brand = brand# self.color = color## def info(self):# return "名稱{0},品牌{1},顏色{2}".format(self.name,self.brand,self.color)## app = Apple("蘋果","富士","紅色")# app.info()# print(app.info())# class Fruit(object):# def __init__(self,name):# self.name = name"""子類因爲業務的需求,不須要繼承父類的實例屬性"""# class Apple(Fruit):# def __init__(self,brand,color):# self.brand = brand# self.color = color## def info(self):# return "品牌{0},顏色{1}".format(self.brand,self.color)## app = Apple("富士#","紅色")# app.info()# print(app.info())"""格式化字符"""#"{a} Love {b}.{0}".format(a = "I",b = "You","com")"{0} Love {a}.{b}".format("I",a = "You",b = "com")"""方法的繼承:子類爲何要重寫父類的方法?子類有本身的特性當子類重寫了父類的方法,對子類進行實例化對象後,子類調用的(父類,子類方法都存在)方法,執行的方法是子類的方法"""# class Person(object):# def eat(self):# print("人須要吃飯的")## class Son(Person):# def __init__(self,name):# self.name = name## def eat(self):# print("名字是{0},爲何?".format(self.name))## son = Son("Teacher")# son.eat()"""單個類繼承的原則:1.從上到下,子類繼承父類,但沒有重寫父類的方法,對子類進行實例化對象後,執行調用是直接父類中的方法2.從下到上,子類繼承父類,但子類重寫父類的方法,對子類進行實例化對象後,執行調用是直接子類中的方法(優先調用本身方法)"""# class Fruit(object):# def eat(self):# print("水果是用來吃的")## class Apple(Fruit):# def __init__(self,color):# self.color = color## def eat(self):# print("蘋果的顏色{0},該吃掉了!".format(self.color))## class Band(Apple):# def eat(self):# print("我是Apple的子類")## band = Band("紅色")# band.eat()# class Person(object):# def __init__(self,name):# self.name=name## def info(self):# print (self.name)## class Son(Person):# def info(self):# print (self.name)## s=Son('name')# s.info()#"""多個繼承:執行的順序,從左到右執行;而且是同一級別的!同一級別指的是共同的類"""# class Person(object):# def eat(self):# print("人是吃飯的")## class Monther(Person):# # def eat(self):# # print("媽媽不吃飯,要減肥")# pass## class Father(Person):# def eat(self):# print("爸爸吃飯!")## class Son(Monther,Father):# pass## son = Son()# son.eat()"""__doc__ 打印出類的註釋"""# class Person(object):# """人的屬性&特性"""# def info(self,username,password):# """# :param username: 參數用戶名# :param password: 參數密碼# :return:# """# pass## per = Person()# print(per.__doc__)"""__call__:對象建立時直接返回__call__的內容,使用該方法能夠模擬靜態方法"""# class Per(object):# def __new__(cls, *args, **kwargs):# print("打印出call方法")## per = Per()"""__str__:對象表明的含義,返回一個字符串,經過他能夠把字符串和對象關聯起來,方便某些程序的實現該字符串表示某個類,實現__str__後,能夠直接使用print語句打印出對象,也能夠經過str來觸發__str__來執行__str__:1.對象的意思2.返回一個字符串,對象和字符串關聯起來 -->該字符串可表示一個類"""# class Per(object):# """我是一個字符串類"""# def __str__(self):# return self.__doc__## per = Per()# print(str(per))class Factory(object): def createFruit(self,fruit): if fruit == "apple": return Apple() elif fruit == "banana": return Banana()class Fruit(object): def __str__(self): return "fruit"class Apple(object): def __str__(self): return "apple"class Banana(object): def __str__(self): return "banana"if __name__ == '__main__': factory = Factory() print(factory.createFruit("apple")) print(factory.createFruit("banana"))"""工廠設計模式在UI中的應用"""from selenium import webdriverfrom appium import webdriverfrom selenuim.webdriver.support.expected_conditions import NoSuchElementExceptionfrom selenium.webdriver.common.by import Byclass Factory(object): def createWebDriver(self,WebDriver): if WebDriver == "web": return WebUI(self.driver) elif WebDriver == "app": return AppUI(self.driver)class WebDriver(object): def __init__(self,webdriver): self.webdriver = webdriver def __str__(self): return "WebDriver" def findElement(self,*loc): try: return self.driver_find_element_By(*loc) except NoSuchElementException as e: print("Error details:%s",e.args[0]) def findElements(self,*loc): try: return self.driver_find_element_By(*loc) except NoSuchElementException as e: print("Error details:%s",e.args[0])class WebUI(WebDriver): def __str__(self): return "web"class AppUI(WebDriver): def __str__(self): return "app"if __name__ == '__main__': factory = Factory() print(factory.createFruit("web")) print(factory.createFruit("app"))