代码如下:
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();
}
}
}
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);
}
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章