C++&;Java重载运算符 我很难理解C++和java中重载运算符的主题。

C++&;Java重载运算符 我很难理解C++和java中重载运算符的主题。,java,c++,Java,C++,例如,我定义了一个新的类分数: class Fraction { public: Fraction (int top, int bottom) { t = top; b = bottom; } int numerator() { return t; } int denominator() { return b; } private: int t, b; }; 我想让操作员过载 在java中,是否可以重载运算符 不,java没有操作符重载。 C++

例如,我定义了一个新的类分数:

class Fraction { 
public: 
    Fraction (int top, int bottom) { t = top; b = bottom; } 
    int numerator() { return t; } 
    int denominator() { return b; } 
private: 
    int t, b; 
};
我想让操作员过载

在java中,是否可以重载运算符


<>不,java没有操作符重载。

C++:重载C++中的,可以为应用的类重载运算符。对你来说,你应该

class Fraction { 
public: 
    Fraction (int top, int bottom) { t = top; b = bottom; } 
    int numerator() { return t; } 
    int denominator() { return b; } 
    inline bool operator << (const Fraction &f) const
    {
        // do your stuff here
    }

private: 
    int t, b; 
};
类分数{
公众:
分数(int-top,int-bottom){t=top;b=bottom;}
int分子(){return t;}
int分母(){return b;}

内联布尔运算符要给您一个由我、Marcelo和David Rodríguez-dribeas在评论中提供的完整答案:


在Java中,不能重载运算符

要完成我的回答:

[……], 但是+和+=运算符是 默认情况下为字符串重载 连接。这是唯一的 例外

@马塞洛

<>和关于C++重载操作符:


对于C++的一方,请看这个问题:


<戴维> RoDr.Guez——Dr.java在爪哇中不能超载运算符,这里是一个例子,如何超载<代码> >不能在Java中重载运算符,但+和+=运算符默认为字符串连接超载。这是唯一的例外。
class Fraction { 
public: 
    Fraction (int top, int bottom) { t = top; b = bottom; } 
    int numerator() { return t; } 
    int denominator() { return b; } 
    inline bool operator << (const Fraction &f) const
    {
        // do your stuff here
    }

private: 
    int t, b; 
};