大数据

2021.10.2 .net控制台应用程序

 

一、今日学习内容

   

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 _20194020

{

    abstract class People   //抽象类:people

    {

        public String name; //姓名

        public int age;//年龄

        public abstract void work();//work方法

    }

    class Student : People  //学生类,继承people类

    {

        public String school; //增加的学校字段

        public override void work()

        {

            Console.WriteLine("student类work");

        }

    }

    class Employer : People //Employer类,继承people类

    {

        public String workplace;//增加的工作单位字段

        public override void work()

        {

            Console.WriteLine("Employer类work");

        }

 

    }

    class Program

    {

        static void Main(string[] args)

        {

            Student st = new Student();

            Console.WriteLine("请输入学生的姓名:");

            st.name = Console.ReadLine();

            Console.WriteLine("请输入学生的年龄:");

            st.age = int.Parse(Console.ReadLine());

            Console.WriteLine("请输入学生的学校:");

            st.school = Console.ReadLine();

            Console.WriteLine("学生的姓名:{0}", st.name);

           // Console.WriteLine("学生的小名:{0}", st.Name);

            Console.WriteLine("学生的年龄:{0}", st.age);

            Console.WriteLine("学生的学校:{0}", st.school);

            st.work();

            Employer em = new Employer();

            Console.WriteLine("请输入员工的姓名:");

            em.name = Console.ReadLine();

            Console.WriteLine("请输入员工的年龄:");

            em.age = int.Parse(Console.ReadLine());

            Console.WriteLine("请输入员工的工作地点:");

            em.workplace = Console.ReadLine();

            Console.WriteLine("员工的姓名:{0}", em.name);

            Console.WriteLine("员工的年龄:{0}", em.age);

            Console.WriteLine("员工的工作地点:{0}", em.workplace);

            em.work();

        }

    }

}
复制代码

 

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;

 

namespace _20194020

{

    public interface IShape

    {

        void initialize();   //初始化

        decimal getPerimeter();   //获取边长

        decimal getArea();    //获取面积

    }

    public interface IDisplayresult

    {

        void show();

    }

    public class Square : IShape, IDisplayresult   //正方形类

    {

        public decimal length;   //边长

        public void initialize()   //正方形初始化

        {

            Console.WriteLine("请输入正方形的边长:");

            do

            {

                length = decimal.Parse(Console.ReadLine());

                if (length <= 0)

                {

                    Console.WriteLine("输入数据错误!请重新输入正方形的边长:");

                }

            } while (length <= 0);

        }

        public decimal getPerimeter()   //获取正方形周长

        {

            return 4 * length;

        }

        public decimal getArea()    //获取正方形面积

        {

            return length * length;

        }

        public void show()    //显示计算结果

        {

            Console.WriteLine("该正方形的周长:{0}", getPerimeter());

            Console.WriteLine("该正方形的面积:{0}", getArea());

        }

    }

    public class Circle : IShape, IDisplayresult   //圆形类

    {

        public decimal radius;   //半径

        const decimal pai = 3.14M;   //定义π的值

        public void initialize()   //圆形初始化

        {

            Console.WriteLine("请输入圆形的半径:");

            do

            {

                radius = decimal.Parse(Console.ReadLine());

                if (radius <= 0)

                {

                    Console.WriteLine("输入数据错误!请重新输入圆形的半径:");

                }

            } while (radius <= 0);

        }

        public decimal getPerimeter()   //获取圆形周长

        {

            return 2 * pai * radius;

        }

        public decimal getArea()    //获取圆形面积

        {

            return pai * radius * radius;

        }

        public void show()    //显示计算结果

        {

            Console.WriteLine("该圆形的周长:{0}", getPerimeter());

            Console.WriteLine("该圆形的面积:{0}", getArea());

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            Square sq = new Square();

            Circle cir = new Circle();

            int i;

            while (true)

            {

                Console.WriteLine("1.正方形  2.圆形  3.退出");

                i = int.Parse(Console.ReadLine());

                switch (i)

                {

                    case 1:

                        sq.initialize();   //正方形初始化

                        sq.show();   //显示正方形计算结果

                        break;

                    case 2:

                        cir.initialize();   //圆形初始化

                        cir.show();   //显示圆形计算结果

                        break;

                    case 3:

                        break;

                    default:

                        Console.WriteLine("输入错误!");

                        break;

                }

                if (i == 3)

                    break;   //退出

            }

        }

    }

}

 

posted @ 2021-10-02 09:17  大风吹爱护  阅读(45)  评论(0编辑  收藏  举报