蓝鸥Unity开发基础——属性
2016-08-24 09:13
225 查看
蓝鸥Unity开发基础——属性
一,属性
属性自动帮我们给字段添加Get和Set方法
属性本质上也是Set和Get方法,只是形式不同
namespace Lesson_07
{
class MainClass
{
public class Person{
private string name;
//name 的Get方法
// public string GetName(){
// return name;
// }
//name 的Set方法
// public string SetName(string value){
// name=value;
// }
//访问修饰符 属性类型 属性名 (set(……);get(……))
public string Name{
//Get访问起
get{
return name;
}
//Set访问起
set{
//value 关键字只有在属性的Set中有意义,表示外界传递过来的值
name = value;
}
}
}
public static void Main (string[] args)
{
Person p = new Person ();
// p.SetName ("lanou");
// Console.WriteLine (p.GetName());
p.Name = "lanou";
Console.WriteLine (p.Name);
}
}
}
二、访问器
属性中的Set和Get称作访问器
Get访问器用来读取属性的值,相当于Get方法
Set访问器用来设置属性的值,相当于Set方法
只有Set访问器的属性叫做只写属性
只有Get访问器的属性叫做只读属性
同时具有两个访问器的属性叫读写属性
using System;
namespace Lesson_07
{
class MainClass
{
public class Person{
private string name;
//name 的Get方法
// public string GetName(){
// return name;
// }
//name 的Set方法
// public string SetName(string value){
// name=value;
// }
//访问修饰符 属性类型 属性名 (set(……);get(……))
public string Name{
//Get访问起
get{
return name;
}
//Set访问起
set{
//value 关键字只有在属性的Set中有意义,表示外界传递过来的值
name = value;
}
}
//只读属性
private int age=18;
//只读属性,只有Get访问器
public int Age
{
get{
return age;
}
}
private float h=1.75f;
//只写属性,只有Set访问器
public float H
{
set{
H=value;
}
}
}
public static void Main (string[] args)
{
Person p = new Person ();
// p.SetName ("lanou");
// Console.WriteLine (p.GetName());
p.Name = "lanou";
Console.WriteLine (p.Name);
Console.WriteLine (p.Age);
p.H = 12;
}
}
}
三、属性访问权限
仅当属性同事具备Set和Get方法时,才能使用访问器修饰符,并且只能够对其中一个访问器使用修饰符。
属性课程代码:
using System;
namespace Lesson_07
{
class MainClass
{
public class Person{
private string name;
//name 的Get方法
// public string GetName(){
// return name;
// }
//name 的Set方法
// public string SetName(string value){
// name=value;
// }
//访问修饰符 属性类型 属性名 (set(……);get(……))
//属性
//1、属性中同事具有get set访问器
//2、只能给一个访问器设置额外的访问权限
public string Name{
//Get访问起
get{
return name;
}
//Set访问起
private set{
//value 关键字只有在属性的Set中有意义,表示外界传递过来的值
name = value;
}
}
public void a(){
name ="lanou";
}
//只读属性
private int age=18;
//只读属性,只有Get访问器
public int Age
{
get{
return age;
}
}
private float h=1.75f;
//只写属性,只有Set访问器
public float H
{
set{
H=value;
}
}
}
public static void Main (string[] args)
{
Person p = new Person ();
// p.SetName ("lanou");
// Console.WriteLine (p.GetName());
// p.Name = "lanou";
Console.WriteLine (p.Name);
Console.WriteLine (p.Age);
p.H = 12;
}
}
}
练习:将10个类中的Set
和Get方法全部替换成属性。
一,属性
属性自动帮我们给字段添加Get和Set方法
属性本质上也是Set和Get方法,只是形式不同
namespace Lesson_07
{
class MainClass
{
public class Person{
private string name;
//name 的Get方法
// public string GetName(){
// return name;
// }
//name 的Set方法
// public string SetName(string value){
// name=value;
// }
//访问修饰符 属性类型 属性名 (set(……);get(……))
public string Name{
//Get访问起
get{
return name;
}
//Set访问起
set{
//value 关键字只有在属性的Set中有意义,表示外界传递过来的值
name = value;
}
}
}
public static void Main (string[] args)
{
Person p = new Person ();
// p.SetName ("lanou");
// Console.WriteLine (p.GetName());
p.Name = "lanou";
Console.WriteLine (p.Name);
}
}
}
二、访问器
属性中的Set和Get称作访问器
Get访问器用来读取属性的值,相当于Get方法
Set访问器用来设置属性的值,相当于Set方法
只有Set访问器的属性叫做只写属性
只有Get访问器的属性叫做只读属性
同时具有两个访问器的属性叫读写属性
using System;
namespace Lesson_07
{
class MainClass
{
public class Person{
private string name;
//name 的Get方法
// public string GetName(){
// return name;
// }
//name 的Set方法
// public string SetName(string value){
// name=value;
// }
//访问修饰符 属性类型 属性名 (set(……);get(……))
public string Name{
//Get访问起
get{
return name;
}
//Set访问起
set{
//value 关键字只有在属性的Set中有意义,表示外界传递过来的值
name = value;
}
}
//只读属性
private int age=18;
//只读属性,只有Get访问器
public int Age
{
get{
return age;
}
}
private float h=1.75f;
//只写属性,只有Set访问器
public float H
{
set{
H=value;
}
}
}
public static void Main (string[] args)
{
Person p = new Person ();
// p.SetName ("lanou");
// Console.WriteLine (p.GetName());
p.Name = "lanou";
Console.WriteLine (p.Name);
Console.WriteLine (p.Age);
p.H = 12;
}
}
}
三、属性访问权限
仅当属性同事具备Set和Get方法时,才能使用访问器修饰符,并且只能够对其中一个访问器使用修饰符。
属性课程代码:
using System;
namespace Lesson_07
{
class MainClass
{
public class Person{
private string name;
//name 的Get方法
// public string GetName(){
// return name;
// }
//name 的Set方法
// public string SetName(string value){
// name=value;
// }
//访问修饰符 属性类型 属性名 (set(……);get(……))
//属性
//1、属性中同事具有get set访问器
//2、只能给一个访问器设置额外的访问权限
public string Name{
//Get访问起
get{
return name;
}
//Set访问起
private set{
//value 关键字只有在属性的Set中有意义,表示外界传递过来的值
name = value;
}
}
public void a(){
name ="lanou";
}
//只读属性
private int age=18;
//只读属性,只有Get访问器
public int Age
{
get{
return age;
}
}
private float h=1.75f;
//只写属性,只有Set访问器
public float H
{
set{
H=value;
}
}
}
public static void Main (string[] args)
{
Person p = new Person ();
// p.SetName ("lanou");
// Console.WriteLine (p.GetName());
// p.Name = "lanou";
Console.WriteLine (p.Name);
Console.WriteLine (p.Age);
p.H = 12;
}
}
}
练习:将10个类中的Set
和Get方法全部替换成属性。
相关文章推荐
- asp.net控件开发基础(9) ----------再谈属性,学习自定义类型转换器
- asp.net控件开发基础(5) -- 复杂属性、内嵌属性
- wpf控件开发基础(3) -属性系统(2)
- wpf控件开发基础(5) -依赖属性实践
- wpf控件开发基础(5) -依赖属性实践
- asp.net控件开发基础(5) ----------简单介绍自定义控件简单属性和复杂属性
- Ruby on rails开发从头来(三十八)- ActiveRecord基础(列和属性)
- wpf控件开发基础(2) -属性系统(1)
- ASP.NET控件开发基础之实现控件集合属性
- Ruby on rails开发从头来(四十)- ActiveRecord基础(Boolean属性)
- asp.net控件开发基础(9) -- 属性类型转换器
- C#基础系列:开发自己的窗体设计器(PropertyGrid显示中文属性名)
- Ruby on rails开发从头来(四十)- ActiveRecord基础(Boolean属性)
- wpf控件开发基础(2) -属性系统(1)
- Ruby on rails开发从头来(三十八)- ActiveRecord基础(列和属性)
- asp.net控件开发基础(10) --------再谈属性,实现自定义控件集合属性
- Ruby on rails开发从头来(三十九)- ActiveRecord基础(访问属性)
- asp.net控件开发基础(5) -- 复杂属性、内嵌属性
- wpf控件开发基础(4) -属性系统(3)
- Ruby on rails开发从头来(三十九)- ActiveRecord基础(访问属性)