【问题标题】:Overlapping classes under the same package in javajava中同一包下的重叠类
【发布时间】:2016-11-26 19:30:22
【问题描述】:

我正在自学 Java,遇到了这个有趣的例子,我发现它很难理解。

我想知道的是,当您在同一个包下有多个类时,Java 中的这称为什么,所有这些类都重叠。请看下面的例子?请注意,没有一个类使用实现、接口、抽象、扩展等......

有没有可能找到更多这样的例子?

类飞行计划

public class Flightplan {
    String type;
    int seat;
    String from;
    String to;
         // Other local variables, style captain ...
    Person[] passenger;
    int counter = 0;

    Flightplan (String t, int s, String startPlace, String d) {
        type = t;
        seat = s;
        passenger = new Person [s-1]; // kapten tar ett säte 
          // Captain takes a seat
        from = startPlace;
        to = d;
    }

    void book (Person p, String f, String t) {
        if (f.equals(from) && t.equals(to)) {
            passenger[counter] = p;
            to = t;
            counter++;
        }
                else System.out.println(p.name + " try to book a wrong flight !");
    }

    void flyg() {
        System.out.println("On the plane " + this.typ + " reser"); 
        for (int i = 0; i < passenger.length && passenger[i] != null; i++) {
            System.out.println(passenger[i].name + ", ");
        }
        System.out.println("\n");
        from = to;
    }

}

类人

public class Person {
    String name;
    String from;
    String to;
    String stopover;
    int bags;
    Flightplan flight;

    Person (String n, String f, String m, String t, int v) {
        name = n;
        from = f;
        stopover = m; // Only one stopover is approved, otherwise we would enter this as an array
        to = t;
        bags = v;
    }

    void boardNextLeg(Flightplan plan) {
        flight = plan;

// Function bar for a stopover due. if-kit building
        if (!stopover.equals(null) && flight.from.equals(this.from) && flight.to.equals(this.stopover)) { 
            System.out.print(this.name + " is now in between");
            System.out.println(from + " and " + stopover);
            flight.book(this, from, stopover);
        }
        else if (flight.from.equals(this.from) && flight.to.equals(this.to)) {
            System.out.println(from + " och " + to);
            flight.book(this, from, to);
        }

                else System.out.println(this.name + " could not be booked on a flight");
    }


}

【问题讨论】:

    标签: java


    【解决方案1】:

    当你有 同一个包下的几个类都是重叠的?

    这不是重叠的,而是称为循环依赖,因为您的FlightplanPerson 相互依赖,这是糟糕的设计和糟糕的开发。

    如果使用不当,基本上循环依赖会导致很多问题(如OutofMemoryError),因此应避免在类/包之间使用它们。

    您可以查看here 了解有关循环依赖的更多详细信息。

    【讨论】:

      【解决方案2】:

      重叠是指它们具有同名的成员变量?每个类都有不同的成员变量列表,一个类对另一个类没有任何限制。

      我认为这被称为“几个类,每个类都处理相似的数据值”

      【讨论】:

        猜你喜欢
        • 2014-04-12
        • 2011-08-23
        • 1970-01-01
        • 2022-08-11
        • 2019-06-19
        • 1970-01-01
        • 1970-01-01
        • 2014-09-06
        • 1970-01-01
        相关资源
        最近更新 更多