隐藏页面特效

C# 类的继承问题 正方形周长面积计算问题

1. 设计编写一个控制台应用程序,练习类的继承。

(1) 编写一个抽象类 People,具有”姓名”,”年龄”字段,”姓名”属性,Work 方法。

(2) 由抽象类 People 派生出学生类 Student 和职工类 Employer,继承 People 类,并覆盖Work 方法。

(3) 派生类 Student 增加”学校”字段,派生类 Employer 增加”工作单位”字段。

(4) 在 Student 和 Employer 实例中输出各自不同的信息。

 

代码如下:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Testing2_1 { abstract class People { public String name; //姓名 public String Name //姓名属性 { get; set; } public String age; //年龄 public abstract void work(); //work方法 } class Stu:People { public String school; //增加学校的字段 public override void work() { Console.WriteLine("这是Student类!"); } } class Employer :People //Empoyer类 { public String workplace; public override void work() { Console.WriteLine("这是Employer类!"); } } class Program { static void Main(string[] args) { Stu stu = new Stu(); Console.WriteLine("请输入学生的姓名:"); stu.name=Console.ReadLine(); Console.WriteLine("请输入学生的小名:"); stu.Name = Console.ReadLine(); Console.WriteLine("请输入学生的年龄:"); stu.age = Console.ReadLine(); Console.WriteLine("请输入学生的学校:"); stu.school = Console.ReadLine(); Console.WriteLine("学生的姓名:{0}", stu.name); Console.WriteLine("学生的小名:{0}", stu.Name); Console.WriteLine("学生的年龄:{0}", stu.age); Console.WriteLine("学生的学校:{0}", stu.school); stu.work(); Employer emp = new Employer(); Console.WriteLine("请输入员工的姓名:"); emp.name = Console.ReadLine(); Console.WriteLine("请输入员工的年龄:"); emp.age = Console.ReadLine(); Console.WriteLine("请输入员工的工作地点:"); emp.workplace = Console.ReadLine(); Console.WriteLine("员工的姓名:{0}", emp.name); Console.WriteLine("员工的年龄:{0}", emp.age); Console.WriteLine("员工的工作地点:{0}", emp.workplace); emp.work(); Console.ReadLine(); Console.ReadLine(); } } }

 

 

2. 编写一个控制台应用程序,输入正方形边长或者半径,计算其周长和面积并输出。

(1) 编写两个接口,接口 IShape 包含三个方法:initialize, getPerimeter, getArea。分别进行初始化、获取边长和面积,其返回值均为 decimal。接口 IDisplayresult 显示计算结果。

(2) 编写两个类,Square(正方形)和 Circle(圆形),实现 IShape 和 IDisplayresult接口。

 

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Testing2_2 { public interface IShape { void initalize(); //初始化 decimal getPerimeter(); //获取边长 decimal getArea(); //获取面积 } public interface IDisplayresult { void show(); //结果显示 } public class Square:IShape,IDisplayresult { public decimal sidelength; public void initalize() { Console.WriteLine("请输入正方形边长:"); do { sidelength = decimal.Parse(Console.ReadLine()); if (sidelength <= 0) { Console.WriteLine("输入错误!请重新输入:"); } }while (sidelength <= 0); } public decimal getPerimeter() //正方形获取面积的实现 { return 4 * sidelength; } public decimal getArea() { return sidelength * sidelength; } public void show() { Console.WriteLine("正方形周长:{0}", getPerimeter()); Console.WriteLine("正方形面积:{0}", getArea()); } }public class Circle:IShape,IDisplayresult { decimal radius; const decimal pi = 3.14M; public void initalize() //圆形初始化实现 { Console.WriteLine("请输入圆形半径:"); do { radius = decimal.Parse(Console.ReadLine()); if(radius<=0) { Console.WriteLine("输入错误!请重新输入!"); } } while (radius <= 0); } public decimal getPerimeter() { return 2 * pi * radius; } public decimal getArea() { return pi * radius * radius; } public void show() { Console.WriteLine("圆形周长:{0}", getPerimeter()); Console.WriteLine("圆形面积:{0}", getArea()); } } class Program { static void Main(string[] args) { Square squ = new Square(); Circle cir = new Circle(); int i = 0; Console.WriteLine("欢迎进入正方形圆形周长面积计算系统!"); do { Console.WriteLine("菜单选择;"); Console.WriteLine("1、正方形 2、圆形 3、退出"); Console.WriteLine("请输入操作:"); i = int.Parse(Console.ReadLine()); switch (i) { case 1: squ.initalize(); squ.show(); break; case 2: cir.initalize(); cir.show(); break; case 3: Console.WriteLine("感谢您的使用!"); break; default: Console.WriteLine("输入错误!请重新输入!"); break; } } while (i != 3); } } }

 


__EOF__

本文作者往心。
本文链接https://www.cnblogs.com/lx06/p/15686207.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   往心。  阅读(228)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
历史上的今天:
2020-12-14 jsp标签问题
点击右上角即可分享
微信分享提示