
C#使用属性封装
使用属性封装身份证号码字段并使用输出语句测试:身份证号码必须是18位,否则重置为:0000-0000-0000-00程序运行结果:“请输入身份证号:1234,身份证号码:...
使用属性封装身份证号码字段并使用输出语句测试:身份证号码必须是18位,否则重置为:0000-0000-0000-00
程序运行结果:“请输入身份证号:1234,身份证号码:0000-0000-0000-00。” 展开
程序运行结果:“请输入身份证号:1234,身份证号码:0000-0000-0000-00。” 展开
展开全部
添加一个类,名字叫PID.cs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
public class PID
{
private string pid;
public string Pid
{
get
{
if (pid.Length == 18)
{
return pid;
}
else
{
return "0000-0000-0000-00";
}
}
set
{
pid = value;
}
}
public PID(string S)
{
pid = S;
}
}
}
Program.cs的main方法里这么写
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("请输入身份证号:");
PID PID = new PID(Console.ReadLine());
Console.WriteLine("身份证号码:" + ID.Pid);
Console.ReadLine();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
public class PID
{
private string pid;
public string Pid
{
get
{
if (pid.Length == 18)
{
return pid;
}
else
{
return "0000-0000-0000-00";
}
}
set
{
pid = value;
}
}
public PID(string S)
{
pid = S;
}
}
}
Program.cs的main方法里这么写
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("请输入身份证号:");
PID PID = new PID(Console.ReadLine());
Console.WriteLine("身份证号码:" + ID.Pid);
Console.ReadLine();
}
展开全部
先构造个类
public Person
{
private string m_codeNO;
public string CodeNO
{
get
{
return this.m_codeNO;
}
set
{
if (value.Length!=18)
this.m_codeNO = '0000-0000-0000-00';
else
this.m_codeNO=value;
}
}
}
然后写个main函数,用个变量接受用户输入的值,
string code = "1234"
然后在main函数中实例化这个类。
Preson person = new Person()
person.CodeNO = code;
System.out.println("你输入的身份证号是" + code + "身份证号:"+ person.CodeNO);
public Person
{
private string m_codeNO;
public string CodeNO
{
get
{
return this.m_codeNO;
}
set
{
if (value.Length!=18)
this.m_codeNO = '0000-0000-0000-00';
else
this.m_codeNO=value;
}
}
}
然后写个main函数,用个变量接受用户输入的值,
string code = "1234"
然后在main函数中实例化这个类。
Preson person = new Person()
person.CodeNO = code;
System.out.println("你输入的身份证号是" + code + "身份证号:"+ person.CodeNO);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |