我遇到了運行此代碼的問題,我試圖讓程序使用來自其他類的輸入來打印下面的字符串。正如你所看到的,放入新娘和新位置對象的信息將被放入婚禮對象中,然後我需要嘗試從婚禮對象中檢索細節並將其顯示在屏幕上,如下所示:方法之間的Java對象範圍
Wedding data:
Bride: Amy Cronos, age: 29
Location: South Rd, suburb: Tonsley
但我不是遇到了與該place.getName,place.getSuburb()等等,等等這說
Main.java:6: error: cannot find symbol System.out.println("Location"+place.getStreet()+", suburb:
"+place.getsuburb());symbol: variable place
location: class Main
我敢肯定,這事做與範圍的4級相同的錯誤,但無法解決我需要做的事情。
什麼是導致這個錯誤,我該如何解決它?
下面是代碼:
public class WeddingDetails {
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: " + person.getName() + ", age: " + person.getAge());
System.out.println("Location: " + place.getStreet() + ", suburb: " + place.getSuburb());
}
public static class Location {
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 static 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 static class Wedding {
private Bride person;
private Location place;
Wedding(Bride person, Location place) {
this.person = person;
this.place = place;
}
public Bride getBride() {
return person;
}
public Location getPlace() {
return place;
}
}
}
歡迎SO不幸的是有幾個問題你的問題:。標題有無關的問題,但目前還不清楚你的問題其實是,你有一個基本的語法錯誤,不適合SO的內容。我建議你刪除該問題,閱讀有關如何提出問題的文檔,然後重試。 – sprinter
你正試圖訪問show()方法中的'person'並在main中聲明它。先嚐試從'結婚'獲得'新娘'。 – jmw5598