java中相同包下的重叠类
我正在自学Java,我遇到了这个有趣的例子,我发现很难理解它 我想知道的是,在Java中,当在同一个包下有几个类时,这些类都是重叠的,这叫什么。请看下面的例子?请注意,没有一个类使用实现、接口、抽象、扩展等 有可能找到更多这样的例子吗 班级飞行计划java中相同包下的重叠类,java,Java,我正在自学Java,我遇到了这个有趣的例子,我发现很难理解它 我想知道的是,在Java中,当在同一个包下有几个类时,这些类都是重叠的,这叫什么。请看下面的例子?请注意,没有一个类使用实现、接口、抽象、扩展等 有可能找到更多这样的例子吗 班级飞行计划 public class Flightplan { String type; int seat; String from; String to; // Other local variables, s
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;
}
}
重叠是指它们的成员变量具有相同的名称?每个类都有不同的成员变量列表,一个类对另一个类没有限制 我认为这被称为“几个类,每个类处理相似的数据值” 当您有 同一个包下的几个类都是重叠的 这不是重叠,而是称之为循环依赖关系,因为您的
飞行计划
和人员
相互依赖,这是一种糟糕的设计和开发
如果使用不当,循环依赖基本上会导致很多问题(如outofmemoryrerror
),因此应避免在类/包之间使用循环依赖
您可以查看有关循环依赖项的更多详细信息
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");
}
}