父类与子类this相关问题
1.SinglyLinkedList:
package No3_PolySinglyList; /*实现 带头结点的单链表SinglyLinkedList类*/ public class SinglyLinkedList<T> extends Object { private Node<T> head; // 头结点 public SinglyLinkedList() { this.head = new Node<T>(); } public SinglyLinkedList(T[] element) {// 由指定数组中的多个对象构造单链表 this(); Node<T> rear = this.head; for (int i = 0; i < element.length; i++) { rear.next = new Node<T>(element[i], null); rear = rear.next; } } public SinglyLinkedList(SinglyLinkedList<T> list) {// 以单链表list构造新的单链表,复制单链表 this(); Node<T> rearOld = list.head; // rearOld为已有链表的结点 Node<T> rearNew = this.head; // rearNew为新链表的结点 while (rearOld.next != null) { rearNew.next = new Node<T>(rearOld.next.data, null); rearNew = rearNew.next; rearOld = rearOld.next; } } public int size() {//计算该单链表的长度(元素个数) int size=0; Node<T> front=this.head; while(front.next !=null) { size++; } return size; } public Node<T> insert(int i,T x){ //插入对象x作为第i个元素 if(x==null) throw new NullPointerException("x==null"); //下标容错 if(i<0) i=0; else if(i>this.size()) i=this.size()-1; Node<T> front=this.head; for(int j=0;front.next!=null&&j<i;j++) { front=front.next; } front.next=new Node<T>(x,front.next); return front.next; } public Node<T> insert(T x){//在单链表最后插入对象x return insert(Integer.MAX_VALUE,x); } public String toString() { Node<T> rear = this.head.next; // rear1为前面链表的结点 String str = "("; while (rear != null) { str += rear.data.toString(); if (rear.next != null) str += ","; rear = rear.next;// 注意!!该语句不能放在if判断语句内 } return str + ")"; } }
2.PolySinglyList:
package No3_PolySinglyList; /** * 带头结点的一元多项式链表 * * @author Navis * * @param <T> */ public class PolySinglyList<T> extends SinglyLinkedList<T> { protected Node<T> head; public PolySinglyList() { this.head = new Node<T>(); } public PolySinglyList(T[] terms) { this(); Node<T> rear = this.head; for (int i = 0; i < terms.length; i++) { rear.next = new Node<T>(terms[i], null); rear = rear.next; } } public PolySinglyList(PolySinglyList<T> list) { this(); Node<T> rearOld = list.head.next; // rearOld为参数链表的结点 Node<T> rearNew = this.head; // rearNew为新链表的结点 while (rearOld != null) { rearNew.next = new Node<T>(rearOld.data, null); rearNew = rearNew.next; rearOld = rearOld.next; } } public PolySinglyList<T> union(PolySinglyList<T> list) { // 两个一元多项式相加 Node<T> rear1; // rear1为原链表的结点 Node<T> rear2; // rear2为参数链表的结点 Node<T> rear1_front; // rear1的前继结点 for (rear2 = list.head.next; rear2 != null; rear2 = rear2.next) { System.out.println("rear2=" + rear2); boolean flag = false; for (rear1_front = this.head, rear1 = rear1_front.next; rear1 != null; rear1_front = rear1, rear1 = rear1.next) { System.out.println("rear1=" + rear1); flag = ((TermX) rear1.data).add((TermX) rear2.data); if (flag) break; } if (rear1 == null) {// 若this链表中没有list中该元素相同指数的结点,则复制该节点,插在this链表最后(链表没有排序) // 插入rear2指向元素,rear1指向该结点 rear1 = this.insert(rear2.data); System.out.println("rear1 =this.insert(rear2.data)=" + rear1); } if (((TermX) rear1.data).coef == 0) {// 若该元素系数为0,则删去 rear1_front.next = rear1.next; } System.out.println(this); } return this; } public String toString() { Node<T> rear = this.head.next; String str = ""; while (rear != null) { str += rear.data.toString(); rear = rear.next; } return str; } }
3.No3://Test
package No3_PolySinglyList; /*一元多项式相加*/ public class No3 { public static void main(String[] args) { TermX[] termA = { new TermX(-7, 9), new TermX(2, 7), new TermX(-9, 4), new TermX(1, 2), new TermX(-1, 1), new TermX(2, 0) }; PolySinglyList<TermX> polyListA = new PolySinglyList<TermX>(termA); PolySinglyList<TermX> polyListA2 = new PolySinglyList<>(polyListA); System.out.println("polyListA2=" + polyListA2); TermX[] termB = { new TermX(-1, 0), new TermX(1, 1), new TermX(-1, 2), new TermX(10, 4), new TermX(-3, 8), new TermX(5, 10),new TermX(9, 11) }; PolySinglyList<TermX> polyListB = new PolySinglyList<>(termB); System.out.println("polyListB=" + polyListB); PolySinglyList<TermX> polyListC = polyListA.union(polyListB); System.out.println("两式相加得:\n polyListC=" + polyListC); } }
输出结果:
(图一)
(图二)
问题:
图一运行结果正常,问题出在图二的-3x^8项。
ListA中没有ListB的该元素,所以调用PolySinglyList类的父类SinglyLinkedList的方法
Node<T> insert(T x)。语句返回结果正常:-3x^8,可是this(指PolySinglyList类的该对象)没有变,仍是-7x^9+2x^7+x^4+1。
待解决