方法

时间:2017-04-06 02:47:16

标签: java object methods

我遇到运行此代码的问题,我试图通过使用其他类的输入来让程序打印下面的字符串。正如您所看到的,放入新的Bride和Location对象的信息被放入婚礼对象中,然后我需要尝试从婚礼对象中检索细节并将其显示在屏幕上,如下所示:

  

婚礼资料:
  新娘:Amy Cronos,年龄:29岁   地点:南路,郊区:Tonsley

但我遇到了4个与place.getName,place.getSuburb()等有关的相同错误。

  

Main.java:6:错误:找不到符号   System.out.println("位置" + place.getStreet()+",郊区:
  &#34 + place.getsuburb());

     

符号:变量位置

     

location:class Main

我很确定这与范围有关,但无法解决我需要做的事情。

导致此错误的原因是什么?如何解决?

以下是代码:

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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这里的问题是你的println语句试图访问对象中的方法,但是通过在错误的对象上调用这些方法。您应该通过婚礼课程访问新娘和地点对象' getter(getBride()和getPlace()。完整的调用将是wed.getBride()。getName()和wed.getPlace()。getStreet()等等。

更正后的代码如下。注意:为了能够编译一个类中的所有代码,我将static关键字添加到Bride,Location和Wedding类声明中。您可以删除static并将每个类复制并粘贴回.java文件。

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: " + wed.getBride().getName() + ", age: " + wed.getBride().getAge());
        System.out.println("Location: " + wed.getPlace().getStreet() + ", suburb: " + wed.getPlace().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;
        }
    }
}