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 10; } } 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-----------
作者:阿笨
【官方QQ一群:跟着阿笨一起玩NET(已满)】:422315558
【官方QQ二群:跟着阿笨一起玩C#(已满)】:574187616
【官方QQ三群:跟着阿笨一起玩ASP.NET(已满)】:967920586
【官方QQ四群:Asp.Net Core跨平台技术开发(可加入)】:829227829
【官方QQ五群:.NET Core跨平台开发技术(可加入)】:647639415
【网易云课堂】:https://study.163.com/provider/2544628/index.htm?share=2&shareId=2544628
【51CTO学院】:https://edu.51cto.com/sd/66c64
【微信公众号】:微信搜索:跟着阿笨一起玩NET

浙公网安备 33010602011771号