为什么这个java类不打印该对象?

时间:2016-04-06 03:49:27

标签: java class oop object

这是我的第一堂课右三角

public class rightTriangle {
    private double sideA;
    private double sideB;

    public double getSideA() {
        return sideA;
    }

    public void changeSideA(double sideA) {
        if (sideA > 0)
            sideA = 4;
    }

    public void changeSideB(double sideB) {
        if (sideB > 0)
            sideB = 9;
    }

    public double getSideB() {
        return sideB;
    }

    public rightTriangle(double a, double b) {
        sideA = a;
        sideB = b;
    }

    public rightTriangle() {
        sideA=0;
        sideB=0;
    }

//START METHODS

    public double getHypotenuse(double sideA, double sideB) {
        double sideASquared, sideBSquared, discriminant, hypotenuse;

        sideASquared = sideA * sideA;
        sideBSquared = sideB * sideB;

        discriminant = sideASquared + sideBSquared;

        hypotenuse = Math.sqrt(discriminant);
        return hypotenuse;
    }

    public double getAngleA(double sideB, double hypotenuse){
        double angleA = Math.asin(sideB/hypotenuse);
        Math.toDegrees(angleA);
        return angleA;
    }

    public double getAngleB(double sideA, double hypotenuse){
        double angleB = Math.asin(sideA/hypotenuse);
        Math.toDegrees(angleB);
        return angleB;
    }

    public double getPerimeter(double sideA, double sideB) {
        double perimeter = (sideA + sideB + getHypotenuse(sideA,sideB));
        return perimeter;
    }

    public double getArea(double sideA, double sideB) {
        double area = (sideA * sideB * .5);
        return area;
    }

    public String toString() {
        //String stringA, stringB, stringC, stringPer, stringArea;
        return ("Side A is " + sideA + "." + " Side B is " + sideB + "." +
                " Side C is " + getHypotenuse(sideA,sideB) + "." + "\n" +
                "The perimeter is " + getPerimeter(sideA,sideB) + "." +
                "The area is " + getArea(sideA, sideB) + ".");   
    }    
}

然后这是我的用户类,它在类中创建对象。

public class user {
    rightTriangle triangle1 = new rightTriangle(5,20);
    rightTriangle triangle2 = new rightTriangle(3,4);
    rightTriangle triangle3 = new rightTriangle();
    rightTriangle triangle4 = new rightTriangle(16.3,4.889);

    public void main(String[] args) {
        System.out.println(triangle2);
    }
}

但是,当我在控制台中打印一个三角形对象时,可以说triangle2没有打印出来。为什么呢?

更新

通过将我的对象创建放在用户类的main方法下来解决这个问题。

最终看起来像这样:

public class user {
    public static void main(String[] args) {

    rightTriangle triangle1 = new rightTriangle(5,20);
    rightTriangle triangle2 = new rightTriangle(3,4);
    rightTriangle triangle3 = new rightTriangle();
    rightTriangle triangle4 = new rightTriangle(16.3,4.889);


        System.out.println(triangle2);
    }
}

5 个答案:

答案 0 :(得分:4)

由于无法引用非静态字段,因此无法执行此操作,且此代码未运行也未编译:

public class user {
    rightTriangle triangle1 = new rightTriangle(5,20);
    ...
    public void main(String[] args) {
        System.out.println(triangle1);
    }
}

通过在main方法中移动对象来修复它

实施例

  public class user {
        public void main(String[] args) {
            rightTriangle triangle1 = new rightTriangle(5,20);
            System.out.println(triangle1);
        }
    }

答案 1 :(得分:1)

静态方法不能使用非静态数据成员或直接调用非静态方法

您必须将实例变量设为静态或将它们放在主方法

    public class user {
    static rightTriangle triangle1 = new rightTriangle(5,20);
    static rightTriangle triangle2 = new rightTriangle(3,4);
    static rightTriangle triangle3 = new rightTriangle();
    static rightTriangle triangle4 = new rightTriangle(16.3,4.889);

    public static void main(String[] args) {
        System.out.println(triangle2);
    }
}

    public class user {

    public static void main(String[] args) {
        rightTriangle triangle1 = new rightTriangle(5,20);
        rightTriangle triangle2 = new rightTriangle(3,4);
        rightTriangle triangle3 = new rightTriangle();
        rightTriangle triangle4 = new rightTriangle(16.3,4.889);
        System.out.println(triangle2);
    }
}

答案 2 :(得分:0)

你真的不需要用户类

public static void main(String[] args) {
    rightTriangle triangle1 = new rightTriangle(5,20);
    rightTriangle triangle2 = new rightTriangle(3,4);
    rightTriangle triangle3 = new rightTriangle();
    rightTriangle triangle4 = new rightTriangle(16.3,4.889);

    System.out.println(triangle2);
}

NB System.out.println(object)表示将执行对象上定义的toString()方法(ovveride),否则将执行默认的toString()方法:

getClass().getName() + '@' + Integer.toHexString(hashCode())

就像rightTriangle@15db9742 因此,请注意并记住覆盖toString方法,否则可以使用getter打印单个值

最后,根据Java代码约定,使用大写字母命名类:

public class RightTriangle { ... }

所以:

RightTriangle rightTriangle1 = new RightTriangle();

答案 3 :(得分:0)

使用此更改您的用户类

public class user {
    static rightTriangle triangle1 = new rightTriangle(5,20);
    static rightTriangle triangle2 = new rightTriangle(3,4);
    static rightTriangle triangle3 = new rightTriangle();
    static rightTriangle triangle4 = new rightTriangle(16.3,4.889);

    public static void main(String[] args) {


        System.out.println(triangle2);
    }


}

在main方法中添加static并使用静态变量traingle 2

答案 4 :(得分:0)

你可以覆盖RightTriangle类中的toString方法,它将在System.out.println(triangle2);之后调用