官术网_书友最值得收藏!

Time for action – Experiments with inheritance

Let's add a variable to our AwesomeGun class and see how it works with another class we'll create.

  1. Add an int to our AwesomeGun class called MyInt. Our code should now look like this:
    class AwesomeGun extends UTWeap_RocketLauncher_Content;
    
    var int MyInt;
    defaultproperties
    {
        FireInterval(0)=0.1
        ShotCost(0)=0
    }
  2. Now create another class in our AwesomeGame/Classes folder called AnotherGun.uc. Type the following code into it:
    class AnotherGun extends AwesomeGun;
    
    defaultproperties
    {
        MyInt=4
    }
    
  3. Compile the code. We'll see that it compiles fine as our AnotherGun is inheriting MyInt from AwesomeGun.
  4. Now let's change the class we're extending from to be the same as AwesomeGun's parent class:
    class AnotherGun extends UTWeap_RocketLauncher_Content;
    
    defaultproperties
    {
        MyInt=4
    }
  5. Now when we compile, we'll get a warning:
    Warning, Unknown property in defaults: MyInt=4

What just happened?

Even though the classes extend off of the same class, inheritance only happens when the class we want to use the variable in is inside the one that declares the variable in the class tree. We can change the default property of the variable for our class, and this is how we get different functionality out of them such as our example with the firing rate.

主站蜘蛛池模板: 田林县| 三穗县| 镇坪县| 中山市| 巫山县| 澄迈县| 吉首市| 香河县| 庆阳市| 迭部县| 大同市| 称多县| 北流市| 如东县| 甘肃省| 秦安县| 驻马店市| 海伦市| 甘德县| 泽州县| 中山市| 鄂托克前旗| 宝兴县| 九龙坡区| 内黄县| 嘉义县| 当阳市| 玉溪市| 西昌市| 大荔县| 张家港市| 砀山县| 刚察县| 二连浩特市| 北辰区| 福州市| 波密县| 巴林左旗| 临漳县| 南京市| 宣城市|