开发者

Make a model for grouping multiple value in java

This is 开发者_Go百科surely obvious but I'm stuck on it.

I want to have something like that

group1 {
name="boo";
gender="male";
}
...
groupN {
name="baa";
gender="female";
}

all the group have the same member. and when i make function

function(Group group) {
blabla = group.name;
bloblo = group.gender;
}

I know how to do it but it's not pretty at all , and I'm sure there is a way to do it cleanly


Java is not designed to be pretty or a functional language. It is fairly likely that the simplest way/most natural way to do what you want is to use a plain loop without a "function"

Perhaps if you can be more specific we can help you more specifically.


all the group have the same member

You're describing classes. This is the exact Java equivalend of your code:

public class Group{
    public String name;
    public String gender;

    public Group(String name, String gender){
        this.name=name;
        this.gender=gender;
    }

    public static void main(String[] args){
        Group group1 = new Group("boo", "male");
        ...
        Group groupN = new Group("baa", "female");
    }

    public static void function(Group group){
        String blabla = group.name;
        String bloblo = group.gender;
    }
}

I suggest getting an introductory Java book and reading the chapters on OOP. The Java tutorial chapter on OOP works as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