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

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.

主站蜘蛛池模板: 长乐市| 宁国市| 承德市| 三原县| 大厂| 陵水| 玛纳斯县| 诸暨市| 凤城市| 曲阜市| 偃师市| 永定县| 宣城市| 麻栗坡县| 汝南县| 黄冈市| 绥棱县| 洛扎县| 宁化县| 游戏| 永善县| 政和县| 沙湾县| 云梦县| 潮安县| 门源| 灵宝市| 贵州省| 绥芬河市| 申扎县| 隆德县| 广灵县| 宁国市| 金门县| 陆良县| 永泰县| 青阳县| 龙南县| 鄂州市| 井研县| 元谋县|