admin管理员组

文章数量:1794759

Python语法之类

Python语法之类

创建和使用类

类=属性+动作

class Dog: def __init__(self, dog_name, dog_age): self.name = dog_name self.age = dog_age def sit(self): print(self.name.title() + " is sitting now. It is " + str(self.age) + " years old.") if __name__ == "__main__": my_dog = Dog('Harry', 12) my_dog.sit() the __init__() Method The __init__() method at w is a special method Python runs automatically whenever we create a new instance based on the Dog class. 参数self When Python calls this __init__() method later (to create an instance of Dog), the method call will automatically pass the self argument.

self在类的定义语句中,起一个穿针引线的作用。程序中实参通过形参dog_name, dog_age借助“马甲”self.name, self.age“游走”于类的定义语句中。

It gives the individual instance access to the attributes and methods in the class. When we make an instance of Dog, Python will call the __init__() method from the Dog class. We’ll pass Dog() a name and an age as arguments; self is passed automatically, so we don’t need to pass it. Making an Instance from a Class

The class Dog is a set of instructions that tells Python how to make individual instances representing specific dogs.

my_dog = Dog('Harry', 12) When Python reads this line, it calls the __init__() method in Dog with the arguments 'Harry' and 12. The __init__() method creates an instance representing this particular dog and sets the name and age attributes using the values we provided. The __init__() method has no explicit return statement, but Python automatically returns an instance representing this dog. Working with Classes and Instances class Car: def __init__(self, car_make, car_model, car_year): self.make = car_make self.model = car_model self.year = car_year def get_descriptive_name(self): full_name = str(self.year) + ' ' + self.make + ' ' + self.model return full_name if __name__ == "__main__": my_car = Car('audi', 'a4', 2016) print(my_car.get_descriptive_name()) Modifying an Attribute’s Value Through a Method class Car: def __init__(self, car_make, car_model, car_year): self.make = car_make self.model = car_model self.year = car_year self.odometer_reading = 0 def get_descriptive_name(self): full_name = str(self.year) + ' ' + self.make + ' ' + self.model return full_name def update_odometer(self, car_mileage): self.odometer_reading = car_mileage def read_odometer(self): print("This car has " + str(self.odometer_reading) + " miles on it.") if __name__ == "__main__": my_car = Car('audi', 'a4', 2016) # print(my_car.get_descriptive_name()) my_car.update_odometer(111) my_car.read_odometer() Inheritance When one class inherits from another, it automatically takes on all the attributes and methods of the first class. The original class is called the parent class, and the new class is the child class. The child class inherits every attribute and method from its parent class but is also free to define new attributes and methods of its own. The __init__() Method for a Child Class from car import Car class ElectricCar(Car): def __init__(self, car_make, car_model, car_year): super().__init__(car_make, car_model, car_year) if __name__ == "__main__": my_electricCar = ElectricCar('tesla', 'model s', 2016) print(my_electricCar.get_descriptive_name()) The super() function 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.

本文标签: 语法Python