需要在Java中将两个对象添加到一起,这两个对象由Feet&;英寸

需要在Java中将两个对象添加到一起,这两个对象由Feet&;英寸,java,object,Java,Object,我有一个问题,我想不起来。也许有人能帮我 我创建了一个包含两个变量ft和INCHES的DISTANCE类。我需要向该类添加一个方法,该方法添加两个独立的英尺和英寸距离对象 这是我目前的课程: public class Distance { static int feet; // 0 - infinity static int inches; // 0 - infinity public Distance() { // default constructor

我有一个问题,我想不起来。也许有人能帮我

我创建了一个包含两个变量ft和INCHES的DISTANCE类。我需要向该类添加一个方法,该方法添加两个独立的英尺和英寸距离对象

这是我目前的课程:

public class Distance {

    static int feet; // 0 - infinity
    static int inches; // 0 - infinity

    public Distance() { // default constructor

        this.feet = 0;
        this.inches = 0;
    }

    public Distance(int ft, int in){ // parametarized constructor

        this.feet = ft;
        this.inches = in;

        System.out.println("constructor w/2 ints : " + feet + ", " + inches);
    }

    public void setDistance(int ft, int in){

        setFeet( ft );
        setInches( in );    
    }

    public static int getFeet() {

        return (feet);
    }

    public static void setFeet(int feet) {
        Distance.feet = feet;
    }

    public static int getInches() {
        return inches;
    }

    public static void setInches( int inches) {
        Distance.inches = inches;

    }



    public Distance(Distance d){

        //Distance total = d;

        d.getDistance();

    }

    private int getDistance() {
        int totalInches;

        totalInches = (feet * 12) + inches;

        return totalInches;

    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + feet;
        result = prime * result + inches;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Distance other = (Distance) obj;
        if (feet != other.feet)
            return false;
        if (inches != other.inches)
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Distance [feet=" + feet + ", inches=" + inches + "]";
    }


}
我尝试编写的方法需要添加到距离对象。以下是我到目前为止尝试过的:如果

公共距离添加(距离d){


不要尝试将
int
分配给
距离
对象,而是使用构造函数

public Distance add(Distance d){
    Distance total = new Distance(this.feet + d.feet, this.inches + d.inches);
    return total;
}
此外,您可能不应该将英尺和英寸声明为静态变量,因为无论何时修改它,它都会更改为距离对象的所有。 更改为:

static int feet; // 0 - infinity
static int inches; // 0 - infinity
致:


您正在对象中使用静态变量。每次实例化距离对象时,都会将所有距离对象设置为这些值

距离a=新距离(1,3); 距离b=新距离(3,1)

System.out.println(a.getFoots());
//应该返回3,您可以在代码中执行此操作

public static Distance add(Distance d1,Distance d2){
    return new Distance(d1.getFeet() + d2.getFeet(), d1.getInches() + d2.getInces);
}
您的整个代码应更改为以下内容

public class Distance {

    private int feet;
    private int inches;

    public Distance(int ft, int in){ // parametarized constructor
        this.feet = ft;
        this.inches = in;
        System.out.println("constructor w/2 ints : " + feet + ", " + inches);
    }

    public int getFeet() {

        return feet;
    }

    public int getInches() {
        return inches;
    }

    public int getDistance() {
        return (feet * 12) + inches;
    }

    public static Distance add(Distance d1,Distance d2){
        return new Distance(d1.getFeet() + d2.getFeet(), d1.getInches() + d2.getInces);
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + feet;
        result = prime * result + inches;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Distance other = (Distance) obj;
        if (feet != other.getFeet())
            return false;
        if (inches != other.getInches())
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Distance [feet=" + getFeet() + ", inches=" + getInches() + "]";
    }
}

您也可以这样做:

public class Distance {

    private int feet;
    private int inches;

    public Distance(int feet, int inches) {
        this.feet = feet;
        this.inches = inches;
    }

    public int getDistanceInInches() {
        return feet * 12 + inches;
    }

    public Distance add(Distance distance) {
        return new Distance(this.feet + distance.feet, this.inches  + distance.inches);
    }

    @Override
    public String toString() {
        return "Distance [feet=" + feet + ", inches=" + inches + "]";
    }
}

好的,这有什么问题吗?我建议在内部只使用英寸。以英寸为单位执行算术,然后再转换回英尺和英寸。对
英尺和
英寸使用
静态
成员是100%不正确的。这些成员不能是
静态
。感谢您的帮助,我没有崩溃将对象添加到它的变量中。我也看到了静态变量的问题。注意,通过改变调用对象,您正在破坏其中的值。问题是添加2个对象并将其放入3个对象中。。。
public class Distance {

    private int feet;
    private int inches;

    public Distance(int ft, int in){ // parametarized constructor
        this.feet = ft;
        this.inches = in;
        System.out.println("constructor w/2 ints : " + feet + ", " + inches);
    }

    public int getFeet() {

        return feet;
    }

    public int getInches() {
        return inches;
    }

    public int getDistance() {
        return (feet * 12) + inches;
    }

    public static Distance add(Distance d1,Distance d2){
        return new Distance(d1.getFeet() + d2.getFeet(), d1.getInches() + d2.getInces);
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + feet;
        result = prime * result + inches;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Distance other = (Distance) obj;
        if (feet != other.getFeet())
            return false;
        if (inches != other.getInches())
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Distance [feet=" + getFeet() + ", inches=" + getInches() + "]";
    }
}
public class Distance {

    private int feet;
    private int inches;

    public Distance(int feet, int inches) {
        this.feet = feet;
        this.inches = inches;
    }

    public int getDistanceInInches() {
        return feet * 12 + inches;
    }

    public Distance add(Distance distance) {
        return new Distance(this.feet + distance.feet, this.inches  + distance.inches);
    }

    @Override
    public String toString() {
        return "Distance [feet=" + feet + ", inches=" + inches + "]";
    }
}