C#测试(二)

一.单选题

1.在C#中,从属性的读写特性上来进行分类,可以划分为以下三种,除了()。

A.只读属性

B.只写属性

C.读写属性

D.不可读不可写的属性

2.以下类MyClass的属性count属于()属性

cs 复制代码
class MyClass
{ 
    int i;
    int count
    { 
        get{ 
            return i;
        }
    }    
}

A.只读

B.只写

C.可读写

D.不可读不可写

3.在类A中定义了属性y:

cs 复制代码
class A
{ 
    public int y;
    { 
        get{ 
            return 1; 
        }
        set {}
    }
}

并试图通过下列代码来实现对属性y的访问:

cs 复制代码
A b=new A();
b.y=2;
int x=b.y;

对此,下列说法正确的是()

A.属性y可读可写,因此变量x的值为2

B.属性y可读,但不可写,因此语句"b.y=2;"是错误的

C.属性y可写,但不可读,因此语句"int x=b.y;是错误的

D.属性y可读可写,变量x的值为1

五. 程序题

  1. 本题实现一个员工类Emlployee,该类包含两个私有字段员工姓名name和出生年份birthyear,一个静态字段员工人数employeeNum。要求补全员工类Emlployee代码, 使该类有一个公共的读写属性Name,一个只读属性Age,一个构造函数:给姓名name和出生年份birthyear通过参数赋值,并且每新建一个员工,员工人数employeeNum加一
cs 复制代码
using System;

namespace ConsoleApplication1
{ 
    public class Emlployee  //员工类
    {
        private string name; //员工姓名
        private int birthyear; //出生年份
        public static int employeeNum=0;  //员工数量

        /* 你的代码写在这里 ,提交时交完整程序,不能只提交该函数*/
    }

    public class myCaller
    {
        public static void Main(string[] args)
        {
            string name;
            int birthYear;
            //Console.Write("请输入一共有多少个员工:");
            int num = Convert.ToInt32(Console.ReadLine());
            Emlployee[] emp = new Emlployee[num];
            for (int i=0;i<num;i++)
            {
                //Console.Write("请输入员工姓名:");
                name = Console.ReadLine();
                //Console.Write("请输入员工出生年份:");
                birthYear = Convert.ToInt32(Console.ReadLine());
                emp[i] = new Emlployee(name,birthYear);
            }

            for (int i = 0; i < num; i++)
            { 
                Console.WriteLine("姓名:" + emp[i].Name + ";    年龄:" + emp[i].Age + ";");
            }
            Console.WriteLine("员工数:" + Emlployee.employeeNum);
        }
    }
}
cs 复制代码
using System;

namespace ConsoleApplication1
{ 
    public class Emlployee  //员工类
    {
        DateTime dt = DateTime.Now;
        private string name; //员工姓名
        private int birthyear; //出生年份
        public static int employeeNum=0;  //员工数量

        /* 你的代码写在这里 ,提交时交完整程序,不能只提交该函数*/
        public  string Name{
            get{
                return name;
            }
            set{
                name=value;
            }
        }

        public int Age{
            get{
                return Convert.ToInt32(dt.Year)-birthyear;
            }
        }
        
        public Emlployee(string name,int birthYear){
            this.name=name;
            this.birthyear=birthYear;
            Emlployee.employeeNum++;
        }
    }

    public class myCaller
    {
        public static void Main(string[] args)
        {
            string name;
            int birthYear;
            //Console.Write("请输入一共有多少个员工:");
            int num = Convert.ToInt32(Console.ReadLine());
            Emlployee[] emp = new Emlployee[num];
            for (int i=0;i<num;i++)
            {
                //Console.Write("请输入员工姓名:");
                name = Console.ReadLine();
                //Console.Write("请输入员工出生年份:");
                birthYear = Convert.ToInt32(Console.ReadLine());
                emp[i] = new Emlployee(name,birthYear);
            }

            for (int i = 0; i < num; i++)
            { 
                Console.WriteLine("姓名:" + emp[i].Name + ";    年龄:" + emp[i].Age + ";");
            }
            Console.WriteLine("员工数:" + Emlployee.employeeNum);
        }
    }
}
相关推荐
蓝斯4972 小时前
C# 调用邮箱应用发送带附件的邮件
网络·c#·github
BuHuaX3 小时前
U3D抖微小游戏开发流程 (一)
unity·c#·游戏引擎·游戏策划
脚踏实地皮皮晨3 小时前
003003002_WPF Grid 基类官方类定义逐行深度解析
开发语言·windows·算法·c#·wpf·visual studio
凯丨13 小时前
AI 蠕虫来了:恶意文档如何通过 Copilot for Word 自我传播?
人工智能·c#·copilot
Aaron - Wistron14 小时前
Web API C# (Furion版)带 单元测试
开发语言·后端·c#
骊城英雄16 小时前
基于C#+avalonia ui实现的跨平台点胶机灌胶监控控制上位机软件
开发语言·ui·c#
吾儿良辰16 小时前
一个被BCL遗忘的高性能集合:C# CircularBuffer<T>深度解析
开发语言·windows·c#
哎呦喂我去去去16 小时前
C#实现屏幕墙:同时监控多个电脑桌面(支持Windows、信创Linux、银河麒麟、统信UOS)
linux·windows·c#
caishenzhibiao17 小时前
期货先行者主图 同花顺期货通指标
java·c语言·c#