Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The ' this ' keyword in abstract classes

I am slightly confused towards why I am able to use the 'this' in abstract classes.

I am making a very very simple object oriented role playing game. I have a base/super class called Items. I then have two types of items, equiptable and non-equiptable.

interface IEquiptable
{
    void equiptItem(Player p);
    void unequiptItem(Player p);
}

Theres the interface for equiptable items.

Next, I have an abstract class called Weapons:

abstract class Weapons : Items, IEquiptable 
{
    public double powerOfWeapon { get; set; }
    public double powerNeededToUse { get; set; }


    public void equiptItem(Player p)
    {
        Console.WriteLine(this); 
        p.weapon = this;
    }

    public void unequiptItem(Player p)
    {
        //UNTESTED METHOD
        p.weapon = new Swords("Fists", 1, 1);
    }

}

This extends the base class of Items (this class only contains a name for the item at the moment, so not worth show) and implements the interface 'IEquiptable'.

Finally I have a class called Swords

class Swords : Weapons
{

    public Swords(string name, double powerOfS, double powerToU)
    {
        base.name = name;
        powerOfWeapon = powerOfS;
        powerNeededToUse = powerToU;
    }

}

This extends Weapons and provides a constructor for the sword.

Here is some of my Player class

class Player
{
    public string Name { get; set; }
    public double life { get; set; }
    public double power { get; set; }
    public Weapons weapon { get; set; } // Currently held weapon

    private List<Items> items; //This is being used to represent a 'backpack' any item obtained will be added here.

To eqipt a weapon I am using this method: (in my player class)

public void equiptWeapon()
    {
        Weapons tempWep = items.OfType<Weapons>().FirstOrDefault();
        if (tempWep != null)
        {
            tempWep.equiptItem(this);        
        } 
    }

I don't understand why this works. tempWep is being given the value of the first Weapon in the list. But then it is calling the method equiptItem() which is being implemented in by abstract class and then 'this' is being assigned, which I cannot follow how it doesn't throw an error for not being to initialize Weapons

Hopefully you can understand what I mean.

Thanks.

like image 618
William Avatar asked May 26 '13 03:05

William


People also ask

Which keywords define abstract class?

To create an abstract class, just use the abstract keyword before the class keyword, in the class declaration. You can observe that except abstract methods the Employee class is the same as the normal class in Java.

What is a keyword in abstract?

Keywords are important terms that can be found in your abstract and chapters, but they also stand alone as search terms. Abstracts and keywords together help researchers find content.

What is abstract class A class with abstract keyword?

The abstract keyword is a non-access modifier, used for classes and methods: Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class). Abstract method: can only be used in an abstract class, and it does not have a body.

What is an abstract class Mcq?

Explanation: The condition for a class to be called abstract class is that it must have at least one pure virtual function. The keyword abstract must be used while defining abstract class in java. 2.


1 Answers

Language constraints mean you cannot create an abstract class directly, however it is totally possible to have an instance of it - you simply create an instance of a derived class and then cast it down to the abstract class. An abstract class can also have implementation in it, so the usage of the keyword this is perfectly normal and acceptable.

like image 146
slugster Avatar answered Oct 26 '22 05:10

slugster