using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*
* C#属性
* 定义:属性是一种类的成员,它的实现类似方法(函数),访问类似字段(变量)。它的作用是提供一种灵活和安全的机制来访问,修改私有字段。属性一般依赖于字段
* 语法:修饰符+类型(一般为字段类型)+属性名
* {
* get{return 返回值};
* set{设置值 value}
* }
* get:
* get访问器用来返回字段或者计算,并返回字段,他必须以return,只有get只读属性
* set:
* set访问器类似返回类型为void的函数,使用value的隐式参数
* 访问层级限制:
* get、set可以用修饰符修饰,注意修饰符层级比外层修饰符访问级别低;
* 自动属性:
* 修饰符+类型+属性名{get;set}
* public int Num{get;set}
*
* 只读:只有get 没有set
* 只写: 只有set 没有get
* 可读可写:既有set又有get
*
*/
namespace ConsoleApplication4
{
#region
//class Program
//{
// static void Main(string[] args)
// {
// Student st = new Student();
// st.Age = 23;//当点在等号左边,就是set方法(取值)
// int num = st.Age;//当点在等号右边,或者没有等号,等属于get方法(取值)
// Console.WriteLine(num);
// Preson p = new Preson();
// p.Age1 = 3;
// //p.Age1 = 24;
// int numPerson = p.Age1;
// Console.WriteLine(numPerson);
// Num = 1;
// Console.WriteLine(Num);
// Console.ReadKey();
// }
// private static int num = 10;
// public static int Num
// {
// get { return num; }
// set
// {
// if (num > 0)
// {
// num = value;
// }
// }
// }
//}
//class Student
//{
// private int age;
// public int Age
// {
// get { return age; }//get方法专门用来取值
// set { age = value; }//set方法专门给属性赋值
// }
//}
get、set可以滤掉不合理的值
//class Preson
//{
// private int age1;
// public int Age1
// {
// get { return age1; }
// set
// {
// if (value >= 18)
// {
// age1 = value;
// }
// }
// }
//}
#endregion
#region
// class Program
// {
* 类就是模子,对象是有类创建的,类描述了对象有哪些属性和行为
* 创建对象用new关键字
* class类名
* {
* 字段 name;//用来存储数据
* 属性 Name;//属性是用来保护字段的,限制了字段的取值和存值
* 行为(方法)
* }
// static void Main(string[] args)
// {
// Student std = new Student();
// std.Age = 23;
// std.Sex = '男';
// Console.ReadKey();
// }
// }
// class Student
// {
// private int _age;//字段
// public int Age
// {
// get { return _age; }
// set { _age = value; }
// }
// private char _sex;
// public char Sex
// {
// get { return _sex; }
// set { _sex = value; }
// }
// public void Run()
// {
// Console.WriteLine("跑步");
// }
// }
#endregion
#region
//class Program
//{
// public static int Score { get;set }//自动属性,注意这不是一个字段
// static void Main(string[] args)
// {
// Study study = new Study();
// study.SetNum(20);
// int num = study.GetNum();
// Console.WriteLine(num);
// Console.ReadKey();//结果是20
// }
//}
//class Study
//{
// //private int num;//可以说这是一个字段
// private int num = 10;
// public int Num//这里没有小括号
// {
// //get { return 0; }
// get { return num; }
// set { num = value; }
// }
// public int GetNum()
// {
// return num;
// }
// public void SetNum(int value)
// {
// num = value;
// }
//}
#endregion
class Program
{
static void Main(string[] args)
{
Start();
Console.ReadKey();
}
private static int money = 10;
public static int Money
{
get { return money; }
set { money = value; }
}
private static string name = "xiaosong";
public static string Name
{
get { return name; }
set { name = value; }
}
//获取金钱
public static int GetMoneyNum()
{
return money;
}
/// <summary>
/// 设置金钱数量
/// </summary>
/// <param name="value">给予的钱数</param>
public static void SetMoneyNum(int value)
{
money = value;
}
public static void Start()
{
int xiaosognMoney = GetMoneyNum();
Console.WriteLine(xiaosognMoney);
SetMoneyNum(20);
int xiaosongMoney = money;
Console.WriteLine(money);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*
* C#属性
* 定义:属性是一种类的成员,它的实现类似方法(函数),访问类似字段(变量)。它的作用是提供一种灵活和安全的机制来访问,修改私有字段。属性一般依赖于字段
* 语法:修饰符+类型(一般为字段类型)+属性名
* {
* get{return 返回值};
* set{设置值 value}
* }
* get:
* get访问器用来返回字段或者计算,并返回字段,他必须以return,只有get只读属性
* set:
* set访问器类似返回类型为void的函数,使用value的隐式参数
* 访问层级限制:
* get、set可以用修饰符修饰,注意修饰符层级比外层修饰符访问级别低;
* 自动属性:
* 修饰符+类型+属性名{get;set}
* public int Num{get;set}
*
* 只读:只有get 没有set
* 只写: 只有set 没有get
* 可读可写:既有set又有get
*
*/
namespace ConsoleApplication4
{
#region
//class Program
//{
// static void Main(string[] args)
// {
// Student st = new Student();
// st.Age = 23;//当点在等号左边,就是set方法(取值)
// int num = st.Age;//当点在等号右边,或者没有等号,等属于get方法(取值)
// Console.WriteLine(num);
// Preson p = new Preson();
// p.Age1 = 3;
// //p.Age1 = 24;
// int numPerson = p.Age1;
// Console.WriteLine(numPerson);
// Num = 1;
// Console.WriteLine(Num);
// Console.ReadKey();
// }
// private static int num = 10;
// public static int Num
// {
// get { return num; }
// set
// {
// if (num > 0)
// {
// num = value;
// }
// }
// }
//}
//class Student
//{
// private int age;
// public int Age
// {
// get { return age; }//get方法专门用来取值
// set { age = value; }//set方法专门给属性赋值
// }
//}
get、set可以滤掉不合理的值
//class Preson
//{
// private int age1;
// public int Age1
// {
// get { return age1; }
// set
// {
// if (value >= 18)
// {
// age1 = value;
// }
// }
// }
//}
#endregion
#region
// class Program
// {
* 类就是模子,对象是有类创建的,类描述了对象有哪些属性和行为
* 创建对象用new关键字
* class类名
* {
* 字段 name;//用来存储数据
* 属性 Name;//属性是用来保护字段的,限制了字段的取值和存值
* 行为(方法)
* }
// static void Main(string[] args)
// {
// Student std = new Student();
// std.Age = 23;
// std.Sex = '男';
// Console.ReadKey();
// }
// }
// class Student
// {
// private int _age;//字段
// public int Age
// {
// get { return _age; }
// set { _age = value; }
// }
// private char _sex;
// public char Sex
// {
// get { return _sex; }
// set { _sex = value; }
// }
// public void Run()
// {
// Console.WriteLine("跑步");
// }
// }
#endregion
#region
//class Program
//{
// public static int Score { get;set }//自动属性,注意这不是一个字段
// static void Main(string[] args)
// {
// Study study = new Study();
// study.SetNum(20);
// int num = study.GetNum();
// Console.WriteLine(num);
// Console.ReadKey();//结果是20
// }
//}
//class Study
//{
// //private int num;//可以说这是一个字段
// private int num = 10;
// public int Num//这里没有小括号
// {
// //get { return 0; }
// get { return num; }
// set { num = value; }
// }
// public int GetNum()
// {
// return num;
// }
// public void SetNum(int value)
// {
// num = value;
// }
//}
#endregion
class Program
{
static void Main(string[] args)
{
Start();
Console.ReadKey();
}
private static int money = 10;
public static int Money
{
get { return money; }
set { money = value; }
}
private static string name = "xiaosong";
public static string Name
{
get { return name; }
set { name = value; }
}
//获取金钱
public static int GetMoneyNum()
{
return money;
}
/// <summary>
/// 设置金钱数量
/// </summary>
/// <param name="value">给予的钱数</param>
public static void SetMoneyNum(int value)
{
money = value;
}
public static void Start()
{
int xiaosognMoney = GetMoneyNum();
Console.WriteLine(xiaosognMoney);
SetMoneyNum(20);
int xiaosongMoney = money;
Console.WriteLine(money);
}
}
}