package com.lagou.task21;public abstract class Person {private String name;//private int age;public Person() {}public Person(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}public abstract void show();
}
package com.lagou.task21;public class SubPerson extends Person {private int age;@Overridepublic void show() {}
}
package com.lagou.task09;/****/
public class ShapeTest {//自定义成员方法实现将参数指定矩形对象特征打印出来的行为//也就是绘制图形的行为//Rect r = new Rect(1,2,3,4);//public static void draw(Rect r){// r.show(); // 1 2 3 4//}//自定义成员方法实现将参数指定圆形对象特征打印出来的行为//public static void draw(Circle c){ //重载// c.show(); // 1 2 3//}//自定义成员方法实现既能打印矩形对象又能打印圆形对象的特征//对象由参数传入,子类is a 父类//Shape r = new Rect(1,2,3,4);//Shape r = new Circle(1,2,3);//多态的使用场合之一,通过参数传递形成了多态public static void draw(Shape s){ //重载// 父类类型的引用指向子类类型的对象,这就是多态//编译阶段调用父类的版本,运行阶段调用子类重写后的版本s.show(); // 1 2 3 4}public static void main(String[] args) {//Rect r = new Rect(1,2,3,4);ShapeTest.draw(new Rect(1,2,3,4));ShapeTest.draw(new Circle(1,2,3));}
}
package com.lagou.task21;public interface Animal {void run(); // 用于描述奔跑行为的抽象方法void fly(); // 用于描述飞行行为的抽象方法
}
package com.lagou.task21;public interface RunAnimal {void run(); // 用于描述奔跑行为的抽象方法
}
package com.lagou.task21;public interface FlyAnimal {void fly(); // 用于描述飞行行为的抽象方法
}
package com.lagou.task21;public class Dog implements RunAnimal {@Overridepublic void run() {}
}
package com.lagou.task21;public class A {public void show() {System.out.println("这是A类中的show方法!");}
}
package com.lagou.task21;public class B/* extends A*/ {private A a; // 合成复用原则public B(A a) {this.a = a;}public void test() {// 调用A类中的show方法,请问如何实现?a.show();}
}
package com.lagou.task21;public class Singleton {// 2.声明本类类型的引用指向本类类型的对象并使用private static关键字修饰private static Singleton sin = null;// 1.私有化构造方法,使用private关键字修饰private Singleton() {}// 3.提供公有的get方法负责将上述对象返回出去,使用public static关键字修饰public static /*synchronized*/ Singleton getInstance() {/*synchronized (Singleton.class) {if (null == sin) {sin = new Singleton();}return sin;}*/if (null == sin) {synchronized (Singleton.class) {if (null == sin) {sin = new Singleton();}}}return sin;}
}
package com.lagou.task21;public interface Sender {// 自定义抽象方法来描述发送的行为void send();
}
package com.lagou.task21;public class MailSender implements Sender {@Overridepublic void send() {System.out.println("正在发送邮件...");}
}
package com.lagou.task21;public class SmsSender implements Sender {@Overridepublic void send() {System.out.println("正在发送短信...");}
}
package com.lagou.task21;public class SendFactory {// 自定义成员方法实现对象的创建public Sender produce(String type) {//System.out.println("随便加一句打印进行测试");if ("mail".equals(type)) {return new MailSender();}if ("sms".equals(type)) {return new SmsSender();}return null;}//多个工厂public static Sender produceMail() {return new MailSender();}public static Sender produceSms() {return new SmsSender();}
}
package com.lagou.task21;public class SendFactoryTest {public static void main(String[] args) {// 缺点:代码复杂,可读性略差// 优点:扩展性和可维护性更强! 尤其是在创建大量对象的前提下//如:需要再创建对象的前面的打印一句话,那么这里可以直接在方法里写一句话//就可以应对大量的创建对象操作,否则的话,会写很多的打印的代码// 1.声明工厂类类型的引用指向工厂类类型的对象//SendFactory sf = new SendFactory();// 2.调用生产方法来实现对象的创建//Sender sender = sf.produce("mail");//Sender sender = sf.produce("maill");//Sender sender = sf.produceMail();Sender sender = SendFactory.produceMail();// 3.使用对象调用方法模拟发生的行为sender.send();System.out.println("-------------------------------------");// 优点:代码简单,可读性强 在创建单个对象时有优势// 缺点:扩展性和可维护性略差Sender sender1 = new MailSender();sender1.send();System.out.println("-------------------------------------");Provider provider = new MailSendFactory();Sender sender2 = provider.produce();sender2.send();System.out.println("-------------------------------------");Provider provider1 = new PacketSendFactory();Sender sender3 = provider1.produce();sender3.send();}
}
package com.lagou.task21;public interface Provider {// 自定义抽象方法描述产品的生产行为Sender produce();
}
package com.lagou.task21;public class MailSendFactory implements Provider {@Overridepublic Sender produce() {return new MailSender();}
}
package com.lagou.task21;public class SmsSendFactory implements Provider {@Overridepublic Sender produce() {return new SmsSender();}
}
package com.lagou.task21;public class PacketSender implements Sender {@Overridepublic void send() {System.out.println("正在发送包裹...");}
}
package com.lagou.task21;public class PacketSendFactory implements Provider {@Overridepublic Sender produce() {return new PacketSender();}
}
package com.lagou.task21;public interface Sourceable {// 自定义抽象方法void method();
}
package com.lagou.task21;public class Source implements Sourceable {@Overridepublic void method() {System.out.println("素颜美可以如此之美!");}
}
package com.lagou.task21;public class Decorator implements Sourceable {private Sourceable source;public Decorator(Sourceable source) {this.source = source;}@Overridepublic void method() {source.method(); // 保证原有功能不变System.out.println("化妆之后你会更美!");}
}
package com.lagou.task21;public class SourceableTest {public static void main(String[] args) {Sourceable sourceable = new Source();sourceable.method();System.out.println("---------------------------------------------------");// 接下来使用装饰类实现功能Sourceable sourceable1 = new Decorator(sourceable);sourceable1.method();System.out.println("---------------------------------------------------");Sourceable sourceable2 = new Proxy();sourceable2.method();}
}
package com.lagou.task21;public class Proxy implements Sourceable {private Source source;public Proxy() {source = new Source();}@Overridepublic void method() {source.method();System.out.println("我和装饰器模式其实是不一样的!");}
}
package com.lagou.task21;public abstract class AbstractCalculator {// 自定义成员方法实现将参数指定的表达式按照参数指定的规则进行切割并返回计算结果 1+1 +public int splitExpression(String exp, String op) {String[] sArr = exp.split(op);return calculate(Integer.parseInt(sArr[0]), Integer.parseInt(sArr[1]));}// 自定义抽象方法实现运算public abstract int calculate(int ia, int ib);
}
package com.lagou.task21;public class Plus extends AbstractCalculator {@Overridepublic int calculate(int ia, int ib) {return ia + ib;}
}
package com.lagou.task21;public class Minus extends AbstractCalculator {@Overridepublic int calculate(int ia, int ib) {return ia - ib;}
}
package com.lagou.task21;public class AbstractCalculatorTest {public static void main(String[] args) {AbstractCalculator abstractCalculator = new Plus();int res = abstractCalculator.splitExpression("1+1", "\\+");System.out.println("最终的运算结果是:" + res); // 2}
}public int calculate(int ia, int ib) {return ia + ib;}
}
package com.lagou.task21;public class Minus extends AbstractCalculator {@Overridepublic int calculate(int ia, int ib) {return ia - ib;}
}
package com.lagou.task21;public class AbstractCalculatorTest {public static void main(String[] args) {AbstractCalculator abstractCalculator = new Plus();int res = abstractCalculator.splitExpression("1+1", "\\+");System.out.println("最终的运算结果是:" + res); // 2}
}