黑马程序员_c#面向对象基础:属性

---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------

属性:

惯用法:属性开头字母要大写,字段开头字母要小写.

例如:

    class Person
    {
        private int age;             //字段

        private int Age              //属性mz
        {
            get { return age; }
            set { age = value; }
        }

        public void SayHello()       //方法
        {
            Console.WriteLine("我的年龄是:{0}",Age);
        }
    }

  • 只用set,或者只用get就可以定义只读或者只写属性(只写的不常见).
  • 可以为set或get设置访问级别.
  • 例子,限制非法值的设置.
  • (.net3.x)简化了set,get.:public int Age{ set;get},适合于set,get中没有特殊逻辑代码的情况.
  • 字段和属性的区别是:属性看似字段不是字段,可以进行非法值控制,可以设置只读.

属性练习:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace MyTryParse

{

    class Program

    {

        public static void Main(string[] args)

        {

            Person person1 = new Person();

            person1.Age = 30;

            person1.Name = "";

            Console.WriteLine("叫{0},¨{1}.",person1.Name,person1.Age);

 

            Person person2 = new Person();

            person2.Age = 26;

            person2.Name = "";

            Console.WriteLine("我的名字叫{0},我今年¨{1}岁了.",person2.Name, person2.Age);

 

            Person person3 = new Person();

            person3.Age = 28;

            person3.Name = "";

            Console.WriteLine("我的名字叫{0},我今年¨{1}岁了.",person3.Name, person3.Age);

 

 

            PersonCheck personchk = new PersonCheck();

            personchk.Age = -1;          //此处赋了一个非法的值,返回值为0;

            personchk.Name = "马六";

            Console.WriteLine("我的名字叫{0},我今年¨{1}岁了.",personchk.Name, personchk.Age);

 

 

            PersonNew personnew = new PersonNew();

            personnew.Age = 19;

            personnew.Name = "钱七";

            Console.WriteLine("我的名字叫{0},我今年¨{1}岁了.",personnew.Name, personnew.Age);

 

 

            PersonReadOnly personread = new PersonReadOnly();

            personread.IncAge();

            personread.IncAge();

            Console.WriteLine("{0}.", personread.Age);

 

            Console.ReadKey();

 

        }

    }

    class Person

    {

        private int age;             //

        private string name;

 

        public int Age              //

        {          

            set { this.age = value; } //

            get { return this.age; }  //

        }

        public string Name

        {          

            set { this.name = value; }

            get { return this.name; }

        }

 

    }

    class PersonCheck

    {

        private int age;

        private string name;

 

        public int Age                //Age ,age.

        {

            set

            {

                if (value < 0 || value > 100) //

                {

                    return;

                }

                else

                {

                    this.age = value;    //value

                }

            }

            get

            {

                return this.age;

            }

        }

        public string Name

        {

            set

            {

                this.name = value;

            }

            get

            {

                return this.name;

            }

        }

    }

 

    //.net3.x

    class PersonNew

    {

        public int Age

        {

            set;

            get;

        }

        public string Name

        {

            set;

            get;

        }

    }

 

    //

    class PersonX

    {

        public int Age

        {

            set

            {

 

            }

            get

            {

                return 4;  // 值,无论多少,Ì4.

            }

        }

    }

 

    //一个错误的赋值方式 

    class PersonErroe

    {

        private int age;

        private string name;

 

        public int Age

        {

            set

            {

                this.Age = value;   //这样赋值将进入一个死循环,自己调用自己.

            }

            get

            {

                return this.Age;

            }

        }

    }

 

    // 一个只读的属性.

    class PersonReadOnly

    {

        private int age;

        public int Age

        {

            get { return age; }

        }

        public void IncAge()

        {

            age++;

        }

    }

 

}

 

 此代码运行结果为:

---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------

详细请查看:http://edu.csdn.net/heima/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大文件拷贝实例源码,学习关于IO文件流、多线程相关使用做参考。 private void button4_Click(object sender, EventArgs e) { this.progressBar1.Value = 0; this.label1.Text = "0%"; if (!File.Exists(this.textBox1.Text)) { MessageBox.Show("找不到目标文件!"); return; } if (!Directory.Exists(this.textBox2.Text)) { MessageBox.Show("请选择有效的保存路径!"); return; } string fileRead = this.textBox1.Text; string fileSave = Path.Combine(this.textBox2.Text, _fileName); System.Threading.ThreadPool.QueueUserWorkItem((o) => { using (IDisposable file = new FileStream(fileRead, FileMode.Open, FileAccess.Read), fileWrite = new FileStream(fileSave, FileMode.Create, FileAccess.Write)) { int count = 0; long fileLength =((FileStream)file).Length; //目标文件大小 //根据目标文件大小创建byte数组长度 byte[] data = new byte[fileLength > 1024 * 1024 * 30 ? 1024 * 1024 * 30 : fileLength]; //30M 1024 * 1024 * 30 int step = (int)Math.Ceiling(fileLength * 1.0 / data.Length); //分流段数 double n = (100 * 1.0 / step); //每次进度条累加 double m = 0; //累加统计 int spam = 1; //时间间隔 double speed = 0; //拷贝速度 do { DateTime time = DateTime.Now; //文件流操作 count = ((FileStream)file).Read(data, 0, data.Length); ((FileStream)fileWrite).Write(data, 0, count); //保存时间间隔,单位毫秒 spam = (DateTime.Now - time).Milliseconds > 0 ? (DateTime.Now - time).Milliseconds : spam;//必须大于0 //计算速度 单位k/s
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值