014第九章 CLASSES_續2_Inheritance繼承_Import Classes
💛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》