日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

java与python多态的区别_如果未调用父构造函数(与Java不同),多态性在Python中如何工作? - java...

發布時間:2023/12/2 python 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java与python多态的区别_如果未调用父构造函数(与Java不同),多态性在Python中如何工作? - java... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

因此,父類構造函數是在Java中調用的,而在Python中則不是。如果這意味著未創建父對象,那么如何在Python中成功調用def function-這是怎么回事?

Python代碼

class Parent:

def __new__(self):

print(f"I am the real parent constructor Hahahah {self}")

return object.__new__(self)

def __init__(self):

print(f"I am the constructor of parent {self}")

def function(self):

print(f"I am parent's member function and my self value is {self}")

def over(self):

print(f"I am parent's O-function and my self value is {self}")

class Child(Parent):

def __new__(self):

print(f"I am the real chid constructor Hahahah {self}")

return object.__new__(self)

def __init__(self):

print(f"I am the initialize of child {self}")

def over(self):

print(f"I am the child's member O-function and my self value is {self}")

ch = Child()

ch.over()

ch.function()

以上Python代碼的輸出。

注意:沒有打印I am the real parent constructor Hahahah。

I am the real chid constructor Hahahah

I am the initialize of child <__main__.Child object at 0x7f4bb5d997b8>

I am the child's member O-function and my self value is <__main__.Child object at 0x7f4bb5d997b8>

I am parent's member function and my self value is <__main__.Child object at 0x7f4bb5d997b8>

類似的Java代碼

public class main {

public static void main(String[] args) {

Child ch = new Child();

ch.over();

ch.function();

}

}

class Parent {

Parent () {

System.out.println("In the parent class constructor | " + this);

}

public void function () {

System.out.println("In the member function of parent | " + this);

}

public void over () {

System.out.println("In the member O-function of parent | " + this);

}

}

class Child extends Parent {

Child () {

System.out.println("I the child class constructor | " + this);

}

public void over () {

System.out.println("In the member O-function of chlid | " + this);

}

}

上面的Java代碼的輸出

In the parent class constructor | code.Child@2a139a55

I the child class constructor | code.Child@2a139a55

In the member O-function of chlid | code.Child@2a139a55

In the member function of parent | code.Child@2a139a55

參考方案

Python和Java不同。

在Java中,擴展類必須始終調用父構造函數。為了使生活更輕松,如果父構造函數沒有參數,則會首先自動調用它。現在,如果要像這樣向Java Parent構造函數添加一個參數:

Parent (int i) {

System.out.println("In the parent class constructor | " + this);

}

您會發現以下編譯錯誤:

There is no default constructor available in 'org.example.Parent'

這很有意義,當構造Child Java時不知道作為值i傳遞什么。因此,我們必須手動調用Parent構造函數:

Child () {

super(1);

System.out.println("I the child class constructor | " + this);

}

Python的不太嚴格。始終調用父構造函數仍然是一個好習慣,但是Python不需要您這樣做。這是因為Python不是type safe。

現在讓我們看看如果忘記調用父構造函數會發生什么:

class Parent:

def __init__(self):

self.i = 1

print(f"I am the constructor of parent {self}")

def printi(self):

print(self.i)

class Child(Parent):

def __init__(self):

pass

ch = Child()

ch.printi()

將會發生致命錯誤:

Traceback (most recent call last):

File "test.py", line 15, in

ch.printi()

File "test.py", line 7, in printi

print(self.i)

AttributeError: 'Child' object has no attribute 'i'

要修復此代碼,您可以將以下行添加到Child init函數:

Parent.__init__(self)

如您所見,仍然創建了Parent類(因為您可以調用function方法。這再次是因為Python不太嚴格。In python an object can be created without calling the constructor.

Java-父類正在從子類中調用方法? - java

抱歉,我還是編碼的新手,可能還沒有掌握所有術語。希望您仍然能理解我的問題。我想得到的輸出是:"Cost for Parent is: 77.77" "Cost for Child is: 33.33" 但是,我得到這個:"Cost for Parent is: 33.33" "Cost f…Java-搜索字符串數組中的字符串 - java

在Java中,我們是否有任何方法可以發現特定字符串是字符串數組的一部分。我可以避免出現一個循環。例如String [] array = {"AA","BB","CC" }; string x = "BB" 我想要一個if (some condition to tell wheth…Java Scanner讀取文件的奇怪行為 - java

因此,在使用Scanner類從文件讀取內容時,我遇到了一個有趣的問題。基本上,我試圖從目錄中讀取解析應用程序生成的多個輸出文件,以計算一些準確性指標。基本上,我的代碼只是遍歷目錄中的每個文件,并使用掃描儀將其打開以處理內容。無論出于何種原因,掃描程序都不會讀取其中的一些文件(所有UTF-8編碼)。即使文件不是空的,scanner.hasNextLine()在…Java Globbing模式以匹配目錄和文件 - java

我正在使用遞歸函數遍歷根目錄下的文件。我只想提取*.txt文件,但不想排除目錄。現在,我的代碼如下所示:val stream = Files.newDirectoryStream(head, "*.txt") 但是這樣做將不會匹配任何目錄,并且返回的iterator()是False。我使用的是Mac,所以我不想包含的噪音文件是.DS_ST…直接讀取Zip文件中的文件-Java - java

我的情況是我有一個包含一些文件(txt,png,...)的zip文件,我想直接按它們的名稱讀取它,我已經測試了以下代碼,但沒有結果(NullPointerExcepion):InputStream in = Main.class.getResourceAsStream("/resouces/zipfile/test.txt"); Buff…

總結

以上是生活随笔為你收集整理的java与python多态的区别_如果未调用父构造函数(与Java不同),多态性在Python中如何工作? - java...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。