c#中的property的getter setter方法的正确用法(用c#建立一个form)

博客介绍了C#中User类属性的getter和setter方法的使用,与Java稍有不同,每个属性为private,通过外部的public setter和getter方法访问。还展示了User类的构造函数及多个属性的定义和访问方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用法和java的稍有不同,每一个property都是private的,可给他做一个外部的public setter getter方法。
就像下面所示。

using System;

namespace wztj.page.user.model
{
 /// <summary>
 /// Summary description for User.
 /// </summary>
 public class User
 {
  public User()
  {
   //
   // TODO: Add constructor logic here
   //
  }

  public User(long id,string username,string password,string province,string city,string address,string representive,string telephone,string post,string mail,string fax,string company){
   this.id = id;
   this.username=username;
   this.password= password;
   this.province=province;
   this.city=city;
   this.address=address;
   this.representive=representive;
   this.telephone=telephone;
   this.post=post;
   this.fax=fax;
   this.mail=mail;
   this.post=post;
   this.company=company;
  }

  private long id;
  public long Id
  {
   get {return id;}
   set {id = value;}
  }
  
  private string username;
  public string Username
  {
   get {return username;}
   set {username = value;}
  }

  private string password; 
  public string Password
  {
   get {return password;}
   set {password = value;}
  }

  private string province;
  public string Province
  {
   get {return province;}
   set {province = value;}
  }

  private string city; 
  public string City
  {
   get {return city;}
   set {city = value;}
  }

 
  private string address;
  public string Address
  {
   get {return address;}
   set {address = value;}
  }

  private string representive;
  public string Representive
  {
   get {return representive;}
   set {representive = value;}
  }
  
  private string telephone;
  public string Telephone
  {
   get {return telephone;}
   set {telephone = value;}
  }
  
  private string post;
  public string Post
  {
   get {return post;}
   set {post = value;}
  }
  
  private string mail;
  public string Mail
  {
   get {return mail;}
   set {mail = value;}
  }

  private string fax;
  public string Fax
  {
   get {return fax;}
   set {fax = value;}
  }

  private string company;
  public string Company
  {
   get {return company;}
   set {company = value;}
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值