ckage 实验;
abstract class Bred {
String name; public void speakName() {
System.out.println("这是一只 " + this.name);
} public void math() {
}; public String toString() {
return name;
}
}class FengHuang extends Bred {
final String name = "凤凰"; public void math() {
System.out.println("他会做算术");
}
}class Psittaciformes extends Bred {
final String name = "鹦鹉"; public void math() {
System.out.println("他会做算术");
}
}class MaQue extends Bred {
final String name = "麻雀"; public void math() {
System.out.println("他不会做算术");
}
}class Work { public static Bred getAdd(String name) {// throws Exception {
Bred aa = null;
// try {
if ("凤凰".equals(name)) {
aa = new FengHuang();
}
if ("鹦鹉".equals(name)) {
aa = new Psittaciformes();
}
if ("麻雀".equals(name)) {
aa = new MaQue();
}
// } catch (Exception e) {
// System.out.println(e);
// }
return aa;
}
}public class WorkGround {
public static void main(String[] args) {
//Bred a=null;
Work w=new Work();
System.out.println(w.getAdd("凤凰"));
}
}
执行结果为 null
为什么啊,求指教
abstract class Bred {
String name; public void speakName() {
System.out.println("这是一只 " + this.name);
} public void math() {
}; public String toString() {
return name;
}
}class FengHuang extends Bred {
final String name = "凤凰"; public void math() {
System.out.println("他会做算术");
}
}class Psittaciformes extends Bred {
final String name = "鹦鹉"; public void math() {
System.out.println("他会做算术");
}
}class MaQue extends Bred {
final String name = "麻雀"; public void math() {
System.out.println("他不会做算术");
}
}class Work { public static Bred getAdd(String name) {// throws Exception {
Bred aa = null;
// try {
if ("凤凰".equals(name)) {
aa = new FengHuang();
}
if ("鹦鹉".equals(name)) {
aa = new Psittaciformes();
}
if ("麻雀".equals(name)) {
aa = new MaQue();
}
// } catch (Exception e) {
// System.out.println(e);
// }
return aa;
}
}public class WorkGround {
public static void main(String[] args) {
//Bred a=null;
Work w=new Work();
System.out.println(w.getAdd("凤凰"));
}
}
执行结果为 null
为什么啊,求指教