using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 面向对象
{
class Program
{
static void Main(string[] args)
{
//peason p1 = new peason();
//p1.Name = "wangdong";
//p1.Age = 24;
//p1.Hight=180;
//p1.SayHello();
// peason2 p2 = new peason2();
// //p2.Name = "wangdong";
// p2.Age = 24;
// p2.Hight = 180;
// p2.givename("tom");
p2.givename("wangdong");
// //p2.zhangyan();
// p2.SayHello();
//peason3 p3=new peason3() ;
//p3.Age = -2;
//Console.WriteLine("age is : {0}",p3.Age);
//peason4 p4 = new peason4();
//p4.Age = 30;
//p4.Age = p4.Age + 1;
//Console.WriteLine(p4.Age);
//person5 p5 = new person5();
//p5.Age = 30;
//Console.WriteLine(p5.Age);
// person6 p6 = new person6();
// p6.incAge();
// p6.incAge();
p6.age = 30;
// Console.WriteLine("年龄{0}",p6.Age);
person7 p7 = new person7();
p7.Age = 30;
Console.WriteLine(p7.Age);
Console.ReadKey();
}
}
class peason
{
public int Hight;
public int Age;
public string Name;
public void SayHello()
{
Console.WriteLine("Hello!,my name is{0},age is{1},hight is{2}", this.Name, this.Age, this.Hight);
}
}
class peason2
{
public int Hight;
public int Age;
private string Name;
public void givename(string name) //geigefangfajiukeyidianle!
{
if (name=="tom")
{
return;
}
this.Name = name;
}
public void SayHello()
{
Console.WriteLine("Hello!,my name is{0},age is{1},hight is{2}", Name, Age, Hight);
}
private void zhangyan()
{
Console.WriteLine("zhangyan");
}
}
//属性 属性字母开头大写 字段开头小写
class peason3
{
private int age;
public int Age //属性可以自己判断!
{
set { //赋值
if (value < 0)
{ return; }
this.age = value; }
get { return this .age;} //获取
}
//Age 没有保存数据 都是age存的
}
class peason4
{
public int Age
{
set { } //赋值
get { return 3; } //获取
}
}
class person5 //死循环 自己给自己赋值!
{
private int age;
public int Age
{
set { this.Age=value;}
get { return this.Age; }
}
}
class person6
{
private int age;
public int Age
{
get{ return age;}
}
public void incAge()
{
age++;
}
}
class person7
{
public int Age
{
set;
get; //编译器会在自己帮助我们生成私有字段和set,get代码块!
}
public string Name
{
set;
get;
}
}
}
namespace 面向对象2 //对象的引用
{
class Program
{
static void Main(string[] args)
{
int i = 10;
int j = i;
i++;
Console.WriteLine(j);
// int datatime bool char 等是属于值类型(valuetype),赋值的时候是传递拷贝!
// 普通对象是引用类型 直接把对象赋给它
// 注意:对象占的内存比较大 公用比较省内存 指向问题!
person p1 = new person(10);
person p2 = p1;
p1.Age++;
Console.WriteLine(p2.Age); //指向对象的对象
Console.ReadKey();
}
}
class person
{
public int Age { get; set; }
public person(int age)
{
this.Age = age;
}
}
}