zoukankan      html  css  js  c++  java
  • C#.NET程序设计实验二实验报告

    实验二  面向对象程序设计

    一、实验目的

    1. 理解类的定义、继承等面向对象的的基本概念;

    2. 掌握C#语言定义类及其各种成员(字段,属性,方法)的方法;

    3. 掌握方法覆盖的应用;

    4. 掌握接口的定义和实现方法。

    二、实验要求

      根据要求,编写 C#程序,并将程序代码和运行结果写入实验报告。

    三、实验内容

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

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

    (2) 由抽象类 People 派生出学生类 Student 和职工类 Employer,继承 People 类,并

    覆盖Work 方法。

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

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

    源程序代码

    namespace Docu1

    {

        abstract class People

            //抽象类:people

        {

            public String name;//姓名

            public String Name//姓名属性

            {

                get;

                set;

            }

     

            public int age;//年龄

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

        }

     

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

        {

            public String school;//学校

            public override void work()

            {

                Console.WriteLine("学生的工作();");

                //throw new NotImplementedException();

     

            }

        }

     

        class Employer:People//继承people的职员类

        {

            public String workspace;//工作场所

            public override void work()

            {

                Console.WriteLine("职员的work();");

                //throw new NotImplementedException();

            }

        }

     

        class Docu2_1

        {

            static void Main2_1(string[] ba)

            {

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

                student.name = Console.ReadLine();

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

                student.Name = Console.ReadLine();

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

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

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

                student.school = Console.ReadLine();

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

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

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

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

                student.work();

                Employer employer = new Employer();

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

                employer.name = Console.ReadLine();

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

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

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

                employer.workspace = Console.ReadLine();

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

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

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

                employer.work();

            }

       

        }

    }

    程序运行截图

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

    (1) 编写两个接口,接口 IShape 包含三个方法:initialize, getPerimeter, getArea。分

    别进行初始化、获取边长和面积,其返回值均为 decimal。接口 IDisplayresult 显示计算结果。

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

    接口。

    源程序代码

    namespace Docu1

    {

        public interface IShape//IShape接口,方法

        {

     

            void initialize(); //初始化

            decimal getPerimeter();//获取边长

            decimal getArea();//获取面积

        }

     

        public interface IDisplayresult//IDisplayresult接口,方法

        {

            void show();

        }

     

        public class Square : IShape, IDisplayresult   //正方形类//调用接口

        {

            public decimal sidelength;//边长

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

            {

     

                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 pai = 3.14M;//常量pi

            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 Docu2_2

        {

            static void Main2_2(string[] bb)

            {

                Square sq = new Square(); Circle cir = new Circle();

                int i;

                Console.WriteLine("-----------------正方形、圆形周长面积计算系统--------------");

                do

                {

                    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:

                            Console.WriteLine("欢迎下次使用");

                            break;

                        default: Console.WriteLine("输入错误"); break;

                    }

                } while (i != 3);

            }

        }

    }

    程序运行截图

    四、实验总结

    通过本次实验,我再次练习编写C#关于类的继承与重载,以及接口调用等信息。并成功实现习题要求。

  • 相关阅读:
    唐寅 《桃花庵歌》
    asp.net 后台隐藏div
    dataset的用法
    C#中的DateTime类型加减
    discuz! x2.5 文章添加分享按钮
    asp.net学习小网站
    table固定行和表头
    aspx.net开源的画图组件
    Global.asax详解
    int.Parse() int.TryParse
  • 原文地址:https://www.cnblogs.com/aiyyue/p/14071626.html
Copyright © 2011-2022 走看看