我自己学习java,我来了一个有趣的例子,我发现很难理解它。
我想知道的是,当您在同一软件包下有几个类的Java中,这在Java中所说的是重叠的。请查看下面的示例?请注意,所有类都没有使用工具,接口,摘要,扩展等...
是否可以找到更多这些示例?
类Flighplan
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中叫什么 在同一软件包下的几个类都重叠?
这不是重叠的,而是称为循环依赖关系,因为您的Flightplan
和Person
彼此依赖,这是不良的设计且发展不佳。
基本上循环依赖性会导致很多问题(例如OutofMemoryError
),如果不正确使用,则应在类/软件包之间避免它们。
您可以在这里查找有关循环依赖的更多详细信息。
通过重叠,您的意思是它们具有具有相同名称的成员变量?每个类都有不同的成员变量列表,一个类别不限制另一个类。
我认为这被称为"每个涉及相似数据值的几个类"