评论

收藏

[C++] 属性

编程语言 编程语言 发布于:2021-08-03 12:35 | 阅读数:246 | 评论:0

属 性 属性 属性

1.Getter和Setter
属性,Property
这是C#的一种特有语法,类型:Getter/Setter
一般地,将字段设置为private ,
private string name;
然后分别添加Get和Set方法,
public string GetName() {return this.name;
}
public void SetName (string name) {this.name = name;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharp基础语法
{
  class Student
  {
    private string stu_name;
    // Getter
    public string GetName()
    {
      return this.stu_name;
    }
    //Setter
    public void SetName(string stu_name)
    {
      this.stu_name = stu_name;
    }
    //public int stu_id;
    //public string stu_name;
    //public bool stu_sex;
    //public string stu_phone;
    //public Student()
    //{
    //}
    //public Student(int stu_id)
    //{
    //  this.stu_id = stu_id;
    //}
    //public Student(int stu_id, string name)
    //{
    //  this.stu_id = stu_id;
    //  this.stu_name = stu_name;
    //}
  }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharp基础语法
{
  class Program
  {
    static void Main(string[] args)
    {
      Student stu = new Student();
      stu.SetName("major_s");
      Console.WriteLine("姓名:" + stu.GetName());

      //MyForm form = new MyForm();
      //form.button = "保存";
      //form.UserPressButton();
      //Student s1 = new Student();
      //Student s2 = new Student(314521);
      //Student s3 = new Student(1314521, "major_s");
      //Example ex = new Example();
      //ex.number += 3;
      //ex.Output();
      //Student stu = new Student();
      //stu.stu_id = 1314521;
      //stu.stu_name = "major_s";
      //stu.stu_sex = true;
      //stu.stu_phone = "1234567890";
      //Console.WriteLine("学号:{0},姓名:{1},性别:{2},手机号码:{3}", stu.stu_id, stu.stu_name, stu.stu_sex, stu.stu_phone);
    }
  }
}
在C#里,也可以手工添加Getter/Setter
但是,一般使用属性语法来实现。

2.添加属性
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharp基础语法
{
  class Student
  {
    private string stu_name;
    public String Stu_name
    {
      get
      {
        return this.stu_name;
      }
      set
      {
        this.stu_name = value;
      }
    }
     Getter
    //public string GetName()
    //{
    //  return this.stu_name;
    //}
    Setter
    //public void SetName(string stu_name)
    //{
    //  this.stu_name = stu_name;
    //}
    //public int stu_id;
    //public string stu_name;
    //public bool stu_sex;
    //public string stu_phone;
    //public Student()
    //{
    //}
    //public Student(int stu_id)
    //{
    //  this.stu_id = stu_id;
    //}
    //public Student(int stu_id, string name)
    //{
    //  this.stu_id = stu_id;
    //  this.stu_name = stu_name;
    //}
  }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharp基础语法
{
  class Program
  {
    static void Main(string[] args)
    {
      Student stu = new Student();
      stu.Stu_name = "major_s";
      Console.WriteLine("姓名:" + stu.Stu_name);
      //Student stu = new Student();
      //stu.SetName("major_s");
      //Console.WriteLine("姓名:" + stu.GetName());

      //MyForm form = new MyForm();
      //form.button = "保存";
      //form.UserPressButton();
      //Student s1 = new Student();
      //Student s2 = new Student(314521);
      //Student s3 = new Student(1314521, "major_s");
      //Example ex = new Example();
      //ex.number += 3;
      //ex.Output();
      //Student stu = new Student();
      //stu.stu_id = 1314521;
      //stu.stu_name = "major_s";
      //stu.stu_sex = true;
      //stu.stu_phone = "1234567890";
      //Console.WriteLine("学号:{0},姓名:{1},性别:{2},手机号码:{3}", stu.stu_id, stu.stu_name, stu.stu_sex, stu.stu_phone);
    }
  }
}
3.自动属性
自动属性: Auto Property
形式上最简的一种定义方法:
class Student
public string Name { get; set; }}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharp基础语法
{
  class Student
  {
    public string Stu_name { get; set; }
    //private string stu_name;
    //public String Stu_name
    //{
    //  get
    //  {
    //    return this.stu_name;
    //  }
    //  set
    //  {
    //    this.stu_name = value;
    //  }
    //}
    //Getter
     //public string GetName()
     //{
     //  return this.stu_name;
     //}
     //Setter
    //public void SetName(string stu_name)
    //{
    //  this.stu_name = stu_name;
    //}
    //public int stu_id;
    //public string stu_name;
    //public bool stu_sex;
    //public string stu_phone;
    //public Student()
    //{
    //}
    //public Student(int stu_id)
    //{
    //  this.stu_id = stu_id;
    //}
    //public Student(int stu_id, string name)
    //{
    //  this.stu_id = stu_id;
    //  this.stu_name = stu_name;
    //}
  }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharp基础语法
{
  class Program
  {
    static void Main(string[] args)
    {
      Student stu = new Student();
      stu.Stu_name = "major_s";
      Console.WriteLine("姓名:" + stu.Stu_name);
      //Student stu = new Student();
      //stu.Stu_name = "major_s";
      //Console.WriteLine("姓名:" + stu.Stu_name);
      //Student stu = new Student();
      //stu.SetName("major_s");
      //Console.WriteLine("姓名:" + stu.GetName());

      //MyForm form = new MyForm();
      //form.button = "保存";
      //form.UserPressButton();
      //Student s1 = new Student();
      //Student s2 = new Student(314521);
      //Student s3 = new Student(1314521, "major_s");
      //Example ex = new Example();
      //ex.number += 3;
      //ex.Output();
      //Student stu = new Student();
      //stu.stu_id = 1314521;
      //stu.stu_name = "major_s";
      //stu.stu_sex = true;
      //stu.stu_phone = "1234567890";
      //Console.WriteLine("学号:{0},姓名:{1},性别:{2},手机号码:{3}", stu.stu_id, stu.stu_name, stu.stu_sex, stu.stu_phone);
    }
  }
}
自动和手工的属性,有何区别?
手工:可以调试
自定义自动:可以自动生成最简单的get/set
如果想要自定义get/set,应该手工写出



关注下面的标签,发现更多相似文章