[Unity&C#&继承]如何新增新的对象在已经继承的子类中

来源:互联网 发布:淘宝3c数码类目包括 编辑:程序博客网 时间:2024/05/16 19:18

A.cs为父类,B.cs为 A 的子类。B : A{}

实际上 ,可以理解 为 把 A 的 公共 成员 的函数,对象,变量,全部   复制 给了 B.cs


在上一篇文章 参考资料1 里面,测试了一些 子类 继承 父类,改变 父类 的函数 里面的 内容 会发生什么情况。

本文就 探究,父类 A.cs  的 公共class test_class_A 与 子类 继承的关系有什么影响。

using UnityEngine;

public class A : MonoBehaviour
{

public class test_class_A

{

public int test_1;

}

}

----------------------------------------------------------------------------------------------------------------

1.1


和 上一篇文章 参考资料 1 里面 的结论一样,子类 覆盖了父类 的函数。

----------------------------------------------------------------------------------------------------------------

有3个 脚本 A.cs B.cs C.cs

A.cs是父类,B.cs继承A.cs,B是A的子类。

C使用代码 来调用B 的函数,使其 全部显示。


如何在 已经从父类那里继承过来的 子类 中,添加 新的对象。

B.cs继承A.cs,怎么在 B.cs 的内部类 中 新增 新 的 变量。(这种技巧在JSON 数据使用中很常见。)


实际的例子,A.cs = Test_Father

B.cs = Test_Son

C.cs = Test_FSChange


A.cs = Test_Father

using System.Collections;using System.Collections.Generic;using UnityEngine;public class Test_Father : MonoBehaviour{    public void test_father()    {        //int test_father_int = 666;        //Debug.Log("  test_father  string :" + str_1);    }    public class test_f_1    {        public string str_1 = "string_1";        public string str_2 = "string_2";        public void test_f_function()        {            Debug.Log("  test_father  str_1:" + str_1+ "  test_father  str_2:" + str_2);        }    }}

B.cs = Test_Son

using System.Collections;using System.Collections.Generic;using UnityEngine;public class Test_Son : Test_Father {    //public test_f_1 tf_1;           /* public class test_f_1    {       public string str_3 = "string_3";    }*/    public void test_s_function()    {        test_f_1 tf1 = new test_f_1();        string str_1 = tf1.str_1;        string str_2 = tf1.str_2;        string str_3 = "string3";        string str_4 = "string4";        tf1.test_f_function();        Debug.Log( "  str_1:  " + tf1.str_1+ "  str_2:  " + tf1.str_2 + "  str_3:  " + str_3+ "  str_4:  " + str_4);    }}

C.cs = Test_FSChange

using System.Collections;using System.Collections.Generic;using UnityEngine;public class Test_FSChange : MonoBehaviour {    private Test_Son ts;// Use this for initializationvoid Start () {        Test_Son ts = new Test_Son();        ts.test_s_function();    }}
------------------------逻辑结果图


-------------------------------------

那么把结果换成 子类 的内部类 继承  父类的内部类 并 新增 新的 变量


A.cs = Test_Father

using System.Collections;using System.Collections.Generic;using UnityEngine;public class Test_Father : MonoBehaviour{    public void test_father()    {        //int test_father_int = 666;        //Debug.Log("  test_father  string :" + str_1);    }    public class test_father_1    {        public string str_1 = "string_1";        public string str_2 = "string_2";        public void test_f_function()        {            Debug.Log("  test_father  str_1:" + str_1+ "  test_father  str_2:" + str_2);        }    }}

B.cs = Test_Son

using System.Collections;using System.Collections.Generic;using UnityEngine;public class Test_Son : Test_Father {    //public test_f_1 tf_1;   /* public class test_f_1    {       public string str_3 = "string_3";    }*/    public class test_son_1 : test_father_1    {        public test_father_1 tf1 = new test_father_1();        //string str_1 = tf1.str_1;        //string str_2 = tf1.str_2;        public string str_3 = "string3";        public string str_4 = "string4";        //tf1.test_f_function();       /* Debug.Log( "  str_1:  " + //tf1.str_1            + "  str_2:  " + //tf1.str_2            + "  str_3:  " + str_3+            "  str_4:  " + str_4);*/    }}
C.cs = Test_FSChange
using System.Collections;using System.Collections.Generic;using UnityEngine;public class Test_FSChange : Test_Son {    private Test_Son TS;    private test_son_1 ts1;// Use this for initializationvoid Start () {        ts1 = new test_son_1();        // test         Debug.Log( "  str_1:  " + ts1.str_1             + "  str_2:  " + ts1.str_2             + "  str_3:  " + ts1.str_3 +             "  str_4:  " + ts1.str_4);    }}
------------------------逻辑结果图

子类 怎么才能继承 父类 的内部类的变量,然后新增新的变量

A.cs 有个 内部类 class a,class a 有 2个 string 变量 str_1 = "string_1";    string 变量 str_2 = "string_2";

B.cs 是 A.cs 的子类,B.cs 的内部类 class b : class a{},并在 class b 中 新增 2个 string 变量 str_3 = "string_3";    string 变量 str_4 = "string_4";

在C.cs : B.cs ,在C.cs中 显示 B.cs 的 class b 的 4个 string 类型 变量。


(A.cs = Test_Father) : (MonoBehaviour)

{

(class a = test_father_1)

}

(B.cs = Test_Son) : (A.cs = Test_Father)

{

(class b = test_son_1) : (class a = test_father_1)

}

(C.cs = Test_FSChange)  : (B.cs = Test_Son)


只有 一个类 继承 另 一个 class newClass ,才能 使用 newClass nc = newClass();


结论:子类的内部类 继承(父类的内部类)并添加 新的变量 的时候,需要在 子类的内部类 中 新建一个 新的 (父类的内部类)变量 即可。

结合逻辑结果图查看

(B.cs = Test_Son) : (A.cs = Test_Father)

{

(class b = test_son_1) : (class a = test_father_1)

//新增变量

}

----------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------

参考资料:

1.

[Unity&C#&继承]unity怎么改变继承子类的父类的函数

2.内部类——内部类的继承

3.

4.

5.

6.

7.