今天上网查询资料偶然看到一个介绍多态性的文章,老外写的,很有趣但是介绍的很好。含有不少少儿不宜的成分,不过我想大家应该都成年了,所以发上来
Polymorphism shouldn't be a new concept to anybody. You deal with it every day in the real world. There's more than one class of cat to skin, but you skin 'em the same way, even if the specific instance is completely new to you. Let's say for example you want to fuck a hole. You fuck all holes the same. You don't care if that hole happens to be a mouth, an ass, or a pussy, you're gonna fuck it the same way regardless. However, the mouth, pussy, or ass may respond differently to the fucking.
So you have a common abstract class named 'Hole' and 3 concrete classes Pussy, Ass, and Mouth which all extend from Hole:
class Pussy extends Hole {}
class Mouth extends Hole {}
class Ass extends Hole {}
So, now let's say you have a Penis.Fuck(Hole h) method. The Penis class is unconcerned about what the specific Hole instance is, it's gonna fuck it the same regardless. Specificly we thrust the Hole with a Penis until the Penis is spent. Finally, we give the hole the Penis' load.
class Penis {
public Fuck(Hole h) {
while(!this.isSpent) {
h.TakeAThrust(this);
this.arousal++;
}
h.TakeALoad(this.load);
}
}
Now here's where polymorphism gets fun. The Hole will respond different to the thrusting and load depending on what specific type of Hole we're implementing.
First we must implement an abstract class which defines an abstract interface.
abstract class Hole {
public abstract void TakeAThrust(Penis p);
public abstract void TakeALoad(Load l);
}
Now all that's left is the varying implementations of these methods in the seperate concrete classes. For example, an Ass' implementation of TakeAThrust could look something like:
public void TakeAThrust(Penis p) {
if(!enoughLube && p.Circumference > 6) {
analFissureCount++;
}
}
See, the beauty of it is... the Penis doesn't even need to know it's fucking an Ass for the Ass to behave like a proper Ass.
Now, let's see how we might implement TakeALoad differently for Mouth and Pussy:
//in Pussy
public void TakeALoad(Load l) {
//randomly determine whether to cause a pregnancy with a 10% chance...
if(Math.RandomNumber() % 10 == 0) {
this.Woman.EggFactory.GetEgg().Inseminate(l);
}
}
//in Mouth
public void TakeALoad(Load l) {
//50-50 chance of spitting or swallowing
if(Math.RandomNumber() % 1 == 0) {
this.Spit(l);
} else {
this.Swallow(l);
}
}
Putting it all together with client code
Now that we have our classes well planned out with polymorphism in mind, we can see the kind of luxury it is for the client programmer to work with.
//create an array of 4 women
Woman[] women = new Woman[]{new Woman(), new Woman(), new Woman(), new Woman()};
//create a hole array to reference the holes of all 4 women, plus two additional holes.
Hole[] holes = new Hole[4*3 + 2];
for(int i = 0; i < women.Length; i++) {
holes[3 * i + 0] = women[i].Mouth;
holes[3 * i + 1] = women[i].Pussy;
holes[3 * i + 2] = women[i].Ass;
}
//additional holes (so the faggy programmers don't feel left out)
Man m = new Man();
holes[12] = m.Mouth;
holes[13] = m.Ass;
//now we loop through the holes and fuck them all with the same Penis
Penis p = new Man().Penis;
foreach(Hole h in holes) {
p.Fuck(h);
}
See how easy it makes it for the client programmer?