简体   繁体   中英

Difference in abstract class implementation in c# and php

I've been working with PHP for a while now I am trying to explore the world of c#.I was trying to understand the abstract class implementation in c sharp.I've found some fluctuation in behavior in both languages.That's why I am posting it here so that I can get a better insight of abstract class implementation in C#.

Both PHP and C# extends abstract class.That means all the class that extends from abstract class must implement the abstract methods as well as inherit non-abstract public methods from abstract class.

The problem arises while accessing the property of child class from a method of the abstract class. In PHP getHairColor() can access the public hair property. But in C# getHairColor() can't access the public hair property of the class that extends from abstract class.

I think as the Male class extends Person class, it also inherits the getHairColor() method from the parent class.So, it should be able to access the hair property. Here PHP implementation makes more sense than C#. Can I get a proper explanation for different behavior for a similar concept in two different languages?

PHP:

abstract class Person{
    abstract public function setHairColor($color);

    public function getHairColor(){
        return $this->hair;  // $this->hair is accessible from here
    }
}

class Male extends Person{
    public $hair;

    public function setHairColor($color){
        $this->hair = $color;
    }

}

$male1 = new Male();
$male1->setHairColor("black");
echo $male1->getHairColor(); // access hair property

C#:

abstract class Person
{
    public abstract void setHairColor(string hair);

    public string getHairColor()
    {
        return this.hair; // this.hair is not accessible from here
    }

}

class Male : Person
{
    public string hair;

    public override void setHairColor(string hair)
    {
        this.hair = hair;
    }
}

Male male1 = new Male();
male1.setHairColor("black");
Console.WriteLine(male1.getHairColor()); // can not access hair property 

C# has static typing. That's why you can't access a field or property that is declared in a subclass. You have to declare Hair as an abstract property in Person and implement it in the subclass:

    abstract class Person
    {
        public abstract void SetHairColor(string hair);

        public abstract string Hair { get; }

        public string getHairColor()
        {
            return this.Hair; // now it is accessible
        }

    }

    class Male : Person
    {
        private string _Hair;
        public override string Hair { get { return _Hair; } }

        public override void SetHairColor(string hair)
        {
            this._Hair = hair;
        }
    }

You can also implement SetHairColor as setter of the property Hair :

    abstract class Person
    {
        public abstract string Hair { get; set; }

        public string getHairColor()
        {
            return this.Hair; // now it is accessible
        }

    }

    class Male : Person
    {
        private string _Hair;
        public override string Hair 
        { 
            get { return _Hair; } 
            set { _Hair = value }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM