using System;
using System.Collections.Generic;
using System.Linq;
{
class Program
{
static void Main(string[] args)
{
human a = new human();
a.PrintBase();
//a.Print();//基类的对象不能调用子类的方法
//Women c=new Women (175.3,"李四",23);
Women c = new Women();
c.Print();
Console.Read();
}
}
public class human
{
human a;
protected int age;
public string Name;
public human()
{
Name = "张三";
}
public human(int a, string n)
{
age = a;
Name = n;
}
public void PrintBase()
{
Console.WriteLine ("{0}的年龄是 {1} ",Name ,age );
}
}
public class Women:human
{
double hight;
new string Name;
public Women()
{
Name = "李四";
}
public Women(double h,string N,int i)
{
hight = h;
Name = N;
age = i;
}
public void Print()
{ Console.WriteLine("{0}的身高是:{1},年龄是:{2}",Name,hight ,age ); }
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
{
class Program
{
static void Main(string[] args)
{
human a = new human();
a.PrintBase();
//a.Print();//基类的对象不能调用子类的方法
//Women c=new Women (175.3,"李四",23);
Women c = new Women();
c.Print();
Console.Read();
}
}
public class human
{
human a;
protected int age;
public string Name;
public human()
{
Name = "张三";
}
public human(int a, string n)
{
age = a;
Name = n;
}
public void PrintBase()
{
Console.WriteLine ("{0}的年龄是 {1} ",Name ,age );
}
}
public class Women:human
{
double hight;
new string Name;
public Women()
{
Name = "李四";
}
public Women(double h,string N,int i)
{
hight = h;
Name = N;
age = i;
}
public void Print()
{ Console.WriteLine("{0}的身高是:{1},年龄是:{2}",Name,hight ,age ); }
}
}
类的继承:
主函数中用子类实例化一个无参对象时,程序的运行过程值得注意,因为,它首先走到了子类的默认构造函数处,不进去{ }里面,就直接走去基类的默认构造函数处,走完整个基类的构造函数后(包括{里面的内容}),才会又回到子类的默认的构造函数处,这次,会进入到{ }里面。而且,这种现象在子类实例化有参对象是同样适用。
在基类中已有默认的构造函数时,再在子类中写默认的构造函数,不用写override关键字
(1)当基类中没有自己编写的构造函数时,派生类默认调用基类的构造函数
(2)当基类中有自己编写的构造函数时,要在基类中添加无参的构造函数
(3)在基类中有自己编写的构造函数并且在基类中没有添加无参的构造函数时,要在派生类中使用Base指定基类的构造函数