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.




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值