C#程序设计 继承和多态实验

这里是程序设计完全小白一枚……跪求大神帮忙!!
实验目的要求见下,回答请帮忙给出完整且可以在VS05及更高版本运行的C#代码,非常感谢!满意的财富值会追加!!(如果可以请顺便看看我的另一个问题……再次跪谢!!
继承和多态实验目的:
(1)掌握不同继承方式下对基类的成员的访问的控制。(2)掌握使用虚函数实现多态性;(3)掌握运算符重载的方法。
实验内容提要:(任选两题)
(1)设计一个Person类和其派生类教师teacher,新增的属性有专业、职称和主讲课程,并为这些属性定义相应的方法。
(2)设计一个汽车类vehicle,包含的数据成员有车轮个数wheels和车重weight。小车类car是它的公有子类其中包含载人数passenger_load。卡车类truck是vehicle的公有子类其中包含载人数passenger_load和载重量payload,每个类都有相关数据的输出方法。
(3)基类Shape含有一个虚函数Area,由它派生出一个类Point类。Point类有两个数据成员x和y。正方形类从Point类派生,增加一个数据成员:边长;圆类也由Point类派生,增加一个数据成员:半径;圆柱体类从Circle类派生增加一个数据成员:高。编写程序,输出正方形的面积和圆柱体的体积。
(4)定义一个哺乳动物Mammal类,再由此派生出狗Dog类,二者都定义 Speak()成员函数,基类中定义为虚函数,定义一个Dog类的对象,调用Speak函数,观察运行结果。

第四题
namespace ConsoleApplication3
{
/// <summary>
/// 哺乳动物
/// </summary>
public class Mammal
{
public virtual void Speak()
{

}
}

/// <summary>
/// 狗狗
/// </summary>
public class Dog:Mammal
{
public override void Speak()
{
Console.WriteLine("汪、汪、汪");
}
}
}

使用:
static void Main(string[] args)
{
Mammal dog = new Dog();
dog.Speak();
Console.ReadLine();
}

第三题

namespace ConsoleApplication3
{
public class Shape
{
public virtual double Area()
{
return 0;
}
}
public class Point : Shape
{
public int X { get; set; }
public int Y { get; set; }
}
/// <summary>
/// 正方形
/// </summary>
public class Square : Point
{
/// <summary>
/// 边长
/// </summary>
public int SideLength { get; set; }
public override double Area()
{
return SideLength * SideLength;
}
}

/// <summary>
/// 圆形
/// </summary>
public class Circle : Point
{
/// <summary>
/// 半径
/// </summary>
public int Radius { get; set; }

public override double Area()
{
return Math.PI * Math.Pow(Radius, 2);
}
}

/// <summary>
/// 圆柱体
/// </summary>
public class Cylinder : Circle
{
public int Height { get; set; }
public override double Area()
{
return base.Area() * Height;
}
}
}

使用

static void Main(string[] args)
{

Square square = new Square();
Console.WriteLine("输入正方形边长按回车:");
square.SideLength = Int32.Parse(Console.ReadLine());
Console.WriteLine("正方形面积:{0}", square.Area());

Cylinder cylinder = new Cylinder();
Console.WriteLine("输入圆柱体半径按回车:");
cylinder.Radius = Int32.Parse(Console.ReadLine());
Console.WriteLine("输入圆柱体高度按回车:");
cylinder.Height = Int32.Parse(Console.ReadLine());
Console.WriteLine("圆柱体体积:{0}", cylinder.Area());
Console.ReadLine();
}追问

感谢帮忙~ 但是复制到VS里显示有错误……都是最后的那部分,不太清楚怎么改动,能麻烦您帮忙修改调试一下吗……不好意思,辛苦了……

追答

看来你是真没基础,首先,你要新建一个控制台程序

然后,新建一个类,

然后把这个类里面,namespace 保留,把class 删掉,注意{} 的对应

然后把 ///
/// 哺乳动物
///
public class Mammal
{
public virtual void Speak()
{

}
}

///
/// 狗狗
///
public class Dog:Mammal
{
public override void Speak()
{
Console.WriteLine("汪、汪、汪");
}
}

放到namespace 的{}中间,然后,到现有的Main中,把这里的main里面的内容复制进去

追问

你好…… 谢谢帮忙…… 但不介意的话可以加下QQ之类的吗……我还是有点……不太懂QAQ(2770245750

温馨提示:内容为网友见解,仅供参考
第1个回答  2015-05-21
不学习,看你以后怎么找工作。追问

选修刷学分啊……本专业和编程一点点关系都没有的……

选修刷学分啊……本专业和编程一点点关系都没有的……

追答

楼下的帮你写了,你创建控制台项目,把他代码复制进去就可以运行了

追问

并不能运行…你会C#吗…会的话能不能帮忙看一下有什么问题?

追答

少写了 Program类。

其他不变,下面的改成这样,也就是加上 class Program{ }

 class Program
 {       
        static void Main(string[] args)
        {

            Square square = new Square();
            Console.WriteLine("输入正方形边长按回车:");
            square.SideLength = Int32.Parse(Console.ReadLine());
            Console.WriteLine("正方形面积:{0}", square.Area());

            Cylinder cylinder = new Cylinder();
            Console.WriteLine("输入圆柱体半径按回车:");
            cylinder.Radius = Int32.Parse(Console.ReadLine());
            Console.WriteLine("输入圆柱体高度按回车:");
            cylinder.Height = Int32.Parse(Console.ReadLine());
            Console.WriteLine("圆柱体体积:{0}", cylinder.Area());
            Console.ReadLine();
        }
}

第2个回答  2020-11-16
public class Shape
{
public virtual void Area() { }
}
public class Point : Shape
{
public float x { get; set; }
public float y {get;set;}
}
public class Square:Point
{
public float edge { get; set; }
public override void Area()
{
float s = edge * edge;
Console.WriteLine("边长为{0}正方形面积是:{1}" ,edge, s);
}
}
public class Circle:Point
{
public float r { get; set; }
public override void Area()
{
float s = (float)3.14159 * r*r;
Console.WriteLine("半径为{0}圆面积是:{1}", r, s);
}
}
public class Cylinder:Circle
{
public float h { get; set; }
public void Volume()
{
float v = this.r * this.r * h * (float)3.1415926;
Console.WriteLine("圆柱体体积:{0}", v);
}

}
class Program
{
static void Main(string[] args)
{
Square square = new Square();
Console.WriteLine("请输入正方形的边长:");
square.edge = (float)Convert.ToDouble(Console.ReadLine());
square.Area();
Cylinder cylinder = new Cylinder();
Console.WriteLine("请输入圆柱体半径和高(用回车键分隔):");
cylinder.r= (float)Convert.ToDouble(Console.ReadLine());
cylinder.h= (float)Convert.ToDouble(Console.ReadLine());
cylinder.Volume();
Console.ReadLine();
}
}
相似回答