下面是自己写的两个例子,挺有代表性的,一看就知道是什么了。。。using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace C_sharp_control
{
public class person
{
protected readonly int id;
protected string name;
public person(int id, string name)
{
this.id=id;
this.name=name;
}
public int ID
{
get
{
return this.id;
}
}
public string Name
{
get
{
return name;
}
set
{
this.name=value ;
}
}
public void show()
{
Console.WriteLine("I'm a person");
}
}
public class student : person
{
protected int math;
protected int chinese;
protected int total;
public student(int id, string name, int math, int chinese):base(id,name)
{
this.math=math;
this.chinese=chinese;
this.total=this.math+this.chinese;
}
public int Math
{
get
{
return this.math;
}
set
{
this.math=value;
}
}
public int Chinese
{
get
{
return this.chinese;
}
set
{
this.chinese=value;
}
}
public void show()
{
Console.WriteLine("I'm a student");
}
}
public class goodstudet : student
{
private int scholarship;
public goodstudet(int id, string name, int math, int chinese, int scholarship)
: base(id, name, math, chinese)
{
this.scholarship = scholarship;
}
public int Scholarship
{
get
{
return this.scholarship;
}
set
{
this.scholarship=value ;
}
}
public void show()
{
Console.WriteLine("I'm a good student");
}
}
class Program
{
static void show(person per)
{
per.show();
}
static void Main(string[] args)
{
person []array={new person(1,"boy"),new student(1,"NOY",44,55),new goodstudet(1,"Girl",23,54,56654)};
for (int i = 0; i < 3; i++)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace C_sharp_control
{
public class person
{
protected readonly int id;
protected string name;
public person(int id, string name)
{
this.id=id;
this.name=name;
}
public int ID
{
get
{
return this.id;
}
}
public string Name
{
get
{
return name;
}
set
{
this.name=value ;
}
}
public virtual void show()
{
Console.WriteLine("I'm a person");
}
}
public class student : person
{
protected int math;
protected int chinese;
protected int total;
public student(int id, string name, int math, int chinese):base(id,name)
{
this.math=math;
this.chinese=chinese;
this.total=this.math+this.chinese;
}
public int Math
{
get
{
return this.math;
}
set
{
this.math=value;
}
}
public int Chinese
{
get
{
return this.chinese;
}
set
{
this.chinese=value;
}
}
public override void show()
{
Console.WriteLine("I'm a student");
}
}
public class goodstudet : student
{
private int scholarship;
public goodstudet(int id, string name, int math, int chinese, int scholarship)
: base(id, name, math, chinese)
{
this.scholarship = scholarship;
}
public int Scholarship
{
get
{
return this.scholarship;
}
set
{
this.scholarship=value ;
}
}
public override void show()
{
Console.WriteLine("I'm a good student");
}
}
class Program
{
static void show(person per)
{
per.show();
}
static void Main(string[] args)
{
person []array={new person(1,"boy"),new student(1,"NOY",44,55),new goodstudet(1,"Girl",23,54,56654)};
for (int i = 0; i < 3; i++)
{
array.show();
}
}
}
}
作者: 蔚强 时间: 2012-3-23 01:19
public class People
{
private string name;
private int age;
public People() { }//这是没有参数的构造函数
public People(string name, int age)//这是有俩个参数的构造函数
{
this.name = name;
this.age = age;
}
public virtual void Print()
{
Console.WriteLine("这是开山老祖创的秘籍!");
}
}
public class ChinesePerson : People
{
public override void Print()
{
Console.WriteLine("这是中国人自己创的武林秘籍");
}
}
public class My : People
{
public override void Print()
{
Console.WriteLine("这是我自己自创的武功秘籍!");
}
}
People p = new People();
p.Print();这是第一种检验覆盖父类方法的
ChinesePerson Chinesse = new ChinesePerson();
Chinesse.Print();
My my = new My();
my.Print();
第二种的话是利用类的多态性,因为下面俩个子类是继承People父类的
People []p = new People [2];
for(int i=0;i<2;i++)
{
p[i] = new People();
// p[i].Print();
}
p[0] = new ChinesePerson();
p[0].Print();
p[1] = new My();
p[1].Print();
其实我也是C#新手,感觉还是自己写但马会理解的更加清晰一点的,我建议你以后有什么地方不明白,自己写代码验证这到底是怎么回事。这样你自己真的会有所收益的!