0

Can someone gives examples on how to initialize abstract, interface and static classes?

25th May 2017, 6:24 AM
DoubleVermine
DoubleVermine - avatar
4 odpowiedzi
+ 3
static classes cannot be inherited, nor can they inherit other classes.. True, an abstract class cannot be instantiated, but instead forms the base of other classes. The benefit is that you can put functionality into the abstract class to aid reuse. A  static class is one that is instantiated by the CLR when required.
25th May 2017, 11:40 AM
Ang
Ang - avatar
+ 1
Here's an example from something I've actually done in a Unity project I've been working on: I have a bunch of different classes to hold the stats of different types of things: enemyStats, playerStats, weaponStats, and soldierStats. However, all of these stats have some things in common. So, I decided to make an interface 'Stats'. This will contain all the similarities that these classes have. Now, what's the benefit? Well, whenever I need to start a fight that will use the stats class, I now only need to use the interface. As all non-similarities would be used by the sub-classes anyway. So the interface lets me implement all those similarities into one instance (Stats). Ex/ object.getComponent<Stats>().dmg; This will grab the dmg stat of the object. Doesn't even matter if it's weaponStats or playerStats. If I didn't do that I would have had to do this, based on how my scripts work: if(object.getType() == typeOf(EnemyStats))... etc object.getComponent<enemyStats>() else if(... ...etc for other stats. I turned a potential 10 lines of ugly code into 1 line. For abstract classes, I recommend you look at my Pirate Wars OOP code for a practical example https://code.sololearn.com/cZKcrU0TrMtX/?ref=app Static class: I want to be able to pause the game with a press of a button. But where I want to pause the game can be from many different scripts, and I don't really want to have to create an instance of some Pause script just to be able to pause and unpause the game for these things. I also don't want to inherit a Pause script, as there not really related to eachother. So, I decided to create a static class calls Pause. In this class I have the static methods: pause(), unPause() These will litteraly pause and unpause the game. Now, when a shop, inventory, or the game is directly paused I don't even need an instance of the Pause class. I just call it directly: Pause.pause(); Player has just exitted the shop! Pause.unpause(); Hopefully those examples helped in some way, even if they aren't very good 😜.
25th May 2017, 5:03 PM
Rrestoring faith
Rrestoring faith - avatar
0
Thanks for providing the examples
25th May 2017, 6:10 PM
DoubleVermine
DoubleVermine - avatar
0
I have a good idea for a game let me know if you are interested in making it
25th May 2017, 6:11 PM
DoubleVermine
DoubleVermine - avatar