Java中唯一的运算符重载----“+”,我打算去考sun公司的认证,结果发现还有这么个知识点,以前一直也没有用过,望高人指点。
    1. 是不是这种就算是重载了
     String str1="hello";
     String str2=" world!";
    String str3=str1+str2;
   2.还是说要向以前c++里面那种写法,比如写一个复数计算的类public class Complex {  private   float   real; 
    private   float   image;
    Complex() {
    
    }
      Complex(float   r,float   i) 
              {real=r;     image=i;} 
      
    public   float   getreal()
    
    {return   real;} 
    public   float   getimage()
    
    {return   image;} 
    public   void   print()      
    {System.out.print( "( "+real+ "+ "+image+ "i) ");}  Complex  operator + (Complex   a,Complex  b) 
{   float   r=a.getreal()+b.getreal(); 
    float   i=a.getimage()+b.getimage(); 
  return   Complex(r,i); 

public static void main(String[] args) {
 
Complex  m=new Complex(2,3);
Complex  n=new Complex(4,5);
Complex q=new Complex(0,0); 
q=m+n; 
m.print(); 
System.out.print( "+ "); 
n.print(); 
System.out.print( "= "); 
q.print(); 
System.out.println(); 


// 请问问题出在哪里?
}
    

解决方案 »

  1.   

    java里面不允许重载运算符。所谓唯一的重载运算符是指字符串的连接符“+”,jdk里面已经实现好了,但不会允许客户程序员去重载操作符的,。
      

  2.   

    嗯,,对的,,我们个人是无法去重载算术符的,,你上面说的是这个意思把,,+本来表示俩个数的算术运算,,而在String str1="hello"; 
         String str2=" world!"; 
        String str3=str1+str2;
    这里是实现了字符串的连接,,我说的不知道可满意