/**
* 饿汉式(静态常量)
*/
public class Singleton01 {
public static void main(String[] args) {
//测试
Singleton instance = Singleton.getInstance();
}
}
class Singleton{
//1 构造器私有化
private Singleton(){
}
//2 本类内部创建对象实例
private final static Singleton instance = new Singleton();
//3 提供一个公有的静态方法,返回实例对象
public static Singleton getInstance(){
return instance;
}
}
/**
* 饿汉式(静态代码块)
*/
public class Singleton02 {
public static void main(String[] args) {
//测试
Singleton instance = Singleton.getInstance();
Singleton instance1 = Singleton.getInstance();
System.out.println(instance.hashCode() == instance1.hashCode());
}
}
class Singleton{
//1 构造器私有化
private Singleton(){
}
//2 本类内部创建对象实例
private static Singleton instance;
//在静态代码块种创建对象实例
static {
instance = new Singleton();
}
//3 提供一个公有的静态方法,返回实例对象
public static Singleton getInstance(){
return instance;
}
}
/**
* 懒汉式(线程不安全)
*/
public class Singleton03 {
public static void main(String[] args) {
//测试
Singleton instance = Singleton.getInstance();
Singleton instance1 = Singleton.getInstance();
System.out.println(instance.hashCode() == instance1.hashCode());
}
}
class Singleton{
private static Singleton instance;
private Singleton(){
}
//提供静态的公用方法,当使用该方法时,才创建实例
public static Singleton getInstance(){
if (instance == null){
instance = new Singleton();
}
return instance;
}
}
/**
* 懒汉式(线程安全,同步方法)
*/
class Singleton{
private static Singleton instance;
private Singleton(){
}
//使用synchronized解决线程安全问题
public static synchronized Singleton getInstance(){
if (instance == null){
instance = new Singleton();
}
return instance;
}
}
/**
* 双重检查
*/
class Singleton{
private static volatile Singleton instance;
private Singleton(){
}
//加入双重检查代码,解决线程安全问题,通知解决懒加载问题
public static Singleton getInstance(){
if (instance == null){
synchronized (Singleton.class){
if (instance == null){
instance = new Singleton();
}
}
}
return instance;
}
}
/**
* 静态内部类
*/
class Singleton{
private static volatile Singleton instance;
private Singleton(){
}
//静态内部类
//Singleton类装载的时候内部类不会装载
//类装载是线程安全的
private static class SingletonInstance{
private static final Singleton instance = new Singleton();
}
public static Singleton getInstance(){
return SingletonInstance.instance;
}
}
public class Singleton07 {
public static void main(String[] args) {
//测试
Singleton instance = Singleton.INSTANCE;
Singleton instance1 = Singleton.INSTANCE;
System.out.println(instance.hashCode() == instance1.hashCode());
}
}
/**
* 枚举
*/
enum Singleton{
INSTANCE;
}

