属性
目的:像用字段一样用这个属性,优点是我可以控制非法值
下边public void apjob(string job)定义了一个job,前面private string job是一个job名字的字段,应该会报错啊,但是没有????
This . 是调用本对象的成员
如果字段的名字和参数的名字重复,this . 名字就是指的字段,否则指的是参数。写了this .job了就是指第一个,没写就是指第二个。
class Program
{
static void Main(string[] args)
{
person p1 = new person();
p1.APJob("净坛使者?");
Console.WriteLine(p1.GetJob());
Console.ReadKey();
}
}
class person
{
private string job;
public void APJob(string job)
{
this.job=job;
}
public string GetJob()
{
return this.job;
}
}
安排一个净坛使者
或者是这样,拒绝当净坛使者,则不显示。但如果上图中加p1.job = “净坛使者”,下图private为public,即使拒绝也还是会显示净坛使者,就像在脑门上贴上了。即如果直接修改字段是无法拒绝非法值的。
我觉得这样的赋予工作更舒服:
下边继续:
这样看起来麻烦,可否直接public string job呢?
看下边:
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.Job = "1";
Console.WriteLine(p.Job);
Console.ReadKey();
}
}
class Person
{
private string job;
public string Job
{
get
{
return job;
}
set
{
if (value == "1")
{
this.job = "大爷";
return;
}
this.job = value;
}
}
}
其实可以看出 如果public,那么谁都可以修改job,但如果是这样的,如果拒绝当waitor,用public你就没法拒绝觉了。
和使用字段相比,用属性可以拒绝非法值。
class Program
{
static void Main(string[] args)
{
Person p = new Person();
Console.WriteLine(p.EyeCount);
Console.ReadKey();
}
}
class Person
{
public int EyeCount
{
get
{
return 2;
}
}
}
//不写set就是只读属性
不写set就是只读属性;属性返回的值不一定非要存到私有字段中,怎么处理无所谓
如果暂时没有对非法值控制,只是普通属性,可以用这种简写;
编译器的技巧,语法糖。编译器的花儿招,让程序员用起来方便。
继承
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.Age = 30;
p.Age = p.Age + 1;
Console.WriteLine(p.Age);
Console.ReadKey();
}
}
class Person
{
private int age;
public int Age
{
get
{
return 20;
}
set
{
this.age = value;
}
}
}
打印出来依旧是20。
class Program
{
static void Main(string[] args)
{
person p = new person();
p.Age = 30;
p.Age = p.Age + 1;
p.Name = "Tom";
Console.WriteLine(p.Age);
Console.ReadKey();
}
}
class person
{
private int age;
public int Age
{
get
{
return 20;
}
set
{
this.age = value;
}
}
private string name;
public string Name
{
get
{
return name;
}
set
{
this.name = value;
}
}
这样就是tom了
注意:
//用简写方式不能只有get或者只有set。不合理。如下:
//public int Height
//{
// get;
//}
练习:
class Program
{
static void Main(string[] args)
{
宠物 pet =new 宠物();//所有while循环都操作这个宠物。
while (true)
{
Console.WriteLine("来玩我啊(1.说话;2洗澡;3玩球;4.喂饭)");
string key = Console.ReadLine();
if (key == "1")
{
Console.WriteLine("想聊点啥?");
string dialog = Console.ReadLine();
pet.Speak(dialog);
}
else if (key == "2")
{
pet.Wash();
}
else if (key == "3")
{
pet.PlayBall();
}
else if (key == "4")
{
Console.WriteLine("输入喂的数量");
string count = Console.ReadLine();
pet.Feed(Convert.ToInt32(count));
}
else
{
Console.WriteLine("选择错误");
}
Console.WriteLine(pet.report());
}
Console.ReadKey();
}
}
class 宠物
{
private int health=25;//有默认值,字段中可以不赋值
private int fullLevel=20;
//private int age;
public void Speak(string s)
{
if (health <= 0)
{
Console.WriteLine("老子死了,还玩尼玛啊?!");
return;
}
if (fullLevel <= 0)
{
Console.WriteLine("老子饿了,还玩尼玛啊?!");
return;
}
health = health - 1;
fullLevel = fullLevel - 2;
if (s.Contains("他妈")||s.Contains("你妈")||s.Contains("你妹"))
{
Console.WriteLine("好好说话,曹尼玛的!");
return;
}
if(s.Contains("你好"))
{
Console.WriteLine("你好");
return;
}
if (s.Contains("名字")||s.Contains("叫什么"))
{
Console.WriteLine("我叫煞哔");
return;
}
Console.WriteLine("你爹我听不懂,滚!");
}
public void Wash()
{
health = health + 5;
}
public void PlayBall()
{
if (health <= 0)
{
Console.WriteLine("老子死了,还玩尼玛啊?!");
return;
}
if (fullLevel <= 0)
{
Console.WriteLine("老子饿了,还玩尼玛啊?!");
return;
}
health = health + 10;
fullLevel = fullLevel - 5;
}
public void Feed(int foodcount)
{
if (foodcount <= 0)
{
Console.WriteLine("滚!");
}
if (foodcount > 5)
{
health = -1;
Console.WriteLine("我撑死了,艹!");
return;
}
else
{
fullLevel = fullLevel + foodcount;
}
}
public string report()
{
return string.Format("health={0},fullLevel={1}",health,fullLevel);
}
}
继承→
class Program
{
static void Main(string[] args)
{
Chinese b = new Chinese();
//b.可以点出Person的东西,继承了
Japanese p = new Japanese();
//p.点不出户口,缺能点出PoFu。
p.PoFu();
Console.ReadKey();
}
}
class Person
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public void Run()
{
Console.WriteLine("PiaPia的!");
}
}
class Chinese : Person//: 后为父类的名字
{
public string HuKou
{
get; set;
}
}
class Japanese : Person
{
public void PoFu()
{
Console.WriteLine("红的");
}
}
其中:
static void Main(string[] args)
{
Chinese c = new Chinese();
Chinese c1 = c;//让c1指向c所指向的对象
c1.Run();//执行c1所指向的对象的Run方法 Console.ReadKey();
}
下边上同
其中:
static void Main(string[] args)
{
Chinese c = new Chinese();
Person p1 = c;//父类的变量可以指向子类的对象
p1.Run();
Console.ReadKey();
}
下边上同
其中:
class Program
{
static void Main(string[] args)
{
Chinese c = new Chinese();
Person p1 = c; //c从变量看是一个chinese,所以可以把person标签贴到上
c.Run();
//p1从变量看是一个person,就不能擅自把chinese标签贴到p1上
//Chinese c2 = p1;//把子类的变量指向父类的变量就不成功,必须显式转换
Chinese c2 = (Chinese)p1;//可以
Console.ReadKey();
}