gpt4 book ai didi

java - 如何检查对象类别

转载 作者:行者123 更新时间:2023-12-01 23:56:08 25 4
gpt4 key购买 nike

当我尝试获取对象类型时,我做错了什么。

我正在尝试对两个 vector 求和,它可以是笛卡尔坐标(x,y)或极坐标(方位角,长度)。

我如何检查我拥有什么类型的对象?

这是我的代码:

import java.lang.Math;
/**
* Write a description of class Velocity here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Velocity
{
// instance variables - replace the example below with your own
private double x;
private double y;
private double azimuth;
private double length;
private double prod;
private PolarCoordinate pt;
private CartesianCoordinate ct;
private boolean compare;
private double s;
/**
* Constructor for objects of class Velocity
*/
public Velocity(CartesianCoordinate c)
{
x = c.getX();
y = c.getY();
}

public Velocity(Velocity z)
{

}

public Velocity(PolarCoordinate p)
{
ct = new CartesianCoordinate(x, y);
pt = new PolarCoordinate(azimuth, length);
azimuth = p.getAzimuth();
length = p.getLength();
}

private Velocity sumOfPolar(double s)
{
this.s = s;
return null;
}

private Velocity PolarCoordinate(double azimuth, double length)
{
return null;
}

private Velocity CartesianCoordinate(double x, double y)
{
return null;
}

private Velocity dotProduct(double prod)
{
return null;
}

//private boolean compare(java.lang.Object velocity,java.lang.Object ct)
//{
// if (velocity.getClass().equals(ct.getClass())) {
// return true;
// } else {
// return false;
// }
//}

/**
* This method performs a vector addition
* and returns a new object representing the
* sum of two vectors.
*
*/
public Velocity add(final Velocity velocity)
{

if(velocity.getClass().equals(ct.getClass()))
{
double sumX = x + velocity.x;
double sumY = y + velocity.y;
Velocity v = new Velocity(CartesianCoordinate(x,y));
v.x = sumX;
v.y = sumY;
System.out.println("BLYABLYA");
return v;
}

if(compare == false)
{
System.out.println("YEAAAAA");
Velocity v = new Velocity(PolarCoordinate(azimuth, length));

double xFirst = length * Math.cos(azimuth);
System.out.println("xFirst: " + xFirst);
double xSecond = velocity.length * Math.cos(velocity.azimuth);
System.out.println("xSecond: " + xSecond);
double yFirst = length * Math.sin(azimuth);
System.out.println("yFirst: " + yFirst);
double ySecond = velocity.length * Math.sin(velocity.azimuth);
System.out.println("ySecond: " + ySecond);
double sumX = xFirst + xSecond;
System.out.println("sumX: " + sumX);
double sumY = yFirst + ySecond;
System.out.println("sumY: " + sumY);
double sumXY = sumX + sumY;
System.out.println("sumXY: " + sumXY);

Velocity sum = new Velocity(sumOfPolar(sumXY));
sum.s = sumXY;
return sum;

}
return null;
}

/**
* This method performs a vector subtraction
* and returns a new object representing the
* sub of two vectors.
*
*/
public Velocity subtarct(final Velocity velocity)
{
double subX = x - velocity.x;
double subY = y - velocity.y;
Velocity v = new Velocity(CartesianCoordinate(x,y));
v.x = subX;
v.y = subY;
return v;
}

/**
* This method performs a vector dot product
* and returns a new object representing the
* product of two vectors.
*
*/
public Velocity dotProduct(final Velocity velocity)
{
double prodX = x * velocity.x;
double prodY = y * velocity.y;
double sumProdXY = prodX + prodY;
Velocity v = new Velocity(dotProduct(prod));
v.prod = sumProdXY;
return v;
}

/**
* This method performs a scaling on a vector.
*
*/
public Velocity scale(final double scaleFactor)
{
double prodX = x * scaleFactor;
double prodY = y * scaleFactor;
Velocity v = new Velocity(CartesianCoordinate(x, y));
v.x = prodX;
v.y = prodY;
return v;
}

public double getAzimuth()
{
PolarCoordinate p = new PolarCoordinate(azimuth,length);
return p.getAzimuth();
}

public double getLength()
{
PolarCoordinate p = new PolarCoordinate(azimuth,length);
return p.getLength();
}
}

最佳答案

您可以使用instanceof来确定对象类型。

if (velocity instanceof PolarCoordinate) {
return true;
} else {
return false;
}

关于java - 如何检查对象类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15598091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com