代码1:
using System;
using System.Text;
namespace 继承_重载_覆写
{
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.selfIntroduction();
p.selfIntroduction("大家好");
Console.WriteLine();
Person p1 = new Person("渣渣辉","male",45);
p1.selfIntroduction();
p1.selfIntroduction("搭嘎好");
Console.WriteLine();
Student s = new Student();
s.selfIntroduction();
s.selfIntroduction("hello");
Console.WriteLine();
Student s1 = new Student("zhangsan","male",30,"123");
s1.selfIntroduction();
s1.selfIntroduction("hello");
Console.WriteLine();
Student s2 = new Student("lisi","male",30,"456");
Person p2 = s2;//子类赋给父类(不需要强转)
p2.selfIntroduction();//虽然是以父类的名义调用,但调的仍然是子类的方法
p2.selfIntroduction("哈哈哈");
Console.WriteLine();
//Person p3 = new Person("wangwu","male",30);
//Student s3 = (Student)p3;//父类赋给子类(需要强转),编译时没有错误,调试时报错
//s3.selfIntroduction();
//s3.selfIntroduction("嘿嘿嘿");
//Console.WriteLine();
//object obj = "chen";
//string str = (string)obj;//父类强转子类,调试不报错
//int n = (int)obj;//父类强转子类,调试报错,有何感想
Person p4 = new Student("zhaoliu","male",16,"789");
Student s4 = (Student)p4;//父类强转子类,调试不报错
s4.selfIntroduction();
s4.selfIntroduction("啦啦啦");
Console.WriteLine();
ForeignStudent fs = new ForeignStudent();
fs.selfIntroduction();
fs.selfIntroduction("hihihi");
Console.WriteLine();
ForeignStudent fs2 = new ForeignStudent("Tom","xiaoming","male",30,"911");
fs2.selfIntroduction();
fs2.selfIntroduction("hehehe");
Console.WriteLine();
StringBuilder conclusion = new StringBuilder();
conclusion.Append("小结:\r\n");
conclusion.Append("1.覆写不能更改方法签名\r\n");
conclusion.Append("2.base指的是当前类的父类,不是父类的父类\r\n");
conclusion.Append("3.父类转子类会报错,如果父类就是用子类实例化的,就不会报错\r\n");
Console.WriteLine(conclusion.ToString());
Console.ReadLine();
}
}
}
代码2:
using System;
namespace 继承_重载_覆写
{
class Person
{
public string name;
public string sex;
public int age;
#region 构造方法重载
public Person()
:this("unknownPerson","unknownPerson",-1)//这个用法仅用于构造方法,在普通方法上不能用
{
//Console.WriteLine("这里是默认的Person构造方法");
}
public Person(string name, string sex, int age)
{
this.name = name;
this.sex = sex;
this.age = age;
//Console.WriteLine("这里是重载的Person构造方法");
}
#endregion
#region 成员方法重载
public virtual void selfIntroduction()
{
Console.WriteLine("Person --- 我是" + name);
}
public virtual string selfIntroduction(string greetings = "大家好")
{
Console.WriteLine("Person --- " + greetings + ",我是" + name);
return "";
}
#endregion
public void welcome()
{
Console.WriteLine("Person --- welcome " + name);
}
}
}
代码3:
using System;
namespace 继承_重载_覆写
{
class Student : Person
{
public string ID;
#region 构造方法重载
public Student()
: this("unknownStudent","unknownStudent",-1,"000000")
{
//Console.WriteLine("这里是默认的Student构造方法");
}
public Student(string name, string sex, int age, string ID)
: base(name,sex,age)
{
this.ID = ID;
//Console.WriteLine("这里是重载的Student构造方法");
}
#endregion
public override void selfIntroduction()
{
base.selfIntroduction();
Console.WriteLine("Student --- 我是" + ID);
}
}
}
代码4:
using System;
namespace 继承_重载_覆写
{
class ForeignStudent : Student
{
public string englishName;
public ForeignStudent()
:this("unknwonForeignStudent", "unknwonForeignStudent", "unknwonForeignStudent",-1,"000000")
{
}
public ForeignStudent(string englishName,string name,string sex,int age,string ID)
:base(name,sex,age,ID)
{
this.englishName = englishName;
}
public override void selfIntroduction()
{
base.selfIntroduction();
Console.WriteLine("ForeignStudent --- I am " + englishName);
}
}
}
调试结果: