Python 面向對象(中)

在這里插入圖片描述
在python中面向對象的三大特征:
封裝,繼承,多態

1. 析構方法

程序結束后,之后調用析構方法,來釋放空間

def __del__(self):print("析構方法")

2.單繼承

子類繼承父類

class animal():def eat(self):print('吃')class dog(animal):#繼承父類def wwj(self):print('dog')d=dog()
d.eat()

3.多繼承

class animal():def eat(self):print('吃')
class fourleg():def out(self):print('四條腿')class dog(animal,fourleg):#繼承父類def wwj(self):print('dog')d=dog()
d.eat()
d.out()
吃
四條腿

重寫就是在子類中的方法,會覆蓋父類的方法

4.多態

對不同的子類對象有不同的行為表現
要想實現多態必須有兩個前提:
1.繼承:必須存在繼承關系,發生在父類和子類之間
2.重寫:子類需要重寫父類的方法

class animal():def say_who(self):print('我是一個動物')class duck(animal):def say_who(self):print('我是一個鴨子')class dog(animal):def say_who(self):print('我是一個小貓')
duck1=duck()
duck1.say_who()
dog1=dog()
dog1.say_who()
我是一個鴨子
我是一個小貓
def commoninovke(obj):obj.say_who()
li=[duck(),dog()]
for item in li:commoninovke(item)
我是一個鴨子
我是一個小貓

5 類屬性和實力屬性

class student:name='黎明'  #類屬性def __init__(self,age):  #實例屬性self.age=agelm=student(18)
print(lm.name) #通過實例對象訪問類屬性
print(lm.age)
print(student.name) #通過類對象訪問類屬性
print(student.age)
黎明
18
黎明
Traceback (most recent call last):File "D:/index.py", line 190, in <module>print(student.age)
AttributeError: type object 'student' has no attribute 'age'

在這里插入圖片描述

6.類方法和實例方法


class people:country='china'@classmethoddef get_country(cls):return cls.country # 訪問類屬性@staticmethoddef getData():return people.country#類方法
print(people.get_country()) #通過類對象調用
print(people.country)
p=people()
print(p.get_country()) # 通過實例對象訪問
people.country='chinachina'
print(p.country)# 靜態方法
print(p.getData())
china
china
china
chinachina
chinachina

靜態方法中不涉及到類中方法和屬性的操作
數據資源能夠得到有效的利用

在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/535264.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/535264.shtml
英文地址,請注明出處:http://en.pswp.cn/news/535264.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Python 面向對象 實例方法 屬性 (上)

1.面對對象和面對過程的區別 面向對象&#xff1a; 將數據與函數綁定到一起&#xff0c;進行封裝&#xff0c;這樣能夠跟快的開發程序&#xff0c;減少代碼的重復書寫。 函數式&#xff1a; 包裝為一個函數&#xff0c;然后調用 **面向過程&#xff1a;**根據業務邏輯從上到下…

Python高級數據類型-字符串,列表,元組,字典

0.序列 序列&#xff1a;在python中 序列就是一組按照順序排列的值 python中三種內資的序列類型&#xff1a;字符串&#xff0c;列表&#xff0c;元組 優點&#xff1a;支持索引和切片的操作&#xff0c;從0開始 1.字符串 可以查詢&#xff0c;修改 字符串常用的方法 ahe…

Python 判斷語句 if else

判斷語句的三種結構 1.if if a>10:print(a)2. if -else if a >10:print(a) else:print(a1)3. if -elif if a >10:print(a) elif:print(a1) elif:print(a2)

Juypter 代碼自動補全

1.安裝 pip install jupyter_contrib_nbextensions2. 配置 安裝完之后需要配置 nbextension&#xff0c;注意配置的時候要確保已關閉 Jupyter Notebook&#xff1a; jupyter contrib nbextension install --user --skip-running-check3.啟動 Jupyter Notebook 勾選Hinderlan…

Pytorch Anaconda 安裝CPU版本

進入官網 https://pytorch.org/get-started/locally/ 然后打開 anaconda Prompt 輸入 conda install pytorch torchvision cpuonly -c pytorch 即可

super(Net,self).__init__() 的含義

class Net(nn.Module):def __init__(self):super(Net,self).__init__()python中的super(Net, self).init() 首先找到Net的父類&#xff08;比如是類nn.Module&#xff09;&#xff0c;然后把類Net的對象self轉換為類nn.Module的對象&#xff0c;然后“被轉換”的類nn.Module對…

C++ 定義 string

定義字符串 //1 string str;//2 char *str1new char[s.length()1];//定義指定長度的字符數組&#xff0c;然后轉換為string strstring(str1)

