学无止境

Life-long learning
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

构造函数

Posted on 2008-08-04 18:02  anytime8  阅读(186)  评论(0编辑  收藏  举报

using System;

public class Person
{
 public string name = "";
 public int age = 0;

 //默认构造函数
 public Person()
 {
 }

 //构造函数重载(1)
 public Person(int Age)
 {
  this.age = Age;
 }

 //构造函数重载(2)
 public Person(int Age, string Name)
 {
  this.age = Age;
  this.name = Name;
 }

 public void ShowInfo()
 {
  Console.WriteLine("The name is : " + name);
  Console.WriteLine("The age is:" + age);
 }
}

class Client
{
 public static void Main()
 {
  Person p1 = new Person();
  p1.ShowInfo();

  Console.WriteLine("==========================");

  Person p2 = new Person(30);
  p2.ShowInfo();

  Console.WriteLine("==========================");
  Person p3 = new Person(30, "Tom");
  p3.ShowInfo();
 }
}