求助,求大神指出错误啊!
package 实验;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;
try {
Work w = new Work();
w.getAdd("凤凰").speakName();
} catch (Exception e) {
System.out.println(e);
}
}
}
结果是 这是一只 null
我希望是 这是一只凤凰
[ 本帖最后由 long8863 于 2013-3-5 19:30 编辑 ]