wenwen
wenwen

探索網路世界和進入區塊鏈的過程或許很平凡,但我就是想分享給大家,若能幫助到有需要幫助的人,那就更好了。 有幫到你的話,記得按下方拍手圖,不需花你任何金錢,免費註冊Liker ID,就可對我化讚為賞:https://button.like.co/willie5068

014第九章 CLASSES_續2_Inheritance繼承_Import Classes

今天的感言有點少了,原因是花了不少時間去處理 markdown 語法中 code區塊 新衍生的問題(即 使用 ^ 標記 無法連結),所幸最後花了 一個多小時,終於讓我找到問題的源頭並解決 了。

💛20230114 晨讀感言:

💔學了Class 的 Inheritance(繼承)和 super() 方法 的運用,以及 Importing Classes 。而在學習 Inheritance 的同時,也讓我想到了 API 的運用 似乎也有雷同的觀念。《[什麼是 API](https://aws.amazon.com/tw/what-is/api/)》

💛晨讀摘要:

💚167 When you’re writing a new class based on an existing class, you’ll often want to call the `__init__()` method from the parent class. This will initialize any attributes that were defined in the parent `__init__()` method and make them available in the child class.

💚168 When you create a child class, the parent class must be part of the current file and must appear before the child class in the file.

💚168 At 「class ElectricCar(Car):」 we define the child class, ElectricCar. The name of the parent class must be included in parentheses in the definition of the child class.

💚168 The super() function at 「`super().__init__(parameters)`」 is a special function that helps Python make connections between the parent and child class. This line tells Python to call the `__init__()` method from ElectricCar’s parent class, which gives an ElectricCar instance all the attributes of its parent class. The name _super_ comes from a convention of calling the parent class a _superclass_ and the child class a _subclass_.

💚170 An attribute or method that could belong to any car, rather than one that’s specific to an electric car, should be added to the Car class instead of the ElectricCar class.

💚170 Overriding Methods from the Parent Class :You can override any method from the parent class that doesn’t fit what you’re trying to model with the child class. To do this, you define a method in the child class with the same name as the method you want to override in the parent class. Python will disregard the parent class method and only pay attention to the method you define in the child class.

💚170 Instances as Attributes:You’ll find that you have a growing list of attributes and methods and that your files are becoming lengthy. In these situations, you might recognize that part of one class can be written as a separate class. You can break your large class into smaller classes that work together.

💚176 Importing Multiple Classes from a Module :You import multiple classes from a module by separating each class with a comma. (例 form file_name import Class_name1, Class_name2)

💚177 Importing an Entire Module:You can also import an entire module (例 improt file_name)and then access the classes you need using dot notation. (例file_name.Class_name)

from :《python crash course》

CC BY-NC-ND 2.0

Like my work?
Don't forget to support or like, so I know you are with me..

Loading...
Loading...

Comment