public class Test {
static Animal[] animals = new Animal[3];
public static void main(String[] args) {
animals[0] = new Cat("加菲猫",4);
animals[1] = new Ya("唐小鸭",2);
animals[2] = new Shark("小鲨",0);
Scanner input = new Scanner(System.in);
System.out.println("动物名字\t\t腿的条数\t\t动物叫");
for (int i = 0; i < animals.length; i++) {
System.out.println(animals[i].getName() + "\t\t" +animals[i]. + "\t\t"+ animals[i].print());
}
public class Cat extends Animal implements leng {
private int strain;
public Cat(String name, int strain) {
super(name);
this.strain = strain;
}
public String print() {
String speak = "喵喵喵....";
return speak;
}
为什么引用子类构造并赋值时子类的那个this.strain没有显示值
请问下楼主 能否给出全部的源码 方便进行调试查看原因
这是父类
package animalleyuan;
//抽象类
abstract class Animal {
// 动物共有的属性
private String name;
public Animal(String name) {
this.name = name;
}
public String getName() {
return name;
}
// 抽象类方法
abstract String print();
}
这是子类同时实现了接口
package animalleyuan;
public class Cat extends Animal implements leng {
private String strain;
public Cat(String name, String strain) {
super(name);
this.strain = strain;
}
// 重写父类的方法
public String print() {
String speak = "喵喵喵....";
return speak;
}
@Override // 实现接口
public int leng() {
return 4;// 返回腿的数量
}
}
这是接口
package animalleyuan;
//接口
public interface leng {
int leng();
}
这是测试类 另外说一句,子类还有2个,不过与第一个一样的...第一个解决后面就解决了
package animalleyuan;
import java.util.InputMismatchException;
import java.util.Scanner;
import animallianxi.GeIt;
public class Test {
static Animal[] animals = new Animal[3];
public static void main(String[] args) {
/**
* 父类依次对子类实例化 调用有参构造初始化属性
*/
animals[0] = new Cat("加菲猫","优");
animals[1] = new Ya("唐小鸭",2);
animals[2] = new Shark("小鲨",0);
Scanner input = new Scanner(System.in);
// 定义变量存储临时数据
try {
do {
System.out.println("动物名字\t\t腿的条数\t\t动物叫");
for (int i = 0; i < animals.length; i++) {
System.out.println(animals[i].getName() + "\t\t" +animals[i]. + "\t\t"+ animals[i].print());
}
System.out.println("是否要继续修改数据:按0进行修改操作,按其它任意数字键退出");
int num = input.nextInt();
if (num == 0) {
System.out.println("请选择要输入的动物名称:\t1.猫类\t2.鸭类\t3.鱼类");
int sum = input.nextInt();
// 临时姓名
String[] names = new String[3];
switch (sum) {
/**
* 猫类
*/
case 1:
System.out.println("请输入猫的名称");
String name1 = input.next();
names[0] = name1;
System.out.println("请输入腿的条数");
int leng1 = input.nextInt();
if (leng1 == 4) {// 对输入的值判断
// System.out.println(names[0] + "\t\t" + "\t\t" + animals[0].print());// 显示结果
} else {
throw new InputMismatchException("猫只能有4条腿");
}
break;
/**
* 鸭类
/
case 2:
System.out.println("请输入鸭的名称");
String name2 = input.next();
names[1] = name2;
System.out.println("请输入腿的条数");
int leng2 = input.nextInt();
if (leng2 == 2) {// 对输入的值判断
// System.out.println(names[1] + "\t\t" + "\t\t" + animals[1].print());// 显示结果
} else {
throw new InputMismatchException("鸭只能有2条腿");
}
break;
/*
* 鱼类
*/
case 3:
System.out.println("请输入鱼的名称");
String name3 = input.next();
names[2] = name3;
System.out.println("请输入腿的条数");
int leng3 = input.nextInt();
if (leng3 == 0) {// 对输入的值判断
System.out.println(animals[2].getName() + "\t\t" + "\t\t" + animals[2].print());// 显示结果
} else {
throw new InputMismatchException("鲨鱼只能有0条腿");// 手动抛出异常信息
}
break;
}
}
} while (0 != 0);
System.out.println("谢谢回顾");
System.exit(1);// 结束java虚拟机
} catch (InputMismatchException e) {// 捕获异常
System.err.println("输入错误,类型不匹配.");// 抛出异常
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("谢谢使用本程序!");
}
}
}
animals[i].点不出来 strain的信息
但是在调用构造方法时已经赋参数了
不知道你能否看到,,,,,,,,等待ing