在 Java 中像 C++ 一样创建结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28798152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:12:38  来源:igfitidea点击:

Create a Struct In Java Like C++

javastruct

提问by Arshia Aghaei

i want to create an struct in java , like c++ :

我想在 java 中创建一个结构,比如 c++:

struct MyStruct {
    int x;
};
#include <iostream>
int main() {
    MyStruct Struct;
    Struct.x = 0;
    std::cout << Struct.x;
    return 0;
}

can anyone help me ?

谁能帮我 ?

回答by manan

You can use a class, which functions similarly to a structin C++.

您可以使用类,其功能类似于structC++ 中的 a。

For example, a C++ pointstruct may look like

例如,一个 C++point结构可能看起来像

typedef struct __point {
   int x, y;
} point;

A Java pointclass has the form

Javapoint类具有以下形式

final class Point { 
    private final double x;    // x-coordinate
    private final double y;    // y-coordinate

    // point initialized from parameters
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    // accessor methods
    public double x() { return x; }
    public double y() { return y; }

    // return a string representation of this point
    public String toString() {
        return "(" + x + ", " + y + ")";
    } 

}

We may then call the following:

然后我们可以调用以下内容:

Point q = new Point(0.5, 0.5);
System.out.println("q  = " + q);
System.out.println("x = " + q.x());

回答by manan

public class ircodes {
    public ircodes(String msg_id, String node_id, String frequency, String data) {
        this.hdr = new msg_hdr(4 + data.length(), Integer.parseInt(msg_id), Integer.parseInt(node_id));
        this.frequency = Integer.parseInt(frequency);
        this.data = data;
    }

    public class msg_hdr {
        int msg_len;
        int msg_id;
        int node_id;

        public msg_hdr(int msg_len, int msg_id, int node_id) {
            this.msg_len = 12 + msg_len;
            this.msg_id = msg_id;
            this.node_id = node_id;
        }
    }
    msg_hdr hdr;
    int frequency;
    String data;

    public ByteBuffer serialize() {
        ByteBuffer buf = ByteBuffer.allocate(hdr.msg_len);
        buf.putInt(hdr.msg_len);
        buf.putInt(hdr.msg_id);
        buf.putInt(hdr.node_id);
        buf.putInt(frequency);
        buf.put(data.getBytes());
        return buf;
    }
}

回答by user3437460

Java doesn't have structs like C or C++, but you can use Java classes and treat them like a struct. On top of that, you can of course declare all its members as public. (to work exactly like a struct)

Java 没有structC 或 C++ 之类的 s,但您可以使用 Java 类并将它们视为struct. 最重要的是,您当然可以将其所有成员声明为公共成员。(完全像 a 一样工作struct

class MyClass
{
    public int num;
}

MyClass m = new MyClass();
m.num = 5;
System.out.println(n.num);

One of the differences between a structand a classis that a struct do not have methods. If you create a class without methods, it will work like a struct.

astruct和 a之间的区别class之一是结构没有方法。如果您创建一个没有方法的类,它将像struct.

However, you can always put in methods (getters and setters) and set the variables as private like this (it doesn't hurt).

但是,您始终可以放入方法(getter 和 setter)并将变量设置为私有,就像这样(这不会造成伤害)。

class MyClass
{
    private int num;
    public void setNum(int num){
        this.num = num
    }
    public int getNum(){
        return num
    }
}

MyClass m = new MyClass();
m.setNum(5);
System.out.println(n.getNum());

Java doesn't have structs, but a Class can do exactly the same things a structdoes.

Java 没有structs,但 Class 可以做与 a 完全相同的事情struct