我需要这个问题的帮助,有人可以教我如何在主要方法中从ActorAll []类中打印Actor []数组吗?我可以从Actor()类进行打印,但是由于我不知道的原因而无法从ActorAll()进行打印,非常感谢有人可以帮助我。
public class Actor {
private String fName;
private String lName;
public Actor(String f,String l){
setfName(f);
setlName(l);
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public String displayMessage(){
return String.format("%s %s", getfName(),getlName());
}
}
public class ActorAll extends Actor{
private Actor[]actor;
public ActorAll(Actor[] actor){
setActor(actor);
}
public Actor[] getActor() {
return actor;
}
public void setActor(Actor[] actor) {
this.actor = actor;
}
public String displayMessage(){
return String.format("%s ", getActor());
}
}
public class ActorAllTest {
static Scanner sc= new Scanner(System.in);
public static void main(String[] args) {
Actor actor;
actor=addContact();
System.out.println(actor.displayMessage());
}
public static Actor addContact(){
String fName=sc.next();
String lName=sc.next();
Actor actor = new Actor(fName,lName);
return actor;
}
}
最佳答案
如果要从ActorAll打印actor[]
,则需要创建一个ActorAll
动态类型对象,并在该对象上调用displayMessage
。
public static void main(String[] args) {
Actor actor = new ActorAll();
actor=addContact();
System.out.println(actor.displayMessage());
}