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);
}
}
}