C#面向对象 公民Citizen类

定义一个公民类,该类包含的特征信息有身份证号,性别,年龄,籍贯,住址等属性及构造方法。输入公民信息方法input以及输出方法print。要求能对该类对象进行初始化,输入,输出操作

namespace  _3.5
{
    internal class Program
    {
        static void Main(string[] args)
        {
           
            Citizen citizen = new Citizen();    //初始化Citizen类的对象
            Console.WriteLine("初始信息:");
            citizen.Print();    //打印初始信息               
            Console.WriteLine("\n输入要修改的信息(无需修改的信息直接跳过):\n");
            citizen.Input();    //修改初始信息
            citizen.Print();    //打印修改后的信息
        }
    }

    public class Citizen  //创建Citizen类并定义基本字段及属性
    {
        public string? Id { set; get; } 
        public string? Gender { set; get;} 
        public string? Age { set; get; }
        public string? Birthplace { set; get; }
        public string? Familyaddress { set; get; }

        public Citizen()    //构造函数对基本字段初始化
        {
            Id = "123456";
            Gender = "男";
            Age = "20";
            Birthplace = "云南昆明";
            Familyaddress = "云南师范大学";
        }

        public void Input() //方法:修改类的字段值
        {
            Console.WriteLine("身份证号:");
            string? id = Console.ReadLine();
            if (id != "")   //为空值时不改变字段原来的值否则为输入的值
            {               
                Id=id; 
            }
            Console.WriteLine("性别:");
            string? gender = Console.ReadLine();
            if (gender != ""){ 
                Gender = gender;
            }
            Console.WriteLine("年龄:");
            string? age = Console.ReadLine();
            if (age != ""){
                Age = age;
            }
            Console.WriteLine("籍贯:");
            string? birthplace = Console.ReadLine();
            if (birthplace != "") {
                Birthplace = birthplace;
            }
            Console.WriteLine("住址:");
            string? familyaddress = Console.ReadLine();
            if (familyaddress != "") {
                Familyaddress = familyaddress;
            }
        }
        public void Print()     //方法:输出字段的信息
        {
            Console.WriteLine("\n身份证号:{0}\n性别:{1} \n年龄:{2} \n籍贯:{3} \n住址:{4}",Id,Gender,Age,Birthplace,Familyaddress);
        }

    }
}

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PSX_CFC^O^

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值