package test;
import java.util.*;
class Complex{
int a;
int b;
Complex(int a,int b)
{
this.a=a;
this.b=b;
}
Complex()
{
}
Complex(Complex t1){
this.a=t1.a;//s调用的this这里就是指s
this.b=t1.b;
}
Complex Add(Complex r)
{
this.a+=r.a;
this.b+=r.b;
return this;
}
Complex Sub(Complex r)
{
this.a-=r.a;
this.b-=r.b;
return this;
}
void Print()
{
if(a==0&&b==0)
System.out.println(0);
else
System.out.println(a+"+"+b+'i');
}
}
public class Main{
public static void main(String[] args) {
int m,n; //第一个数的实部和虚部
int p,q; //第二个数的实部和虚部
Scanner scanner =new Scanner(System.in);
m=Integer.parseInt(scanner.next());
n=Integer.parseInt(scanner.next());
p=Integer.parseInt(scanner.next());
q=Integer.parseInt(scanner.next());
Complex t =new Complex(m,n);
Complex s =new Complex(t);
Complex r=new Complex(p,q);
(s.Add(r)).Print();
s=new Complex(t);
(s.Sub(r)).Print();
}
}
终于明白了
t将自己的引用传递给了t1
此时t和t1指向同一块实体
s自己存的就是个引用地址 这个地址和t和t1都无关
借用this指针来为默认的成员变量赋值
add和sub中
最后return this 就是说返回当前对象 即s