简体   繁体   English

UML图到Java代码

[英]UML diagram to Java code

I'm new to Java and am trying to work through some questions where I have to convert a UML diagram to Java code: I have an image of the uml document- 我是Java的新手,我正在努力解决一些我必须将UML图转换为Java代码的问题:我有一个uml文档的图像 -

http://s1079.photobucket.com/albums/w513/user20121/?action=view&current=uml.jpg http://s1079.photobucket.com/albums/w513/user20121/?action=view&current=uml.jpg

I'll show you what I have so far: 我会告诉你到目前为止我有什么:

Q1: Write a Java version of class Entry assuming it has this constructor: public Entry(String name) and that the method getSize is abstract. Q1:编写一个Java版本的类,假设它有这个构造函数:public Entry(String name),方法getSize是抽象的。 A: A:

public abstract class Entry {
    private String name;

    public Entry(String name){
        this.name =  name;
    }
    public String getName()
    {
        return name;
    }
    abstract long getSize();
}

Q2: Write a Java version of class File assuming it has this constructor: public File(String name, long size) A: Q2:编写Java版本的类文件,假设它有这个构造函数:public File(String name,long size)A:

public class File extends Entry {
    private long size;

    public File(String name, long size){
        super(name);
        this.size = size;
    }

    public long getSize(){
        return size;
    }
}

Q3: A directory contains a collection of files and directories. 问题3:目录包含文件和目录的集合。 Write a Java version of class Directory assuming it has this constructor: public Directory(String name) and the method getSize returns the total size of all the files in the directory and all its sub-directories (in this model the size of a directory itself is ignored). 编写类Directory的Java版本,假设它具有以下构造函数:public Directory(String name)和方法getSize返回目录及其所有子目录中的所有文件的总大小(在此模型中是目录本身的大小)被忽略了)。

A: This is where I get stuck, I don't know what to do about the getSize method. 答:这是我卡住的地方,我不知道如何处理getSize方法。 Can anyone tell me whether what I have done so far is correct? 谁能告诉我到目前为止我所做的是否正确? And also point me in the right direction for Q3? 还指出我在Q3的正确方向?

Edit: okay I have attempted an answer but I really I don't know what I'm doing.. 编辑:好吧我试过一个答案,但我真的不知道我在做什么..

import java.util.ArrayList;

public class Directory extends Entry {

    ArrayList <Entry> entries = new ArrayList<Entry>();

    public Directory(String name)
    {
        super(name);
    }

    public long getSize(){
        long size;
        for(int i=0;i<entries.size();i++)
        {
        size +=  //don't know what to put here?
        }
        return size;
    }
}

Your answers for Q1 and Q2 are looking fine. 你对Q1和Q2的回答看起来很好。

Regarding Q3: 关于第三季度:

// A Directory is an Entry and can contain an arbitrary number of other Entry objects
public class Directory extends Entry {

    // you need a collection of Entry objects, like ArrayList<Entry>
    // ArrayList<Entry> entries = ...

    function getSize() {
        long size;
        // now we calculate the sum of all sizes
        // we do not care if the entries are directories or files
        // the respective getSize() methods will automatically do the "right thing"
        // therefore: you iterate through each entry and call the getSize() method
        // all sizes are summed up
        return size;
    }

}

You can try the following 30 days evaluation build: http://www.uml2.org/eclipse-java-galileo-SR2-win32_eclipseUML2.2_package_may2010.zip 您可以尝试以下30天的评估版本: http//www.uml2.org/eclipse-java-galileo-SR2-win32_eclipseUML2.2_package_may2010.zip

Just unzip and it works. 只需解压缩即可。 You code your java and get your UML class diagram live at the same time. 您可以编写Java代码并同时获取UML类图。 Easy to use and very efficient. 易于使用且非常高效。

As pointed out first two items are correct. 正如前面指出的那样,前两项是正确的。

Here's how i would implement your Directory class. 以下是我将如何实现您的Directory类。

import java.util.ArrayList;

public class Directory extends Entry{


ArrayList <Entry> entries = new ArrayList<Entry>();

public Directory(String name)
{
    super(name);
}

public long getSize(){
    long size = 0;
    for(int i=0;i<entries.size();i++)
    {
    //Probably something like this
    size += entries.get(i).getSize();
    }
    return size;
}
}

If your Directory contains other directories, the getSize of the child directory will be called to get its Size. 如果您的目录包含其他目录,则将调用子目录的getSize以获取其大小。

You will have to add a control that you can't add a directory to itself, or you will have a infinite loop :) 您将不得不添加一个控件,您无法将目录添加到自身,或者您将有一个无限循环:)

An other option is 另一种选择是

public long getSize(){
    long size = 0;
    for( Entry e : entries)
    {
    //Probably something like this
    size += e.getSize();
    }
    return size;
}

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

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