using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//类和对象、构造函数、方法、访问修饰符
namespace T4
{
/// <summary>
/// Person类
/// </summary>
class Person
{
/// <summary>
/// Person类的默认构造函数
/// </summary>
public Person()
{
Console.WriteLine("这个是Person类的默认构造函数.");
}
public Person(string name)
{
Console.WriteLine("这个是Person类的单参构造函数:" + name + ".");
}
//以下为Person类的方法
public void Eating()
{
Console.WriteLine("person吃饭.");
}
public void Walking()
{
Console.WriteLine("person走路.");
}
private void Sleep()//访问级别为private所以派生类的实例无法访问
{
Console.WriteLine("person睡觉.");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//类的继承、构造函数的继承、new关键字的用法、this和base关键字的作用
namespace T4
{
/// <summary>
/// Children类,继承Person类
/// </summary>
class Children : Person
{
/// <summary>
/// Children类的默认构造函数
/// </summary>
public Children()
: base()
{
}
/// <summary>
/// Children类的单参构造函数
/// </summary>
/// <param name="name">姓名</param>
public Children(string name):this(name,15)
{
Console.WriteLine("这个是Children类的单参构造函数:" + name + ".");
}
/// <summary>
/// Children类的双参构造函数
/// </summary>
/// <param name="name">姓名</param>
/// <param name="age">年龄</param>
public Children(string name, int age)
{
Console.WriteLine("这个是Children类的双参构造函数.");
}
/// <summary>
/// 使用new关键字隐藏基类的Eating()方法
/// </summary>
new public void Eating()
{
//base代表基类,即Person类
base.Eating();
}
/// <summary>
/// 使用new关键字隐藏基类的Walking()方法
/// </summary>
new public void Walking()
{
//this代表当前类,即Children类
this.Eating();
Console.WriteLine("这个Children走路了.");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace T4
{
class Program
{
static void Main(string[] args)
{
//Children a = new Children();
//Children b = new Children("Mary");
//a.Eating();
//a.Walking();
//b.Eating();
//b.Walking();
Person p = new Person("Mary");
p.Walking();
Console.WriteLine("----------------------------------");
Children b = new Children("Mary");
//b.Walking();
b.Eating();
Console.Read();
}
}
}