ylbtech- .NET-Basic: A.3.2-创建简单的类(人类),包含的概念(字段,构造,封装字段,创建方法,创建对象,赋值,调用方法) |
A.3.2-创建简单的类(人类),包含的概念(字段,构造,封装字段,创建方法,创建对象,赋值,调用方法)
1.A,源代码返回顶部 |
1.A.1,Person.cs
using System;
namespace Test2
{
class Person
{
//字段
int id;//编号
string name;//姓名
int age;//年龄
//两参构造
public Person(string name, int age)
{
this.name = name;
this.age = age;
}
//全参构造
public Person(int id, string name, int age)
{
this.id = id;
this.name = name;
this.age = age;
}
//方法
public void Show()
{
Console.WriteLine("编号:{0},姓名:{1},年龄:{2}", id,name,age);
}
//封装字段
public int Id
{
get { return id; }
set { id = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
}
}
1.A.2,Program.cs
using System;
namespace Test2
{
class Program
{
static void Main(string[] args)
{
Person p = new Person(10000, "jik", 4);
//调用方法
p.Show();
Console.Read();
}
}
}
![]() |