
/**
* 版权所有 2022 涂聚文有限公司
* 许可信息查看:
* 描述:
* 不变模式 Immutable Patterns
* 历史版本: JDK 14.02
* 2022-09-12 创建者 geovindu
* 2022-09-12 添加 Lambda
* 2022-09-12 修改:date
* 接口类
* 2022-09-12 修改者:Geovin Du
* 生成API帮助文档的指令:
*javadoc - -encoding Utf-8 -d apidoc Pig.java
*
* */
package com.javapatterns.immutable;
/**
*
* @author geovindu
*
* */
public class Pig {
/**
*
*
* */
public Pig()
{
System.out.println("I am a pig.");
}
/**
*
*
* */
public Pig(String greeting)
{
System.out.println("Hello from a pig");
}
/**
*
*
* */
public static void speak()
{
System.out.println("!@#$%^&!");
}
/**
*
*
* */
public void walk()
{
System.out.println("I walk a pig's walk");
}
}/**
* 版权所有 2022 涂聚文有限公司
* 许可信息查看:
* 描述:
* 不变模式 Immutable Patterns
* 历史版本: JDK 14.02
* 2022-09-12 创建者 geovindu
* 2022-09-12 添加 Lambda
* 2022-09-12 修改:date
* 接口类
* 2022-09-12 修改者:Geovin Du
* 生成API帮助文档的指令:
*javadoc - -encoding Utf-8 -d apidoc BaPig.java
*
* */
package com.javapatterns.immutable;
/**
*
* @author geovindu
*
* */
public class BaPig extends Pig {
/**
*
*
* */
public BaPig()
{
System.out.println("I am Ba Pig.");
}
/**
*
*
* */
public BaPig(String greeting)
{
System.out.println("Hello from Ba Pig");
}
/**
*
*
* */
public static void speak()
{
System.out.println("I am Ba Pig, a beast that talks");
}
/**
*
*
* */
public void walk()
{
System.out.println("I walk a man's walk");
}
}调用测试:、
//不变模式
System.out.println("==============Pig starting==============");
Pig du=new BaPig();
du.speak();
du.walk();
System.out.println("==============Pig finishing==============");
System.out.println("==============starting==============");
du = new BaPig("Hello");
du.speak();
du.walk();
System.out.println("==============finishing==============");输出:
==============Pig starting==============
I am a pig.
I am Ba Pig.
!@#$%^&!
I walk a man's walk
==============Pig finishing==============
==============starting==============
I am a pig.
Hello from Ba Pig
!@#$%^&!
I walk a man's walk
==============finishing==============