Skip to content

Instantly share code, notes, and snippets.

@3F
Last active January 2, 2016 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3F/8357145 to your computer and use it in GitHub Desktop.
Save 3F/8357145 to your computer and use it in GitHub Desktop.
Java7, PHP 5.5, C++, C# .NET 4 :: Properties & Methods / LSP
// Java
/* *** test1 *** */
class A
{
public String p = "from A";
public String m()
{
return this.p;
}
}
class B extends A
{
public String p = "from B";
}
...
B b = new B();
System.out.println(b.m()); // from A
/* *** test2 *** */
class A
{
public String p(){ return "from A"; }
public String m()
{
return this.p();
}
}
class B extends A
{
public String p(){ return "from B"; }
}
...
B b = new B();
System.out.println(b.m()); // from B
// PHP
/* *** test1 *** */
class A
{
public $p = "from A";
public function m()
{
return $this->p;
}
}
class B extends A
{
public $p = "from B";
}
$b = new B();
echo $b->m(); // from B
// >= v5.3 also available 'LSB' (for static bind), - e.g.: static::
// and self::
/* *** test2 *** */
class A
{
public function p(){ return "from A"; }
public function m()
{
return $this->p();
}
}
class B extends A
{
public function p(){ return "from B"; }
}
$b = new B();
echo $b->m(); // from B
// C++
/* *** test1 *** */
class A
{
public:
string p;
A(): p("from A"){};
string m()
{
return this->p; // p & (*this).p, etc.,
};
};
class B : public A
{
public:
string p;
B(): p("from B"){};
};
...
B b;
TRACE(b.m()); //from A
/* *** test2 *** */
class A
{
public:
virtual string p(){ return "from A"; };
string m()
{
return this->p();
};
};
class B : public A
{
public:
string p(){ return "from B"; };
};
...
B b;
TRACE(b.m()); //from B
// C#
/* *** test1 *** */
class A
{
public String p = "from A";
public String m()
{
return this.p;
}
};
class B : A
{
public String p = "from B";
};
...
B b = new B();
Console.WriteLine(b.m()); // from A
/* *** test2 *** */
class A
{
public virtual String p(){ return "from A"; }
public String m()
{
return this.p();
}
};
class B : A
{
public override String p() { return "from B"; }
};
...
B b = new B();
Console.WriteLine(b.m()); // from B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment