From 6cacdf8d7f6806bb948a2261c4d7a4a236a89061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cidpeng=E2=80=9D?= <630606938@qq.com> Date: Mon, 23 Aug 2021 18:04:12 +0800 Subject: [PATCH] =?UTF-8?q?=C3=A5=C2=B7=E5=B7=A5=E5=8E=82=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/github/hcsp/encapsulation/Cat.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/github/hcsp/encapsulation/Cat.java b/src/main/java/com/github/hcsp/encapsulation/Cat.java index 97eba15..b90da82 100644 --- a/src/main/java/com/github/hcsp/encapsulation/Cat.java +++ b/src/main/java/com/github/hcsp/encapsulation/Cat.java @@ -5,6 +5,11 @@ public class Cat { private String name; private int age; + public Cat(String name, int age) { + this.name = name; + this.age = age; + } + /** * 创建一只猫的工厂方法。当传入的参数无效,即: * @@ -18,7 +23,13 @@ public class Cat { * @param name 名字 * @return 创建的猫 */ - public static Cat newCat(String name, int age) {} + public static Cat newCat(String name, int age) { + if(name == null || age< 0.2 ){ + return INVALID_CAT; + }else { + return newCat(name,age); + } + } public String getName() { return name;