用程序的思想思考物理问题
用类定义物理实体的思想,来思考如何把编程和教学结合起来,让教学更轻松,让学习更容易。
# !/usr/bin/env python
# -*-coding:gbk -*-
"""
# File :pratise_one.py
# Time :2022/10/11 22:01
# Author : Detian_teacher
# Description class Templates
"""
class Liquid:
def __init__(self,rou,v,):
self.rou = rou
self.v = v
@property
def get_m(self):
'''
获得液体质量方法
:return:
'''
return f'液体质量为{self.rou * self.v}g,合{(self.rou * self.v)/1000}kg'
def __str__(self):
return f'密度为{self.rou}的液体,体积为{self.v}m3,它的质量为{self.get_m}'
water = Liquid(1.0,5)
class Solid:
'''固体类'''
def __init__(self,long,width,height,rou):
self.long = long
self.width = width
self.height = height
self.rou = rou
@property
def get_v(self):
return self.long * self.width * self.height
@property
def get_m(self):
return self.rou * self.get_v
def __str__(self):
return f'长为{self.long}cm,宽为{self.width}cm,高为{self.height}cm固体密度为{self.rou},体积为{self.get_v}cm3,固体的质量为{self.get_m}g,合{self.get_m/1000}kg'
iron_rect = Solid(20,10,30,7.9) #实例化一个铁块
print(iron_rect) #实例化一个铁块,并输入其质量
print(water)