java设计一个类,该类包含两个成员变量,X,Y,两个成员方法:getX(),gety().

2024-12-29 05:44:53
推荐回答(2个)
回答1:

public class Function {
private double x;
private double y;

public Function(double a,double b)
{
this.x = a;
this.y = b;
}

public double getX()
{
return this.x;
}

public double getY()
{
return this.y;
}

public static void main(String []args)
{
Function tester = new Function(1.5,1.6);
System.out.println(tester.getX() + "+" + tester.getY());
}
}

回答2:

好久没有碰JAVA了,不知道它的代码风格了。
弄个C++的吧,反正和JAVA在这方面差不了多少。
(C++中 this为指针,且只能用->引用其成员,
而JAVA必须使用 this. 。JAVA中输入输出是不是用
System.out()??? C++中使用的是 cout)

#include
using namespace std;

Class Point
{
private:
int X, Y;
public:
Point();
{}
Point(Point p);
{
this->X = p.X;
this->Y = p.Y;
}
Point(int x, int y);
{
this->X = x;
this->Y = Y;
}
void setX(x)
{
this->X = x;
}
void setY(y)
{
this->Y = y;
}
int getX()
{
return this->X;
}
int getY()
{
return this->Y;
}
};

int main(int argc, char *argv)
{
Point p(0, 0);
Point t(50, 40);
point r(t), s;

cout << "p.X = " << p.getX() << endl;
cout << "p.Y = " << p.getY() << endl;

s.setX(30);
s.setY(48);

cout << "r.X = " << r.getX() << endl;
cout << "r.Y = " << r.getY() << endl;

return 0;
}