Last active
January 2, 2016 20:29
-
-
Save 3F/8357145 to your computer and use it in GitHub Desktop.
Java7, PHP 5.5, C++, C# .NET 4 :: Properties & Methods / LSP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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