python类学习
创建关于汽车的类
class Cars(): def __init__(self, brand, country):
self.brand = brand
self.country = country def show_brand(self):
print(self.brand.title() + " is a popular automobile brand.") def show_country(self):
print("Its headquater is in " + self.country.title() + ".") my_car = Cars('toyota', 'Japan')
my_car.show_brand()
my_car.show_country()
输出
Toyota is a popular automobile brand.
Its headquater is in Japan.
列子
class Restaurant():
def __init__(self, restaurant_name, cuisine_type):
self.restaurant_name = restaurant_name
self.cuisine = cuisine_type def describe_restaurant(self):
print("There is a restaurant called " + self.restaurant_name.title() + ", its cuisine is " + self.cuisine.title() + ".") def open_restaurant(self):
print("The restaurant " + self.restaurant_name.title() + " is opening...") restaurant = Restaurant('xianghui xin', 'xiang cuisine')
restaurant.describe_restaurant()
print("I like " + restaurant.restaurant_name.title() + '.')
restaurant.open_restaurant()
print("Let's go " + restaurant.restaurant_name.title() + " and have lunch.")
输出
There is a restaurant called Xianghui Xin, its cuisine is Xiang Cuisine.
I like Xianghui Xin.
The restaurant Xianghui Xin is opening...
Let's go Xianghui Xin and have lunch.
类的默认属性值和修改方法1
class Car():
def __init__(self, make, model, year):
"""初始化汽车描述属性"""
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0 def read_odometer(self):
print("My car is " + self.make.title() + " " + self.model.title() + ", production date is " + str(self.year) + ".")
print("This car has " + str(self.odometer_reading) + "KM mileage on it.") my_new_car = Car('audi', 'a8l', 2019)
my_new_car.read_odometer() my_new_car.odometer_reading = 1000
my_new_car.read_odometer()
输出
My car is Audi A8L, production date is 2019.
This car has 0KM mileage on it.
My car is Audi A8L, production date is 2019.
This car has 1000KM mileage on it.
修改方法2
class Car():
"""一次模拟汽车的简单尝试"""
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0 def get_descriptive_name(self):
long_name = str(self.year) + ' ' + self.make + ' ' + self.model
return long_name.title() def read_odometer(self):
print("This car has" + " " + str(self.odometer_reading) + "KM mileage on it.") def update_odometer(self, mileage):
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer !") def increment_odometer(self, miles):
self.odometer_reading += miles my_new_car = Car('audi', 'a8l', 2019)
get_descriptive_name = my_new_car.get_descriptive_name()
print(get_descriptive_name)
my_new_car.read_odometer()
my_new_car.update_odometer(100)
my_new_car.read_odometer()
my_new_car.update_odometer(90)
my_new_car.increment_odometer(10)
my_new_car.read_odometer()
输出
2019 Audi A8L
This car has 0KM mileage on it.
This car has 100KM mileage on it.
You can't roll back an odometer !
This car has 110KM mileage on it.
父类和子类
class Car():
"""传统汽车的属性"""
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0 def get_descriptive_name(self):
long_name = str(self.year) + ' ' + self.make + ' ' + self.model
return long_name.title() def read_odometer(self):
print("This car has" + " " + str(self.odometer_reading) + "KM mileage on it.") def update_odometer(self, mileage):
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer !") def increment_odometer(self, miles):
self.odometer_reading += miles class ElectricCar(Car):
"""电动汽车的独特属性"""
def __init__(self, make, model, year):
super().__init__(make, model, year)
self.battery = 70 def describe_battery(self):
print("This car has a " + str(self.battery) + "-kWh battery.") my_car = Car('audi', 'a8l', 2019)
get_my_car = my_car.get_descriptive_name()
print(get_my_car)
my_car.read_odometer() my_tesla = ElectricCar('tesla', 'mode s', 2019)
get_my_tesla = my_tesla.get_descriptive_name()
print(get_my_tesla)
my_tesla.describe_battery()
输出
2019 Audi A8L
This car has 0KM mileage on it.
2019 Tesla Mode S
This car has a 70-kWh battery.
python类学习的更多相关文章
- python类学习以及mro--多继承属性查找机制
版权声明:本文为博主原创文章,未经博主允许不得转载. 还记得什么是新式类和旧式类吗? Python中,一个class继承于object,或其bases class里面任意一个继承于object,这个c ...
- Python 类学习的一些Tips
这里不详细介绍类,只总结一些小萌新在学习python 类时会有的一些疑点. 类的私有性 在python中,属性和方法的访问权限只有两种,公开的,和私有的.在给属性命名时用两个“__”下划线作为开头,就 ...
- python类,魔术方法等学习&&部分ssti常见操作知识点复习加深
python类学习&&部分ssti常见操作知识点复习加深 在做ssti的模块注入的时候经常觉得自己python基础的薄弱,来学习一下,其实还是要多练习多背. 在python中所有类默认 ...
- python入门学习:8.类
python入门学习:8.类 关键点:类 8.1 创建和使用类8.2 使用类和实例8.3 继承8.4 导入类 8.1 创建和使用类 面向对象编程是最有效的软件编写方法之一.在面向对象编程中,你编写 ...
- Python学习笔记之面向对象编程(三)Python类的魔术方法
python类中有一些方法前后都有两个下划线,这类函数统称为魔术方法.这些方法有特殊的用途,有的不需要我们自己定义,有的则通过一些简单的定义可以实现比较神奇的功能 我主要把它们分为三个部分,下文也是分 ...
- 学习python类
类:Python 类提供了面向对象编程的所有基本特征: 允许多继承的类继承机制, 派生类可以重写它父类的任何方法, 一个方法可以调用父类中重名的方法. 对象可以包含任意数量和类型的数据成员. 作为模块 ...
- 60分钟Python快速学习(给发哥一个交代)
60分钟Python快速学习 之前和同事谈到Python,每次下班后跑步都是在听他说,例如Python属于“胶水语言啦”,属于“解释型语言啦!”,是“面向对象的语言啦!”,另外没有数据类型,逻辑全靠空 ...
- !!对python列表学习整理列表及数组详细介绍
1.Python的数组分三种类型:(详细见 http://blog.sina.com.cn/s/blog_6b783cbd0100q2ba.html) (1) list 普通的链表,初始化后可以通过特 ...
- Python Tutorial 学习(八)--Errors and Exceptions
Python Tutorial 学习(八)--Errors and Exceptions恢复 Errors and Exceptions 错误与异常 此前,我们还没有开始着眼于错误信息.不过如果你是一 ...
随机推荐
- MFC的静态链接、动态链接
项目属性页面可以查看更改编译方式,推荐使用静态编译.也可以在创建MFC项目时选择静态编译. 标准Windows库,使用的是系统API,Win32是面向API的编程平台.Win32项目使用的是此编译方式 ...
- Unknown property 'mybatis-plus' yml文件报错
就是因为没有对应的依赖 package com.taotao.config; import org.mybatis.spring.annotation.MapperScan; import org.s ...
- .net实现浏览器大文件分片上传
以ASP.NET Core WebAPI 作后端 API ,用 Vue 构建前端页面,用 Axios 从前端访问后端 API ,包括文件的上传和下载. 准备文件上传的API #region 文件上传 ...
- Crash的数字表格 / JZPTAB
https://www.cnblogs.com/peng-ym/p/8666124.html #include<bits/stdc++.h> #define LL long long #d ...
- java学习第一天:环境的配置
1.下载JDK,当前版本下载地址为:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.htm ...
- HDU 1512 Monkey King(左偏堆)
爱争吵的猴子 ★★☆ 输入文件:monkeyk.in 输出文件:monkeyk.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] 在一个森林里,住着N只好斗的猴子.开始,他们各 ...
- python 鼠标输入
import win32con import win32api import time win32api.SetCursorPos([,]) # 设置鼠标位置 time.sleep(0.1) #win ...
- 线程的interrupt()
官网解释 If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) method ...
- JavaWeb_(Mybatis框架)使用Mybatis对表进行增、删、改、查操作_二
系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...
- shell练习1
题目 把ls -l 的输出按照属主分类,打印每个属住的文件名 ls -l |sed -n '2,$p'| awk ' {hash[$]=hash[$]} END{ for (user in hash) ...