function createPerson(name,age,job){ var o=new Object(); o.name=name; o.age=age; o.job=job; o.sayName=function(){ alert(this.name) }; return o; } //可以无数次调用createPerson函数 var person1=createPerson("GReg",20,"sofwear engineer"); var perso
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cstdlib> using namespace std; struct node { int data; int left; int right; int key; int size; bool turn; int ans; int mark; node* ls,*rs;
<?php header("Content-type:text/html;charset=utf-8"); /** * 共同接口 */ interface db { function conn(); } /** * mysql类 */ class DbMysql implements db { public function conn() { echo "连接上了mysql<br>"; } } /** * sqlite类 */ class DbSq
抽象工厂,名字就告诉你是抽象的了.上代码. public interface BMW { public void Drive(); } public class BMW730 : BMW { public override void Drive() { Console.WriteLine("BMW 730 Run"); } } public class BMWX6 : BMW { public override void Drive() { Console.WriteLine(&quo
简单工厂,代码: public interface ISpeak { public void Say(); } public class Hello : ISpeak { public void Say() { //throw new NotImplementedException(); Console.WriteLine("Hello"); } } public class Bye : ISpeak { public void Say() { Console.WriteLine(&q
(用到了依赖倒置原则) 我们写的代码中,有的时候可能会出现根据外面给定的不同的参数在方法中根据参数实例化不同的实例,就是会根据不同的参数会new出不同的实例.如果这么写了,这段代码会非常的脆弱,一旦出现新的类型,那么就需要重写将这段代码找出来添加写的new.之前说过针对接口编程,而不是针对实现.这句话也可以理解成,代码中尽量不要出现具体的实例.能用到实例的地方,应该用超类来代替,通过多态让代码走正确的分支. 通过例子来说明(看的就是<Head First 设计模式>这本书,所有还是用书中的例子