C# foundation of Unity learning 21 -- inheritance, as usage, static

Keywords: C# Unity

1. Succession

A subclass can use members of the parent class. A parent class cannot use subclass members, but only its own members.

A reference of a parent type points to an object of a child class. Only parent class members can be used. If you need to access subclass members, you need to cast.

  definition:
    class Gun
    {
        public string GunName { get; set; }
        public string GunAmmoType { get; set; }
        public int GunAmmoNum { get; set; }
        public decimal GunShotRange { get; set; }
        

    }

    class Rifle : Gun
    {
        private bool useGunstock;
        protected bool UseAccessories { get; set; }
        public bool UseGunstock
        {
            set { useGunstock = value; }
            get { return useGunstock; }
        }

        
    }
    [Flags]
    enum GunUse
    {
        FarMoreShot=0,
        AntiTank=2,
        AntiAir=4
    }
    class SniperGun : Rifle
    {
        public GunUse GunUseType { get; set; }

    }
Call:
            Gun M4A1 = new Gun();
            M4A1.GunName = "M4A1";
            Rifle M14 = new Rifle();
            M14.GunName = "M14";
            M14.UseGunstock = true;
            SniperGun AWP = new SniperGun();
            AWP.GunName = "AWP";
            AWP.GunUseType = GunUse.FarMoreShot;

2. Usage of as:

When we have two different subclasses and use cast to change from one subclass to another, we will not report an error, but the runtime will throw an exception.

However, using as can avoid exceptions, but the conversion will not succeed. Here is an example:

definition:

    class Rifle : Gun
    {
        private bool useGunstock;
        protected bool UseAccessories { get; set; }
        public bool UseGunstock
        {
            set { useGunstock = value; }
            get { return useGunstock; }
        }

        
    }
    class MachineGun : Gun
    {
        public GunUse GunUseType { get; set; }
    }

Call:

            Gun gun3 = new MachineGun();
            MachineGun gun4 = (MachineGun)gun3;
            Rifle gun5 = gun3 as Rifle;

3.static

static can be used for values that need to be called simultaneously in different instance objects. See an example for the specific meaning:

Suppose we want to view a class. We instantiate several instance objects. What should we do? Look at the difference between the following two methods:

Class:
class Gun
    {
        public string GunName { get; set; }
        public string GunAmmoType { get; set; }
        public int GunAmmoNum { get; set; }
        public decimal GunShotRange { get; set; }

        public int dynamicNum;
        public static int staticNum;
        public Gun()
        {
            dynamicNum++;//Here we increase the dynamic count
            staticNum++;//Here we increase the static count
        }
    }
Call:
static void Main()
        {
            Gun gun1 = new Gun();
            Gun gun2 = new Gun();
            Gun gun3 = new Gun();
            Gun gun4 = new Gun();
            Console.WriteLine(gun4.dynamicNum);//Output dynamic count from 0 - > 1
            Console.WriteLine(Gun.staticNum);//Call static variables with class name from 0 - > 4

        }

When we declare a static variable, we need to use the type name instead of the instance name.

Of course, there are many static things, as follows:

Static member variables:

  • This is why we use instances to refer to instances while we use classes to call static instances.
  • When our class is declared without new, static member variables have been loaded.

  Static constructor:

  • Static constructors cannot be modified with public, protected, private, etc.
  • Static constructor, generally used to initialize static data members of a class. The constructor we mentioned earlier is generally used to initialize the data members of the instance object.

  Static method:

  • In static methods or static code blocks, we can only access static members.
  • The meaning of this usage is a member variable in this instance object, so we use this.
  • Why can ordinary methods call static member variables? Because, with an instance object, the class must be referenced.

  Static class:

  After talking about all the static types above, let's see their applicability:

 

Posted by dannyluked on Mon, 11 Oct 2021 15:13:43 -0700