我无法在驱动程序中添加子类:Martian和Saturner到我的ArrayList。
我得到一个"找不到符号错误",任何原因导致这可能是什么原因?
import java.util.*;
import java.io.*;
abstract class Alien
{
private String planet;
private String venus;
private String name;
private String skinColor;
private String alienClass;
public Alien(String planet, String venus, String name, String skinColor, String alienClass)
{
this.planet=planet;
this.venus=venus;
this.name=name;
this.skinColor=skinColor;
this.alienClass=alienClass;
}
abstract void invade();
public String toString()
{
return name + "has" + skinColor + "skin, and is apart of the " + alienClass + "class" ;
}
public abstract class Martian extends Alien
{
public Martian(String p, String v, String n, String s, String a)
{
super(p,v,n,s,a);
}
}
public abstract class Saturner extends Alien
{
public Saturner(String p, String v, String n, String s, String a)
{
super(p,v,n,s,a);
}
abstract void invade();
{
System.out.println("The aliens are invading: ");
}
}
}
public class AlienDriver
{
public static void main(String[]args)
{
ArrayList<Alien> alienList = new ArrayList<>();
alienList.add(new Martian("Hookrah"));
}
这是不相关的 我正在为这篇文章添加更多细节,因为它主要是代码,但这是无关紧要的,我只是输入这个,因为我必须。我的问题清晰简洁,代码不多,我不认为问题非常诚实。
答案 0 :(得分:1)
您的所有类都是抽象类,因此无法实例化它们。 在你的情况下:覆盖Alien子类中的invade()方法并删除'abstract'关键字。
public class AlienDriver { // only one class have 'public' keyword in a file
public static void main(String[] args){
// do your stuff
}
}
abstract class Alien {
// constructor
...
abstract void invade(); // abstract method
}
class Martian extends Alien {
// constructor
...
@Override
void invade() {
// do something such as print line
}
}
// Same for Saturner
希望有所帮助。
答案 1 :(得分:0)
也许我错过了它,但看起来你正试图使用一个带有一个参数的构造函数
new Martian("Hookrah")
但是你定义的构造函数需要5个参数
public Martian(String p, String v, String n, String s, String a)
{
super(p,v,n,s,a);
}
编辑:
继续我们在评论中的讨论,这里是应该起作用的示例代码
Alien.java
abstract class Alien
{
private String planet;
private String venus;
private String name;
private String skinColor;
private String alienClass;
public Alien(String planet, String venus, String name, String skinColor, String alienClass)
{
this.planet=planet;
this.venus=venus;
this.name=name;
this.skinColor=skinColor;
this.alienClass=alienClass;
}
abstract void invade();
public String toString()
{
return name + " has " + skinColor + " skin, and is apart of the " + alienClass + " class" ;
}
}
Martian.java
public class Martian extends Alien
{
public Martian(String p, String v, String n, String s, String a)
{
super(p,v,n,s,a);
}
@Override
void invade() {
// TODO Auto-generated method stub
}
}
Saturner.java
public class Saturner extends Alien
{
public Saturner(String p, String v, String n, String s, String a)
{
super(p,v,n,s,a);
}
void invade()
{
System.out.println("The aliens are invading: ");
}
}
AlienDriver.java
import java.util.ArrayList;
public class AlienDriver
{
public static void main(String[]args)
{
ArrayList<Alien> alienList = new ArrayList<>();
alienList.add(new Martian("MarPlanet", "MarVenus", "MarName", "MarSkinColor", "MarAlienClass"));
alienList.add(new Saturner("SatPlanet", "SatVenus", "SatName", "SatSkinColor", "SatAlienClass"));
for (Alien al : alienList) {
System.out.println("Class of Object: " + al.getClass().getName() + " || description = " + al.toString());
}
}
}
输出结果为:
Class of Object: Martian || description = MarName has MarSkinColor skin, and is a part of the MarAlienClass class
Class of Object: Saturner || description = SatName has SatSkinColor skin, and is a part of the SatAlienClass class
我不确定您是否使用IDE(如Eclipse),但我强烈建议您使用它来帮助调试。在此示例中,所有这些类都位于src文件夹的相同默认包中。