0

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

我试图对两个向量求和,它可以是笛卡尔坐标(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();
    }
}
4

2 回答 2

2

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

if (velocity instanceof PolarCoordinate) {
       return true;
} else {
       return false;
}
于 2013-03-24T11:53:43.377 回答
1

使用instanceof.

String s = "TGTGGQCC";
System.out.println(s instanceof String); // true
于 2013-03-24T11:55:03.913 回答