这段函数代表什么 public int compareTo(WindowContainer other) { Slog.d("huangbg", Log.getStackTraceString( new Throwable())); Slog.d("huangbg","other = " + other); if (this == other) { Slog.d("huangbg"," compareTo 929"); return 0; } Slog.d("huangbg"," mParent = " + mParent); if (mParent != null && mParent == other.mParent) { Slog.d("huangbg"," compareTo 934"); final WindowList<WindowContainer> list = mParent.mChildren; return list.indexOf(this) > list.indexOf(other) ? 1 : -1; } final LinkedList<WindowContainer> thisParentChain = mTmpChain1; final LinkedList<WindowContainer> otherParentChain = mTmpChain2; try { getParents(thisParentChain); other.getParents(otherParentChain); // Find the common ancestor of both containers. WindowContainer commonAncestor = null; WindowContainer thisTop = thisParentChain.peekLast(); WindowContainer otherTop = otherParentChain.peekLast(); while (thisTop != null && otherTop != null && thisTop == otherTop) { commonAncestor = thisParentChain.removeLast(); otherParentChain.removeLast(); thisTop = thisParentChain.peekLast(); otherTop = otherParentChain.peekLast(); } Slog.d("huangbg"," commonAncestor = " + commonAncestor); // Containers don't belong to the same hierarchy??? if (commonAncestor == null) { throw new IllegalArgumentException("No in the same hierarchy this=" + thisParentChain + " other=" + otherParentChain); } // Children are always considered greater than their parents, so if one of the containers // we are comparing it the parent of the other then whichever is the child is greater. if (commonAncestor == this) { Slog.d("huangbg"," compareTo 965"); return -1; } else if (commonAncestor == other) { Slog.d("huangbg"," compareTo 968"); return 1; } Slog.d("huangbg"," compareTo 971"); // The position of the first non-common ancestor in the common ancestor list determines // which is greater the which. final WindowList<WindowContainer> list = commonAncestor.mChildren; return list.indexOf(thisParentChain.peekLast()) > list.indexOf(otherParentChain.peekLast()) ? 1 : -1; } finally { mTmpChain1.clear(); mTmpChain2.clear(); } }
时间: 2023-02-11 10:55:37 浏览: 162
这段代码是一个比较函数,它重写了 Java 中的 Comparable 接口中的 compareTo 方法。它的作用是用于比较两个 WindowContainer 对象的大小。在代码中,首先使用了 Slog.d() 方法输出了一些日志,用于调试。然后比较了 this 和 other 两个对象是否相等,如果相等,返回 0;否则返回其他值。
相关问题
怎么在这段代码里打出this的值 class WindowContainer<E extends WindowContainer> extends ConfigurationContainer<E> implements Comparable<WindowContainer>, Animatable { public int compareTo(WindowContainer other) { Slog.d("huangbg","other = " + other); if (this == other) { Slog.d("huangbg"," compareTo 929"); return 0; } } }
在这段代码中,可以使用 "this" 关键字打印出当前对象的值。例如,在 compareTo 方法中,可以使用 Slog.d("huangbg"," compareTo 929") 打印 this 对象的值。
int compareTo1 = pfslBig.compareTo(new BigDecimal(1));
这行代码是将一个名为pfslBig的BigDecimal对象与值为1的另一个BigDecimal对象进行比较,并将结果存储在名为compareTo1的整数变量中。compareTo()方法将返回一个整数值,该值指示调用对象与传递的对象之间的关系。如果调用对象小于传递的对象,则返回负整数;如果它们相等,则返回零;如果调用对象大于传递的对象,则返回正整数。
```java
BigDecimal pfslBig = new BigDecimal("2.5");
int compareTo1 = pfslBig.compareTo(new BigDecimal(1));
```
在这个例子中,pfslBig对象的值为2.5,它将与值为1的另一个BigDecimal对象进行比较。由于pfslBig大于1,因此compareTo()方法将返回一个正整数,该整数将存储在compareTo1变量中。
阅读全文