抱歉,我是大学编程的新手。这是我们测试的练习题,并在代码运行器上设置。整个main方法以及所有类,方法,构造函数和变量都已经提供给我了,我必须使显示的类打印出我写的内容。但Bride.getAge()
中的Location.getSuburb()
和println
将无效。我是否需要添加其他内容?
public class Location {
public static void main(String[] args) {
Bride person = new Bride("Amy Cronos", 29);
Location place = new Location("Tonsley", "South Rd");
Wedding wed = new Wedding(person,place);
show(wed);
}
public static void show(Wedding wed){
System.out.println("Wedding data:" );
System.out.println("Bride: " + wed.getBride() + ", age: " + Bride.getAge);
System.out.println("Location: " + wed.getPlace() + ", suburb: " + Location.getSuburb());
}
private String suburb;
private String street;
Location(String suburb, String street){
this.suburb = suburb;
this.street = street;
}
public String getSuburb(){
return suburb;
}
public String getStreet(){
return street;
}
public class Bride {
private String name;
private int age;
Bride(String name, int age){
this.name = name;
this.age = age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
}
public class Wedding {
private Bride person;
private Location place;
Wedding(Bride person, Location place){
this.person = person;
this.place = place;
}
public Bride getPerson(){
return person;
}
public Location getPlace(){
return place;
}
}
}
答案 0 :(得分:0)
您需要更正show
方法
System.out.println("Bride: " + wed.getBride() + ", age: " + wed.getPerson().getAge());
System.out.println("Location: " + wed.getPlace() + ", suburb: " + wed.getPlace().getSuburb());
由于您使用的方法包含Bride.getAge()
和Location.getSuburb()
方法,因此这些方法被视为静态方法。因此错误。由于这些不是静态方法,因此您需要使用类的对象来访问它们而不是类名。
答案 1 :(得分:0)
你已经通过了类Bride和Location的对象所以你需要获得如下代码的值:尝试使用下面的代码并尝试理解java中的对象,静态是不同的东西。
public static void show(Wedding wed){
System.out.println("Wedding data:" );
System.out.println("Bride: " + wed.getPerson() + ", age: " + wed.getPerson().getAge());
System.out.println("Location: " + wed.getPlace() + ", suburb: " + wed.getPlace().getSuburb());
}