C#基础知识—父类和子类的关系

时间:2023-03-09 01:59:13
C#基础知识—父类和子类的关系

基础知识一:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms; namespace WindowsFormsApplication2
{
public class ParentClass
{
public ParentClass()
{ }
public string NamePropety { get; set; } public string GetName()
{
return "";
}
} public class ChildClass : ParentClass
{
public ChildClass()
{ } public int Age { get; set; } public int GetAge()
{
return ;
}
} static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//=>1、实例化父类
ParentClass parent = new ParentClass();
string _NamePropety = parent.NamePropety;
string _name = parent.GetName(); //1.1向上转型 子类转父类
ParentClass parent1 = new ChildClass(); //或者ParentClass parent1 = new ChildClass() as ParentClass;
string _NamePropety1 = parent1.NamePropety;
string _name1 = parent1.GetName(); //=>2、实例化子类
ChildClass child = new ChildClass();
string _NamePropety2 = child.NamePropety;
string _name2 = child.GetName();
int ageName2 = child.GetAge();
int age2 = child.Age; //2.1向下转型 父类转换子类。
ParentClass child3 = new ChildClass();
ChildClass child4 = (ChildClass)child3;
string _NamePropety3 = child4.NamePropety;
string _name3 = child4.GetName();
int ageName3 = child4.GetAge();
int age3 = child4.Age; //=>3、不正确的父类转子类。 //as方式转换。(as 转换失败时,程序不会抛异常,child1对象为NULL。)
ChildClass child1 = new ParentClass() as ChildClass; //或者 ChildClass child1 = (ChildClass)new ParentClass();
Console.WriteLine(child1.NamePropety); //强制转换。(程序会抛出异常。)
ChildClass child1_1 = (ChildClass)new ParentClass(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
} }
}

基础知识二:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IBO.XJMYQP.ControlLib; namespace IBO.XJMYQP.UI
{
public class ParentClass
{
public ParentClass()
{
Console.WriteLine("初始化父类构造函数");
}
public virtual void Test1()
{
Console.WriteLine("我是基类的Test1");
}
public void Test2()
{
Console.WriteLine("我是基类的Test2");
}
public virtual void Test3()
{
Console.WriteLine("我是基类的Test3");
}
//=>//protected访问修饰符在大多数资料中的定义:访问仅限于包含类或从包含类派生的类型。包含类指的父类
protected void Test4()
{ }
} public class ChildClass : ParentClass
{
public ChildClass()
{
Console.WriteLine("初始化子类构造函数");
}
public override void Test1()
{
Console.WriteLine("我是子类的Test1");
} public new void Test2()
{
Console.WriteLine("我是子类的Test2");
} public new void Test3()
{
Console.WriteLine("我是子类的Test3");
} } static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Console.WriteLine("-------(1)、new ParentClass()用于调用的都是基类 Begin-----------");
//=》调用的是基类。
ParentClass b1 = new ParentClass();
b1.Test1();
ParentClass b2 = new ParentClass();
b2.Test2();
ParentClass b3 = new ParentClass();
b3.Test3();
Console.WriteLine("-------END-----------"); Console.WriteLine("-------(2)、override关键字与父类的virtual 关键字 Begin-----------"); //=>override 关键字,重写父类的方法。只要 new ChildClass()后,不管对象转化谁调用的都是子类重写方法。
ParentClass p1 = new ChildClass();
p1.Test1();
ChildClass c1 = new ChildClass();
c1.Test1(); Console.WriteLine("-------END-----------"); Console.WriteLine("-------(3)、new 关键字 Begin-----------"); ParentClass p2 = new ChildClass();
p2.Test2();
ChildClass c2 = new ChildClass();
c2.Test2(); Console.WriteLine("-------END-----------"); Console.WriteLine("-------(4)、new 关键字与父类的virtual Begin-----------"); //=>new 关键字,就是独立子类与父类的相同方法,转化为谁后调用的就是谁。
ParentClass p3 = new ChildClass();
p3.Test3();
ChildClass c3 = new ChildClass();
c3.Test3(); Console.WriteLine("-------END-----------"); Console.ReadKey(); }
}
}

  输出:

-------(1)、new ParentClass()用于调用的都是基类 Begin-----------
初始化父类构造函数
我是基类的Test1
初始化父类构造函数
我是基类的Test2
初始化父类构造函数
我是基类的Test3
-------END-----------
-------(2)、override关键字与父类的virtual 关键字 Begin-----------
初始化父类构造函数
初始化子类构造函数
我是子类的Test1
初始化父类构造函数
初始化子类构造函数
我是子类的Test1
-------END-----------
-------(3)、new 关键字 Begin-----------
初始化父类构造函数
初始化子类构造函数
我是基类的Test2
初始化父类构造函数
初始化子类构造函数
我是子类的Test2
-------END-----------
-------(4)、new 关键字与父类的virtual Begin-----------
初始化父类构造函数
初始化子类构造函数
我是基类的Test3
初始化父类构造函数
初始化子类构造函数
我是子类的Test3
-------END-----------