这是我的四个班级:
J_Animal.java
package Animal;
public abstract class J_Animal
{
protected String name;
protected int age;
protected int weight;
public void showInfo()
{
System.out.println("Name : " + this.name);
System.out.println("Age : " + this.age);
System.out.println("Weight : " + this.weight);
}
public abstract void move();
public abstract void eat();
}
J_Bird.java
package Animal;
import Animal.J_Animal;
public class J_Bird extends J_Animal
{
public J_Bird(String name, int age, int weight)
{
this.name = name;
this.age = age;
this.weight = weight;
}
public void move()
{
System.out.println("I'm a bird whose name is " + this.name + ". And I'm flying in the sky.");
}
public void eat()
{
System.out.println("I'm a bird whose name is " + this.name + ". And I'm eating.");
}
}
J_Fish.java
package Animal;
import Animal.J_Animal;
public class J_Fish extends J_Animal
{
public J_Fish(String name, int age, int weight)
{
this.name = name;
this.age = age;
this.weight = weight;
}
public void move()
{
System.out.println("I'm a fish whose name is " + this.name + ". And I'm swimming in the water.");
}
public void eat()
{
System.out.println("I'm a fish whose name is " + this.name + ". And I'm eating.");
}
}
J_Mammal.java
package Animal;
import Animal.J_Animal;
public class J_Mammal extends J_Animal
{
public J_Mammal(String name, int age, int weight)
{
this.name = name;
this.age = age;
this.weight = weight;
}
public void move()
{
System.out.println("I'm a mammal whose name is " + this.name + ". And I'm walking on the ground.");
}
public void eat()
{
System.out.println("I'm a mammal whose name is " + this.name + ". And I'm eating.");
}
}
我写了一个测试类来试试。 的 J_TestAnimal.java
import Animal.*;
public class J_TestAnimal
{
public void TestForFish()
{
//J_Animal testForAnimal = J_Animal("Joe" , 2, 10);
J_Fish testForAnimal = J_Fish("Joe", 2, 10);
testForAnimal.showInfo();
}
public static void main(String args[])
{
J_TestAnimal j_test =new J_TestAnimal();
j_test.TestForFish();
}
}
这是我的目录: C:\ J_TestAnimal
\动物
\J_Animal.class
\J_Fish.class
\J_Bird.class
\J_Mammal.class
\J_Animal.java
\J_Fish.java
\J_Bird.java
\J_Mammal.java
\ J_TestAnimal.java
当我运行命令“javac J_TestAnimal.java”时,错误“找不到符号”发生在arond:J_TestAnimal.java:8:错误:找不到符号
J_Fish testForAnimal = J_Fish("Joe", 2, 10);
Symbol : Method J_Fish(String, int, int)
Position : Class J_TestAnimal
答案 0 :(得分:1)
使用new
关键字创建J_Fish
:
J_Fish testForAnimal = new J_Fish("Joe", 2, 10);