读书人

这是啥语法解决方法

发布时间: 2013-01-11 11:57:35 作者: rapoo

这是啥语法
这咋用,干啥用的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace Csharp
{
class People
{
private int myInt;
public int MyIntProp//这是什么玩意
{
get
{
return myInt;
}
set
{
myInt = value;
}
}
}
class Program
{
static void Main(string[] args)
{
People p = new People();
p.MyIntProp = 1;
Console.ReadKey();
}

}
}

[解决办法]
这个是属性啊。
[解决办法]
MyIntProp是My(我的)Integer(整数)Property(属性)的意思。
[解决办法]
目测楼主是学C++的...我也不懂这是啥意思
[解决办法]
这个是属性语法啊,如果是.NET FrameWork 3.0以上的话,可以简写成

private int myInt;
public int MyIntProp
{
get
{
return myInt;
}
set
{
myInt = value;
}
}

========》简写成

public int MyIntProp { get;set; }
[解决办法]
        private int myInt;
public int MyIntProp
{
get
{
return myInt;
}
set
{


myInt = value;
}
}


这种写法就是脱裤子放屁。
[解决办法]
相当于
        private int myInt;
public int Get_myInt()
{
return myInt;
}
public void Set_myInt(int value)
{
myInt = value;
}

当myInt与接口无关时,其实就是
        public int myInt;

[解决办法]
引用:
引用:目测楼主是学C++的...我也不懂这是啥意思
你真是好眼力。



引用:这个是属性语法啊,如果是.NET FrameWork 3.0以上的话,可以简写成……
这属性是干啥的


按照面向对象的做法,一个对象不应该有任何共有字段。
也就是一个对象只能通过它自己的方法去修改自身状态。
比如
class People
{
public int Age;
}
这是不好的设计
你应该设计成
class Prople
{
private int age;
public int Get_Age() { return age; }
public void Set_Age(int value) { age = value; }
}
为此,C#提供了简略的写法
class Prople
{
private int age;
public int Age { get { return age; } set { age = value; } }
}
也就是
class Prople
{
public int Age { get; set; }
}
[解决办法]
还有一种写法
        public int myInt { get; set; }


下面这种写法还有点意义
        public int myInt { get; private set; }

[解决办法]
我也转到 C# 不久,相当于 java 中 的属性的set 和get方法,
在C# 中,规范了属性用大写,字段用小写,如:
private int age -> 字段。
public Age{set{this.age = value};get{return this.age}}
这些都是C# 内置的工作,不明白的话,可以看 Reflector
这是平时写的,都省略了!
就如, 对象.Age = "Lisi" ->内部调用 Age{set{this.age = value}
int output = 对象.Age ->内部调用 get{return this.age}
[解决办法]
属性

读书人网 >C#

热点推荐