简体   繁体   English

Java,如何创建类似于Math的类

[英]Java, how to create a class similar to Math

I am trying to write a porting of glm in java. 我试图在Java中编写glm的移植。

At the moment I created a package jglm and inside this package there are all the different classes Mat4, Vec4, Vec3, etc.. 目前,我创建了一个包jglm,并且在该包内有所有不同的类Mat4,Vec4,Vec3等。

Such as: 如:

public class Mat {

    protected float[] matrix;
    protected int order;
}

I can call them by typing 我可以打给他们

jgml.Mat4 modelviewMatrix = new Jgml.Mat4(1.0f);

And this is fine.. 这很好。

Now I am writing also some methods, like 现在我也在写一些方法,例如

mix(x, y, lerp)

And I also would like to use this as stated, that is 我也想使用这个,即

float value = jglm.mix(x, y, lerp)

But of course in this case Jglm must be a class... 但是当然在这种情况下,Jglm必须是一个类...

Is there a way to conjugate the two things together? 有没有办法将这两件事结合在一起?

Edit: If I create a class jglm.Jglm 编辑:如果我创建一个类jglm.Jglm

package jglm;

/**
 *
 * @author gbarbieri
 */
public class Jglm {

    public class Mat {

        protected float[] matrix;
        protected int order;
    }

    public class Mat4 extends Mat {

        public Vec4 c0;
        public Vec4 c1;
        public Vec4 c2;
        public Vec4 c3;

        public Mat4(float value) {

            order = 4;

            matrix = new float[16];

            for (int i = 0; i < 4; i++) {
                matrix[i * 5] = value;
            }
            c0 = new Vec4(matrix, 0);
            c1 = new Vec4(matrix, 4);
            c2 = new Vec4(matrix, 8);
            c3 = new Vec4(matrix, 12);
        }

        public float[] toFloatArray() {
            return new float[]{
                c0.x, c0.y, c0.z, c0.w,
                c1.x, c1.y, c1.z, c1.w,
                c2.x, c2.y, c2.z, c2.w,
                c3.x, c3.y, c3.z, c3.w,};
        }
    }

    public static float mix(float start, float end, float lerp) {
        return (start + lerp * (end - start));
    }
}

When I try to instantiate 当我尝试实例化时

cameraToClipMatrix_mat4 = new Jglm.Mat4(1.0f);

I get " an enclosing instance that contains Jglm.Mat4 is required " 我得到“ 包含Jglm.Mat4是必需的一个封闭实例

Declare all your methods in Mat class as static Mat类中的所有方法声明为static

which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class. 在声明中具有static修饰符的元素,应使用类名调用,而无需创建类的实例。

Ex: ClassName.methodName(args) 例如: ClassName.methodName(args)

See Class Methods in http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html 请参见http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html中的 类方法

At the moment I created a package Jglm and inside this package there are all the different classes Mat4, Vec4, Vec3, etc.. 目前,我创建了一个包Jglm,并且在该包内有所有不同的类Mat4,Vec4,Vec3等。

In Java, packages must be named in all lowercase. 在Java中,软件包必须全部用小写字母命名。 Naming it the way you did will surprise anyone who looks at your code and may also cause serious name-resolution issues for the compiler. 以这种方式命名将使任何查看您的代码的人感到惊讶,并且还可能导致编译器出现严重的名称解析问题。

It is also customary to import all classes we are using so we refer to them just by their simple name in code. 习惯上导入所有我们正在使用的类,因此我们仅通过它们在代码中的简单名称来引用它们。 Perhaps you could consider declaring 也许您可以考虑声明

package org.example.jglm;

public class Jglm {
  public static void mix(double x, double y, Lerp lerp) {
    ...
  }
}

on the client side, you'd write 在客户端,你会写

import org.example.Jglm;

void someMethod() {
  Jglm.mix(x,y,lerp);
}

In general, when you need some pure functions in your code, then declare them as static methods. 通常,当您在代码中需要一些纯函数时,然后将它们声明为static方法。 Look at java.lang.Math source code for guidance. 查看java.lang.Math源代码以获取指导。

将Jglm类中的所有方法设置为静态将通过实例和类名启用方法调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM