python之類的繼承及方法講解分析


#!/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 webdriver
from appium import webdriver
from selenuim.webdriver.support.expected_conditions import NoSuchElementException
from selenium.webdriver.common.by import By

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM