/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:从PhysicalObject中派生出移动物体类MovingObject
* 作 者: 雷恒鑫
* 完成日期: 2012 年10 月18日
* 版 本 号: V1.0
* 对任务及求解方法的描述部分
* 输入描述:在其中创建物体类PhysicalObject,通过其私有字段来存放重量和体积.
* 输入描述:通过公有属性来访问其重量、体积、密度,并通过公有方法来判断该物体是否会在水中下沉。
* 输入描述:从PhysicalObject中派生出移动物体类MovingObject,在其中增加物体的速度信息,并实现物体动量和动能的计算。
* 问题描述:
* 程序输出:
* 程序头部的注释结束
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace seven_week
{
class Program
{
static void Main(string[] args)
{
PhysicalObject ph1 = new PhysicalObject();
ph1.set_weight(50);
ph1.set_volume(20);
ph1.Calculation_Density();
ph1.panduan();
PhysicalObject ph2 = new PhysicalObject(100, 50);
ph2.panduan();
MovingObject ph3 = new MovingObject (100,50,200);
Console.WriteLine("该物体的动量为{0}",ph3.energy_motion());
Console.WriteLine("该物体的动能为{0}",ph3.momentum());
Console.ReadKey();
}
}
class PhysicalObject
{
private double volume;
private double weight;
private double Density;
public PhysicalObject()
{
volume = 0;
weight = 0;
Density = 0;
}
public PhysicalObject(double volume1, double weight1)
{
volume = volume1;
weight = weight1;
Density = weight / volume;
}
public void set_volume(double volume)
{
this.volume = volume;
}
public void set_weight(double weight)
{
this.weight = weight;
}
public void set_Density(double Density)
{
this.Density = Density;
}
public double get_volume()
{
return volume;
}
public double get_weight()
{
return weight;
}
public double get_Density()
{
return Density;
}
public void Calculation_Density()
{
Density = weight / volume;
}
public void panduan()
{
if (Density > 1)
{
Console.WriteLine("物体的重量为{0}克,体积为{1}立方厘米,密度为{2}克/立方厘米,此物体会在水中下沉。", volume, weight, Density);
}
else
{
Console.WriteLine("物体的重量为{0}克,体积为{1}立方厘米,密度为{2}克/立方厘米,此物体不会在水中下沉。", volume, weight, Density);
}
}
}
class MovingObject : PhysicalObject
{
private double speech;
public MovingObject(double volume1, double weight1, double speech):base(volume1, weight1)
{
this.speech = speech;
}
public double energy_motion()
{
return ((this.get_weight()*this.speech*this.speech)/2);
}
public double momentum()
{
return (this.get_weight()*this.speech);
}
}
}
运行结果: