c#编程问题
展开全部
using System;
namespace subClassing
{
class Program
{
public static void Main(string[] args)
{
sphere sp = new sphere(10);
Console.WriteLine("球的表面积:{0}\t体积:{1}", sp.surface(), sp.volume());
cylinder cy = new cylinder(10, 8);
Console.WriteLine("圆柱的表面积:{0}\t体积:{1}", cy.surface(), cy.volume());
cone co = new cone(10, 10);
Console.WriteLine("圆锥的表面积:{0}\t体积:{1}", co.surface(), co.volume());
Console.ReadKey(true);
}
}
public class circle
{
public double radius { get; set; }
public circle() { }
public circle(double r) { radius = r; }
}
public class sphere : circle
{
public sphere() { }
public sphere(double r) : base(r) { }
public double surface() { return 4 * Math.PI * radius * radius; }
public double volume() { return surface() * radius / 3; }
}
public class cylinder : circle
{
public double height { get; set; }
public cylinder(){ }
public cylinder(double r, double h) : base(r) { height = h; }
protected double scircle() { return Math.PI * radius * radius; }
public virtual double surface() { return 2 * Math.PI * radius * height + 2 * scircle(); }
public virtual double volume() { return scircle() * height; }
}
public class cone : cylinder
{
public cone() { }
public cone(double r, double h) : base(r, h) {}
public override double surface() { return Math.PI * radius * Math.Sqrt ( radius * radius + height * height) + scircle(); }
public override double volume() { return scircle() * height / 3; }
}
}
namespace subClassing
{
class Program
{
public static void Main(string[] args)
{
sphere sp = new sphere(10);
Console.WriteLine("球的表面积:{0}\t体积:{1}", sp.surface(), sp.volume());
cylinder cy = new cylinder(10, 8);
Console.WriteLine("圆柱的表面积:{0}\t体积:{1}", cy.surface(), cy.volume());
cone co = new cone(10, 10);
Console.WriteLine("圆锥的表面积:{0}\t体积:{1}", co.surface(), co.volume());
Console.ReadKey(true);
}
}
public class circle
{
public double radius { get; set; }
public circle() { }
public circle(double r) { radius = r; }
}
public class sphere : circle
{
public sphere() { }
public sphere(double r) : base(r) { }
public double surface() { return 4 * Math.PI * radius * radius; }
public double volume() { return surface() * radius / 3; }
}
public class cylinder : circle
{
public double height { get; set; }
public cylinder(){ }
public cylinder(double r, double h) : base(r) { height = h; }
protected double scircle() { return Math.PI * radius * radius; }
public virtual double surface() { return 2 * Math.PI * radius * height + 2 * scircle(); }
public virtual double volume() { return scircle() * height; }
}
public class cone : cylinder
{
public cone() { }
public cone(double r, double h) : base(r, h) {}
public override double surface() { return Math.PI * radius * Math.Sqrt ( radius * radius + height * height) + scircle(); }
public override double volume() { return scircle() * height / 3; }
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询