栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

C#:定义并实现一个公民类Citizen

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#:定义并实现一个公民类Citizen

一、运行效果截图

二、实验要求


三、代码示例
//author---Henan University.software engineering.李思佳
using System;

namespace ConsoleApp3
{
    public enum Gender 
    {
        男,
        女
    }
    public class Citizen
    { 
        public string ID { set; get; }
        public string Name { set; get; }
        public Gender G { set; get; }
        public int Age;
        public string Birthplace { set; get; }
        public string Familyaddress { set; get; }

        public int AGE //属性
        {
            get
            {
                return Age;
            }
            set
            {
                if (value <= 0 || value > 150)
                   Console.WriteLine("您输入的年龄不合法!(将修改为默认值)");
                 else
                   Age = value;                            
            }  //set
        }//age
        public Citizen()
        {
            ID = "0000";
            Name = "xxx";
            G = Gender.男;
            Age = 20;
            Birthplace = "xx省xx市";
            Familyaddress = "xx省xx市";
        }
        public void Input(string id,string name,Gender g,int age,string birthplace,string familyaddress)
        {
            ID = id;
            Name = name;
            G = g;
            AGE = age;
            Birthplace = birthplace;
            Familyaddress = familyaddress;
        }
        public void Print()
        {
            Console.WriteLine("该公民的信息如下:");
            Console.WriteLine("身份证号:" + ID);
            Console.WriteLine("姓名:" + Name);
            Console.WriteLine("性别:" + G);
            Console.WriteLine("年龄:" + Age);
            Console.WriteLine("籍贯:" + Birthplace);
            Console.WriteLine("家庭住址:" + Familyaddress);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Citizen c0 = new Citizen();
            Console.WriteLine("the example person:");
            c0.Print(); 
            Console.WriteLine("");

            Citizen c1 = new Citizen();
            Console.WriteLine("the first person:");
            c1.Input("412825", "lisi", Gender.女, 18, "河南省", "河大");
            c1.Print();
            Console.WriteLine("");

            Citizen c2 = new Citizen();
            Console.WriteLine("the second person:");
            c2.Input("412826", "zhangsan", Gender.男, -1, "河南省", "郑大");
            c2.Print();
            Console.WriteLine("......");

            Console.ReadLine();
        }
    }
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/907693.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号