Javascript required
Skip to content Skip to sidebar Skip to footer

Unity C# Reading a List of Objects From a Json File

What are Interfaces

Interfaces are a set of methods, properties, and other members that a target class must implement.

C# is a type-safe linguistic communication, meaning information technology checks if every statement is using Data Types correctly, by using an interface every bit your Information Blazon, you lot are able to admission the interface fields on unlike classes.

Build an Interface

The interface declaration is pretty similar to a class declaration:

The idea is to accept multiple classes that implement the IDamageable course, so we can reference the Interface rather than the classes themselves. In this example, nosotros could have the Thespian, Enemies, and Props all implement the IDamegeable, this way if we get a collision in Unity, we cheque if information technology is IDamegeable, and use corresponding damage, regardless of its class.

Usage

The main differences between classes and interfaces are:

  • Interfaces tin't declare variable fields or constructors.
  • Interface members don't have access modifiers, as they are all public.
  • Interfaces don't declare methods or properties implementations, simply their declarations. Each grade that implements the interface volition have its own methods or properties trunk.

It is a good do to always name an interface with a starting 'I'.

Implementing an Interface

To use an interface we demand get-go to declare that a class implements the interface, and then implement each member of that interface.

Usage

  • During the class declaration, subsequently the class name, add a colon ':' followed by the interface name.
  • The course must so implement every member alleged in the interface, making them all public.

If you forget to implement a fellow member of the interface, y'all will go a compilation error in the unity console like to this: error CS0535: 'Enemy' does not implement interface member 'IDamageable.ApplyDamage(int)'.

Multiple classes implementing an Interface

Similar I said before, interfaces really shine when there are multiple classes implementing it, that's when we tin can treat the classes that implement them as a unmarried blazon.

Tip: "CurrentHealth => isBroken ? 0 : 1;" is a shorthand for declaring a getter body. This is called a lambda expression in example you want to dig deeper.

Inheritance

Inheritance is like an interface on steroids, while interfaces are thought of as what a class DOES, inheritance is what a grade IS. Inheritance will not merely give you the actions that each course that inherits from it has, but likewise some or all implementations of said members, with a bonus of beingness able to declare fields and constructors.

Oh wow! So why don't we just use inheritance you enquire.

Beginning, at that place is a hard condition, we tin simply inherit from a single grade, but can implement multiple interfaces. But also, from a design perspective, they are unlike, again: interfaces are what a class does, the other what a class IS.

In inheritance, i class inherits the members of some other grade, so to help with communication, the inherited class is called the Super Class or Base Class, while the one that inherits is the Subclass or Derived Class.

Inheritance is groovy to tackle the abiding problem of lawmaking reuse, by having the shared implementations in the base grade, if we ever need to change that code, we only need to change it in a single place.

Declaration

Inheritance uses only things we are already familiar with, declare the base class as you would any other class, and as for the derived class declare that it inherits from the base class every bit you would declare it implements an interface.

In this example, the new class Orc has iii public members:

  • Shout, implemented past the derived class Orc
  • CurrentHealth and ApplyDamage, inherited from the base grade Enemy

Usage

  • A class tin inherit from a unmarried form and implement multiple interfaces, during declaration the interfaces must be declared subsequently the base class inheritance.
  • A base class tin can in turn inherit from another class, this fashion inheritance becomes like a tree.

Protected Admission Modifier

As a reminder, public members can be accessed from anywhere, while private members can only exist accessed by the aforementioned class that declared them. But what if I wanted to access a base class field, but still go along information technology blocked to others?

Yeah, the protected access modifiers, who would accept guessed. Simply use it like you would any other access modifier 😉

Override Inherited Members

So, what if nosotros wanted to make that ApplyDamage piece of work a bit differently on orcs, or even completely different? That is why nosotros have overrides.

When yous want to extend the logic of a base class method, add an extra 'virtual' modifier to the method declaration, telling the compiler that this method may have additional logic added during inheritance.

The derived form must then add together an 'override' while declaring that same office, and then declare the method as you would normally.

If you want to reuse the base class functionality, you can explicitly telephone call its office by calling "base.NameOfTheOverridenMethod".

Abstract Members

Abstract members work similarly to interface members, y'all declare an abstract member and requite it no implementation, this way any derived course must have its ain implementation of said member.

Since the base class has unimplemented members, nosotros tin't instantiate them, only their derived classes, and so we must as well tag the class as abstract.

Coming up side by side!

Next week is our final and long-awaited chapter: Integrating into Unity. Now that we got the tools, it's time for some hands-on coding, allow'due south meet how it all ties together and create a cool game while we are at it!

Happy coding 🙂

🔙  Back to the Learn C# for Unity carte du jour

warkwatints.blogspot.com

Source: https://circuitstream.com/blog/learn-c-for-unity-lesson-6-inheritance-and-interfaces/