//将Pizza类做成抽象类
public abstract class Pizza {
//披萨名字
protected String name;
//准备工作,不同的披萨材料不一样,做成抽象方法
public abstract void prepare();
//烘烤
public void bake() {
System.out.println(name + " baking;");
}
//切割
public void cut() {
System.out.println(name + " cutting;");
}
//打包
public void box() {
System.out.println(name + " boxing;");
}
public void setName(String name) {
this.name = name;
}
}
//奶酪披萨
public class CheesePizza extends Pizza{
public void prepare() {
System.out.println("给制作奶酪披萨准备原材料");
}
}
//希腊披萨
public class GreekPizza extends Pizza{
public void prepare() {
System.out.println("给制作希腊披萨准备原材料");
}
}
class PizzaStore {
public static void main(String[] args) {
getPizza("cheese");
}
public static Pizza getPizza(String type){
Pizza pizza = null;
if (type.equals("greek")) {
pizza = new GreekPizza();
pizza.setName("希腊披萨");
}else if (type.equals("cheese")){
pizza = new CheesePizza();
pizza.setName("奶酪披萨");
}
//输出披萨制作过程
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
}
//简单工厂
public class SimpleFactory {
//根据orderType返回对应的Pizza对象
public Pizza createPizza(String orderType){
Pizza pizza = null;
if (orderType.equals("greek")) {
pizza = new GreekPizza();
pizza.setName("希腊披萨");
}else if (orderType.equals("cheese")){
pizza = new CheesePizza();
pizza.setName("奶酪披萨");
}
return pizza;
}
}
class PizzaStore {
public static void main(String[] args) {
getPizza("cheese");
}
public static Pizza getPizza(String type){
//使用简单工厂模式创建披萨
SimpleFactory simpleFactory = new SimpleFactory();
Pizza pizza = simpleFactory.createPizza(type);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.bake();
return pizza;
}
}

//将Pizza类做成抽象类
public abstract class Pizza {
//披萨名字
protected String name;
//准备工作,不同的披萨材料不一样,做成抽象方法
public abstract void prepare();
//烘烤
public void bake() {
System.out.println(name + " baking;");
}
//切割
public void cut() {
System.out.println(name + " cutting;");
}
//打包
public void box() {
System.out.println(name + " boxing;");
}
public void setName(String name) {
this.name = name;
}
}
//北京奶酪披萨
public class BJCheesePizza extends Pizza {
public void prepare() {
System.out.println("给制作北京奶酪披萨准备原材料");
}
}
//北京胡椒披萨
public class BJPepperPizza extends Pizza {
public void prepare() {
System.out.println("给制作北京胡椒披萨准备原材料");
}
}
//伦敦奶酪披萨
public class LDCheesePizza extends Pizza {
public void prepare() {
System.out.println("给制作伦敦奶酪披萨准备原材料");
}
}
//伦敦胡椒披萨
public class LDPepperPizza extends Pizza {
public void prepare() {
System.out.println("给制作伦敦胡椒披萨准备原材料");
}
}

//方法工厂模式
public abstract class OrderPizza {
//根据orderType返回对应的Pizza对象
abstract Pizza createPizza(String type);
}
//北京订购披萨工厂
public class BJOrderPizza extends OrderPizza{
Pizza createPizza(String type) {
Pizza pizza = null;
if (type.equals("cheese")){
pizza = new BJCheesePizza();
pizza.setName("北京奶酪披萨");
}else if(type.equals("pepper")){
pizza = new BJPepperPizza();
pizza.setName("北京胡椒披萨");
}
return pizza;
}
}
//伦敦订购披萨工厂
public class LDOrderPizza extends OrderPizza{
Pizza createPizza(String type) {
Pizza pizza = null;
if (type.equals("cheese")){
pizza = new LDCheesePizza();
pizza.setName("伦敦奶酪披萨");
}else if(type.equals("pepper")){
pizza = new LDPepperPizza();
pizza.setName("伦敦胡椒披萨");
}
return pizza;
}
}
class PizzaStore {
public static void main(String[] args) {
Pizza pizza = new BJOrderPizza().createPizza("cheese");
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}
}


原型模式的克隆分为浅克隆和深克隆
Java中的Object类提供了clone()方法来实现浅克隆。cloneable接口是上面的类图中的抽象类原型,而实现了Cloneable接口的子实现类就是具体的原型类
Realizetype.java
public class Realizetype implements Cloneable{
//重写clone方法
@Override
protected Realizetype clone() throws CloneNotSupportedException {
System.out.println("具体原型复制成功");
return (Realizetype)super.clone();
}
}
public class Client {
public static void main(String[] args) throws CloneNotSupportedException {
//创建一个原型类
Realizetype realizetype = new Realizetype();
//调用原型类中的clone方法进行对象的克隆
Realizetype clone = realizetype.clone();
//原型对象和克隆出来的对象不是同一个对象
System.out.println(realizetype == clone); // false
}
}

//奖状(浅克隆)
public class Citation implements Cloneable{
private String name;
@Override
protected Citation clone() throws CloneNotSupportedException {
return (Citation)super.clone();
}
public void setName(String name) {
this.name = name;
}
}
public class Client {
public static void main(String[] args) throws CloneNotSupportedException {
//创建原型对象
Citation citation = new Citation();
//克隆奖状
Citation student1 = citation.clone();
//给奖状赋学生姓名
student1.setName("学生001");
Citation student2 = citation.clone();
student2.setName("学生002");
}
}
public class Student implements Serializable {
private String name;
public Student(String name){
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
//奖状,深克隆
public class Citation implements Cloneable{
private Student student;
@Override
protected Citation clone() throws CloneNotSupportedException {
return (Citation)super.clone();
}
public void setStudent(Student student) {
this.student = student;
}
public Student getStudent() {
return student;
}
}
public class Client {
public static void main(String[] args) throws CloneNotSupportedException {
//创建原型对象
Citation citation = new Citation();
Student student = new Student("张三");
citation.setStudent(student);
//克隆奖状
Citation citation1 = citation.clone();
citation1.getStudent().setName("李四");
//判断citation对象和citation对象是否是同一对象
System.out.println(citation.getStudent().getName()); //李四
System.out.println(citation1.getStudent().getName()); //李四
}
}
public class Client {
public static void main(String[] args) throws Exception {
//创建原型对象
Citation citation = new Citation();
Student student = new Student("张三");
citation.setStudent(student);
//克隆奖状
//1.将原型对象写入文件中
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\temp\\a.txt"));
oos.writeObject(citation);
oos.close();
//2.从文件中读取,再修改student的姓名
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\temp\\a.txt"));
Citation citation1 = (Citation)ois.readObject();
citation1.getStudent().setName("李四");
ois.close();
System.out.println(citation.getStudent().getName()); //张三
System.out.println(citation1.getStudent().getName()); //李四
}
}


//自行车(产品类)
public class Bike {
private String frame;
private String seat;
public String getFrame() {
return frame;
}
public void setFrame(String frame) {
this.frame = frame;
}
public String getSeat() {
return seat;
}
public void setSeat(String seat) {
this.seat = seat;
}
}
//抽象Builder类
public abstract class Builder {
protected Bike mBike = new Bike();
public abstract void buildFrame();
public abstract void buildSeat();
public abstract Bike createBike();
}
//摩拜单车
public class MobikeBuilder extends Builder{
public void buildFrame() {
mBike.setFrame("碳纤维车架");
}
public void buildSeat() {
mBike.setSeat("橡胶车座");
}
public Bike createBike() {
return mBike;
}
}
//Ofo单车
public class OfoBikeBuilder extends Builder{
public void buildFrame() {
mBike.setFrame("铝合金车架");
}
public void buildSeat() {
mBike.setSeat("真皮车座");
}
public Bike createBike() {
return mBike;
}
}
//指挥者类
public class Directory {
private Builder mBuilder;
public Directory(Builder builder){
mBuilder = builder;
}
public Bike construct(){
mBuilder.buildFrame();
mBuilder.buildSeat();
return mBuilder.createBike();
}
}
public class Client {
public static void main(String[] args) {
Directory directory = new Directory(new OfoBikeBuilder());
Bike bike = directory.construct();
System.out.println(bike.getFrame() + " " + bike.getSeat());
Directory directory1 = new Directory(new MobikeBuilder());
Bike bike1 = directory1.construct();
System.out.println(bike1.getFrame() + " " + bike1.getSeat());
}
}

//卖票接口
public interface SellTickets {
void sell();
}
//火车站 火车站具有卖票功能,实现SellTickets接口
public class TrainStation implements SellTickets{
public void sell() {
System.out.println("火车站卖票");
}
}
//代售点
public class ProxyPoint implements SellTickets{
private TrainStation trainStation = new TrainStation();
public void sell() {
System.out.println("代理点收取一些服务费");
trainStation.sell();
}
}
//测试类
public class Client {
public static void main(String[] args) {
ProxyPoint proxyPoint = new ProxyPoint();
proxyPoint.sell();
//输出...
//代理点收取一些服务费
//火车站卖票
}
}
public class ProxyFactory {
TrainStation station = new TrainStation();
public SellTickets getProxyObject(){
/**
* 使用Proxy获取代理对象
* newProxyInstance()方法参数说明
* 参数1:ClassLoader 类加载器,用于加载动态类,使用真实对象的类加载器即可
* 参数2:Class<?>[] interfaces 真实对象所实现的接口,代理模式真实对象和代理对象实现相同的接口
* 参数3:InvocationHandler 代理对象的调用处理程序
*/
SellTickets sellTickets = (SellTickets) Proxy.newProxyInstance(station.getClass().getClassLoader(),
station.getClass().getInterfaces(),
new InvocationHandler() {
/**
*
* @param proxy 代理对象
* @param method 对应于在代理对象上调用的接口方法的Method
* @param args 代理对象调用接口方法时传递的实际参数
* @return
* @throws Throwable
*/
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("代售点收取一部分服务费");
//执行真实对象
return method.invoke(station);
}
});
return sellTickets;
}
}
public class Client {
public static void main(String[] args) {
ProxyFactory proxyFactory = new ProxyFactory();
SellTickets proxyObject = proxyFactory.getProxyObject();
proxyObject.sell();
//代售点收取一部分服务费
//火车站卖票
}
}
静态代理和动态代理对比
优缺点
定义
结构

public interface SDCard {
String readSD();
void writeSD();
}
public class SDCardImpl implements SDCard{
public String readSD() {
return "sd card read";
}
public void writeSD() {
System.out.println("sd card write");
}
}
public interface TFCard {
String readTF();
void writeTF();
}
public class TFCardImpl implements TFCard{
public String readTF() {
return "tf card read";
}
public void writeTF() {
System.out.println("tf cart write");
}
}
public class Computer {
public String readSD(SDCard sdCard){
return sdCard.readSD();
}
}
public class TFAdapter extends TFCardImpl implements SDCard {
public String readSD() {
return readTF();
}
public void writeSD() {
writeTF();
}
}
public class Client {
public static void main(String[] args) {
Computer computer = new Computer();
String msg = computer.readSD(new TFAdapter());
System.out.println(msg); //tf card read
}
}
public class TFAdapterSD implements SDCard{
private TFCard tfCard;
public TFAdapterSD(TFCard tfCard){
this.tfCard = tfCard;
}
public String readSD() {
return tfCard.readTF();
}
public void writeSD() {
tfCard.writeTF();
}
}
public class Client {
public static void main(String[] args) {
Computer computer = new Computer();
String s = computer.readSD(new TFAdapterSD(new TFCardImpl()));
System.out.println(s);
}
}

//快餐抽象类(抽象构建)
public abstract class FastFood {
//价格
private float price;
//描述
private String desc;
public FastFood(float price,String desc){
this.price = price;
this.desc = desc;
}
//获取价格
public abstract float cost();
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
//炒饭(具体构建)
public class FiredRice extends FastFood {
public FiredRice() {
super(10, "炒饭");
}
public float cost() {
return getPrice();
}
}
//炒面(具体构建)
public class FiredNoodles extends FastFood{
public FiredNoodles(float price, String desc) {
super(12, "炒面");
}
public float cost() {
return getPrice();
}
}
//配料类(抽象装饰)
public abstract class Garnish extends FastFood{
private FastFood fastFood;
public Garnish(FastFood fastFood,float price, String desc) {
super(price, desc);
this.fastFood = fastFood;
}
public abstract float cost();
public FastFood getFastFood() {
return fastFood;
}
}
//鸡蛋配料(具体装饰)
public class Egg extends Garnish{
public Egg(FastFood fastFood) {
super(fastFood, 1, "鸡蛋");
}
public float cost() {
return getFastFood().cost() + 1;
}
@Override
public String getDesc() {
return super.getDesc() + getFastFood().getDesc();
}
}
//培根配料(具体装饰)
public class Bacon extends Garnish {
public Bacon(FastFood fastFood) {
super(fastFood, 1, "培根");
}
public float cost() {
return getFastFood().cost() + 1;
}
@Override
public String getDesc() {
return "培根" + getFastFood().getDesc();
}
}
public class Client {
public static void main(String[] args) {
//点一份炒饭
FastFood food = new FiredRice();
//花费的价格
System.out.println(food.getDesc() + " " + food.cost() + "元"); //鸡蛋炒饭 11.0元
System.out.println("====================");
//点一份鸡蛋炒饭
FastFood food1 = new Egg(food);
//花费的价格
System.out.println(food1.getDesc() + " " + food1.cost() + "元"); //鸡蛋炒饭 11.0元
System.out.println("====================");
//点一根培根
FastFood food2 = new Bacon(food1);
//花费的价格
System.out.println(food2.getDesc() + " " + food2.cost() + "元"); //培根鸡蛋炒饭 12.0元
}
}


//视频文件
public interface VideoFile {
void decode(String fileName);
}
//avi文件
public class AVIFile implements VideoFile{
@Override
public void decode(String fileName) {
System.out.println("avi视频文件:"+fileName);
}
}
//rmvb文件
public class RMVBFile implements VideoFile {
@Override
public void decode(String fileName) {
System.out.println("rmvb视频文件:"+fileName);
}
}
//操作系统
public abstract class OperatingSystem {
protected VideoFile videoFile;
public OperatingSystem(VideoFile videoFile){
this.videoFile = videoFile;
}
public abstract void play(String fileName);
}
//Windows版本
public class Windows extends OperatingSystem{
public Windows(VideoFile videoFile) {
super(videoFile);
}
@Override
public void play(String fileName) {
videoFile.decode(fileName);
}
}
//Mac版本
public class Mac extends OperatingSystem{
public Mac(VideoFile videoFile) {
super(videoFile);
}
@Override
public void play(String fileName) {
videoFile.decode(fileName);
}
}
//测试类
public class Client {
public static void main(String[] args) {
OperatingSystem os = new Windows(new AVIFile());
os.play("名侦探柯南"); //avi视频文件:名侦探柯南
}
}


//灯类
public class Light {
public void on(){
System.out.println("打开了灯");
}
public void off(){
System.out.println("关闭了灯");
}
}
//电视类
public class TV {
public void on(){
System.out.println("打开了电视");
}
public void off(){
System.out.println("关闭了电视");
}
}
//空调类
public class AirCondition {
public void on(){
System.out.println("打开了空调");
}
public void off(){
System.out.println("关闭了空调");
}
}
//外观对象
public class SmartAppliancesFacade {
private Light light;
private TV tv;
private AirCondition airCondition;
public SmartAppliancesFacade(){
light = new Light();
tv = new TV();
airCondition = new AirCondition();
}
public void say(String message){
if (message.contains("打开")){
on();
}else if (message.contains("关闭")){
off();
}
}
private void on(){
light.on();
tv.on();
airCondition.on();
}
private void off(){
light.off();
tv.off();
airCondition.off();
}
}
//测试
public class Client {
public static void main(String[] args) {
//创建外观对象
SmartAppliancesFacade facade = new SmartAppliancesFacade();
facade.say("打开");
facade.say("关闭");
}
}
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数
给定一个复杂的对象层次结构,幸运的是它不包含循环引用,我如何实现支持各种格式的序列化?我不是来讨论实际实现的。相反,我正在寻找可能会派上用场的设计模式提示。更准确地说:我正在使用Ruby,我想解析XML和JSON数据以构建复杂的对象层次结构。此外,应该可以将该层次结构序列化为JSON、XML和可能的HTML。我可以为此使用Builder模式吗?在任何提到的情况下,我都有某种结构化数据-无论是在内存中还是文本中-我想用它来构建其他东西。我认为将序列化逻辑与实际业务逻辑分开会很好,这样我以后就可以轻松支持多种XML格式。 最佳答案 我最
目录前言滤波电路科普主要分类实际情况单位的概念常用评价参数函数型滤波器简单分析滤波电路构成低通滤波器RC低通滤波器RL低通滤波器高通滤波器RC高通滤波器RL高通滤波器部分摘自《LC滤波器设计与制作》,侵权删。前言最近需要学习放大电路和滤波电路,但是由于只在之前做音乐频谱分析仪的时候简单了解过一点点运放,所以也是相当从零开始学习了。滤波电路科普主要分类滤波器:主要是从不同频率的成分中提取出特定频率的信号。有源滤波器:由RC元件与运算放大器组成的滤波器。可滤除某一次或多次谐波,最普通易于采用的无源滤波器结构是将电感与电容串联,可对主要次谐波(3、5、7)构成低阻抗旁路。无源滤波器:无源滤波器,又称
项目介绍随着我国经济迅速发展,人们对手机的需求越来越大,各种手机软件也都在被广泛应用,但是对于手机进行数据信息管理,对于手机的各种软件也是备受用户的喜爱小学生兴趣延时班预约小程序的设计与开发被用户普遍使用,为方便用户能够可以随时进行小学生兴趣延时班预约小程序的设计与开发的数据信息管理,特开发了小程序的设计与开发的管理系统。小学生兴趣延时班预约小程序的设计与开发的开发利用现有的成熟技术参考,以源代码为模板,分析功能调整与小学生兴趣延时班预约小程序的设计与开发的实际需求相结合,讨论了小学生兴趣延时班预约小程序的设计与开发的使用。开发环境开发说明:前端使用微信微信小程序开发工具:后端使用ssm:VU
了解Rails缓存如何工作的人可以真正帮助我。这是嵌套在Rails::Initializer.runblock中的代码:config.after_initializedoSomeClass.const_set'SOME_CONST','SOME_VAL'end现在,如果我运行script/server并发出请求,一切都很好。然而,在我的Rails应用程序的第二个请求中,一切都因单元化常量错误而变得糟糕。在生产模式下,我可以成功发出第二个请求,这意味着常量仍然存在。我已通过将以上内容更改为以下内容来解决问题:config.after_initializedorequire'some_cl
我在我的项目中有一个用户和一个管理员角色。我使用Devise创建了身份验证。在我的管理员角色中,我没有任何确认。在我的用户模型中,我有以下内容:devise:database_authenticatable,:confirmable,:recoverable,:rememberable,:trackable,:validatable,:timeoutable,:registerable#Setupaccessible(orprotected)attributesforyourmodelattr_accessible:email,:username,:prename,:surname,:
我经常迷上ruby的一件事是递归模式。例如,假设我有一个数组,它可能包含无限深度的数组作为元素。所以,例如:my_array=[1,[2,3,[4,5,[6,7]]]]我想创建一个方法,可以将数组展平为[1,2,3,4,5,6,7]。我知道.flatten可以完成这项工作,但这个问题是作为我经常遇到的递归问题的一个例子-因此我试图找到一个更可重用的解决方案。简而言之-我猜这种事情有一个标准模式,但我想不出任何特别优雅的东西。任何想法表示赞赏 最佳答案 递归是一种方法,它不依赖于语言。您在编写算法时要考虑两种情况:再次调用函数的情