C#类的多态性:求图形的面积
来源:互联网 发布:shopnc java 源码 编辑:程序博客网 时间:2024/06/10 14:21
1)、代码:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace shap{ public class shap { public virtual void MyArea() { Console.WriteLine("no use"); } } public class circle : shap { double r, carea; public circle(double r) { this.r = r; } public override void MyArea() { carea = Math.PI * r * r; Console.WriteLine("该圆形的面积为{0}", carea); } } public class triangle : shap { double tarea, hemiline, h; public triangle(double hemiline, double h) { this.hemiline = hemiline; this.h = h; } public override void MyArea() { tarea = hemiline * h / 2; Console.WriteLine("hemiline={0},h={1}", hemiline, h); Console.WriteLine("该圆形的面积为{0}", tarea); } } class Program { static void Main(string[] args) { for (int i = 0; i < 100; i++) { Console.WriteLine("请输入你选择你要计算的图形类型:"); Console.WriteLine("1.circle(圆形面积)"); Console.WriteLine("2.triangle(三角形的面积)"); int s1 = Convert.ToInt32(Console.ReadLine()); switch (s1) { case 1: Console.WriteLine("请输入圆的半径r:"); int r; r = Int32.Parse(Console.ReadLine()); shap MyShape; MyShape = new circle(r); MyShape.MyArea(); break; case 2: Console.WriteLine("请输入三角形的高h,宽íhemiline:"); int h, hemiline; h = Int32.Parse(Console.ReadLine()); hemiline = Int32.Parse(Console.ReadLine()); MyShape = new triangle(hemiline, h); MyShape.MyArea(); break; default: Console.WriteLine("您输入有误,请重新输入!"); break; } } } }}
2)、实验结果截图:
1 0
- C#类的多态性:求图形的面积
- 求图形的面积
- C++:多态性2(图形的面积)
- 多态性创建三角形的类求面积并打印
- 求图形的最大面积
- 求折痕组成的图形的面积
- C#求圆的面积
- 图形类的派生与继承(求面积与求周长)
- C#动态求圆的面积
- C#求三角形的周长和面积
- 使用接口求图形的周长与面积
- (LeetCode)Rectangle Area --- 求图形覆盖的面积
- Problem G: 求图形的面积和体积
- Problem A: 求图形的面积和体积
- Problem G: 求图形的面积和体积
- java求图形的面积(两种比较)
- 1001:图形的面积
- C#类的多态性(非常重要)
- LeetCode|Longest Substring Without Repeating Characters
- linux下的时间 文章来自:http://wenwen.sogou.com/z/q311998900.htm
- Linux 网络命令总结
- 图解Linux命令之--time命令
- 精益创业和画布实战(2):皇包车和易途8,中文包车游世界
- C#类的多态性:求图形的面积
- LeetCode|Sum of Two Integers
- VMware vSphere
- JDK动态代理与Spring AOP
- LeetCode|Add Two Numbers
- AngularJS Directive 隔离 Scope 数据交互
- LeetCode|4Sum
- BZOJ 1336 & 1337 最小圆覆盖
- 用自己电脑做服务器发布Java项目到外网