samedi 30 décembre 2017

what does if(getClass().getSimpleName().equals("blahblah")) is actually doing

I Made a Class "Hamburger" and it Has 2 Other Sub classes that Extend from it, there's also a method in Hamburger Class Which is "setbaseprice", I want to Make this Method Unchangeable for one of Sub Classes which is Called Delux... so I made an if statement inside of that method like

public void setBaseprice(double price){
        if(getClass().getSimpleName().equals("Delux")){
            this.baseprice = 37000;
            System.out.println("Total Price for Delux Burger is $" + this.baseprice);

I wanted to tell the method, don't take anything other than 37000 from Delux.. or change what ever it gives you to 37000.. but i just noticed, this thing is working even if we don't call the method. baseprice is set and system.out.prinln works right after we create an object in delux class.. no matter we write object.setBaseprice() or not. Can you please tell me what is happening in there ?

-I'm a Beginner

Aucun commentaire:

Enregistrer un commentaire