神經網絡加載數據 自建數據集 官方數據集 pytorch 顯示數據集

1.官方的數據集 MNIST 使用torchvision.datasets 里面有很多數據集供選擇 import torch import torchvision from torchvision import transforms, models batch_size 32 transform transforms.Compose([transforms.ToTensor(),transforms.Normalize(mean(0.5),std(0.5)),…

Juypter 打開其他路徑文件

juypter 打開默認的C盤路徑 假如我們想要進入其他盤&#xff08;D,E,F&#xff09;時&#xff0c;我們需要進入命令行&#xff0c;然后cd 到其他盤&#xff0c;再在命令行中運行juypter 過程如下 >>>cd D:/train >>>juypter notebook如果顯示jupyter noteboo…

離線安裝 Pytorch 1.2.0 torchvision 0.3.0

1.進入官網 https://download.pytorch.org/whl/torch_stable.html 選擇自己合適的下載下來&#xff0c;然后把下載下來的文件 放入服務器的指定文件夾中 2.cd 到指定文件夾 運行 pip3 install torch-1.0.0-cp36-cp36m-linux_x86_64.whlpip install torchvision-0.2.0-py2.…

Python報錯:PermissionError: [Errno 13] Permission denied 解決方案詳解

出現錯誤的原因&#xff1a; 1.文件找不到&#xff0c; 2.文件被占用&#xff0c; 3.文件無權限訪問&#xff0c; 4.打開的不是文件&#xff0c;而是一個目錄&#xff0c; 我就是這個問題&#xff0c;打開的是一個目錄

pytorch 查看參數是否被訓練 require_grad()

遍歷named_parameters()中的所有的參數&#xff0c;只打印那些param.requires_gradTrue的變量。 for name, param in model.named_parameters():if param.requires_grad:print(name)

Ubuntu18.0.1 安裝 anaconda conda cudnn pytorch-gpu

安裝顯卡驅動 查看驅動版本 cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2 安裝anaconda 下載&#xff1a; 獲取anaconda在清華鏡像站的網址&#xff0c;然后在服務器端wget 網址就行了。 清華鏡像站中anaconda的所有版本的網址&#xff1a;https://mirror…

win10 利用安裝包 安裝 ssh

下載ssh https://github.com/PowerShell/Win32-OpenSSH/releases 然后把它放入 C:\Program Files 中 添加環境變量 高級系統設置-》高級-》環境變量-》 是用戶變量 不是系統變量 選擇 path- 然后點擊編輯&#xff0c;然后新建&#xff0c; 把剛才的地址放進去 C:\Program F…

Latex 表格 行合并,列合并,控制行間距 單元格寬度

多行合并的時候&#xff0c;需要加入新的usepackage, 不然會報錯 \usepackage{multirow}1.多行合并 \linespread{1.3} %控制行間距 \begin{table*}[t]\centering\caption{Experimental arrangement}\begin{tabular}{p{4.1cm}|l|l} %控制單元格的寬度\hlineSection & con…

win10 使用ssh連接服務器 ‘’Bad owner or permissions on C:\\Users\\Administrator/.ssh/config‘’

這個時候只需要把這個文件夾中的config 文件刪除就行了 因為可能在vscode 中使用config文件導致的不兼容&#xff0c;只需要把config文件刪除就行了

Linux Unbunt 安裝顯卡驅動 簡單方法

1.卸載之前驅動 sudo apt-get --purge remove nvidia-* sudo apt-get --purge remove xserver-xorg-video-nouveau2. 重啟服務器 sudo shutdown 3. 在官網下載驅動 官網下載驅動 然后放大服務器的指定文件夾中 4 .關閉X-service 最好遠程連接服務器安裝&#xff0c;因為要…

Ubuntu 查看磁盤空間 及目錄容量

Df命令是linux系統以磁盤分區為單位查看文件系統&#xff0c;可以加上參數查看磁盤剩余空間&#xff1a; df -hl 顯示格式為&#xff1a; 文件系統 容量 已用 可用 已用% 掛載點 以上面的輸出為例&#xff0c;表示的意思為&#xff1a; HD硬盤接口的第二個硬盤&#xff08;…

Latex 調整表格大小 表格過大 表格過小

1.表格過大 超出了邊界 \resize{width}{length}{text}調節表格大小 \begin{table*}[h]\centering\small\caption{The face identity evaluation based on Face API}\label{tab:The evaluation based on Face API}\resizebox{\textwidth}{9mm}{\begin{tabular}{c c c c c c c…