如何管理多个对象的变量 [英] How to manage variables for multiple objects

查看:139
本文介绍了如何管理多个对象的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为每个对象分配属性。我不能修改Sigar库的FileSystemUsage类,所以我不得不另外一个类来管理propoerties。

I am trying to assign properties to each object. I cannot modify the FileSystemUsage class of Sigar library, so i had to make another class to manage the propoerties.

我生成对象的函数是以下

The function where i am generating the objects is the following

private String getDiskUsage() {
    try {
        List<FileSystem> result = new ArrayList<FileSystem>();
        org.hyperic.sigar.FileSystem[] fss = null;
        fss = sig.getFileSystemList();
        List<Integer> diskUsage = new ArrayList<Integer>();
        String ioStat = "";
        DiskMembers diskm = new DiskMembers();
        for (int i = 0; i<fss.length; i++) {
            FileSystem fs = fss[i];
            if (fs.getType()==2) {
                FileSystemUsage usage = sig.getFileSystemUsage(fs.getDirName());                    
                if (diskm.getlastDiskCheck(i - 1) != 0) {
                    long diff = (nanoTime() - diskm.getlastDiskCheck(i - 1)) / 1000000;
                    long diffRead = usage.getDiskReadBytes() - diskm.getLastDiskRead(i - 1);
                    long diffWrite = usage.getDiskWriteBytes() - diskm.getlastDiskWrite(i - 1);
                    ioStat = String.format("(%d/%d kB/s)", diffRead / diff, diffWrite / diff);
                }
                diskm.setLastDiskRead(i, usage.getDiskReadBytes());
                diskm.setlastDiskWrite(i, usage.getDiskWriteBytes());
                diskm.setlastDiskCheck(i, nanoTime());
                diskUsage.add((int) (usage.getUsePercent() * 100));
            }
        }
        return String.format("%d%% %s", (Collections.max(diskUsage)), ioStat);

    } catch (SigarException e) {
        e.printStackTrace();
    }
    return "n/a";
}

我为每个对象管理属性的类是以下

The class that i made for managing the properties for each object is the following

class DiskMembers{
private long[] lastDiskCheck = new long[5];
private long[] lastDiskRead = new long[5];
private long[] lastDiskWrite = new long[5];

public DiskMembers(){
    this.lastDiskCheck[0] = 0;
    this.lastDiskRead[0] = 0;
    this.lastDiskWrite[0] = 0;
}
public boolean setLastDiskRead(int i, long value){
    this.lastDiskRead[i] = value;
    return true;
}
public boolean setlastDiskCheck(int i,long value){
    this.lastDiskCheck[i] = value;
    return true;
}
public boolean setlastDiskWrite(int i,long value){
    this.lastDiskWrite[i] = value;
    return true;
}
public long getLastDiskRead(int i){
    if (i==-1)
        return this.lastDiskRead[0];
    return this.lastDiskRead[i];
}
public long getlastDiskCheck(int i){
    if (i==-1)
            return this.lastDiskCheck[0];
    return this.lastDiskCheck[i];
}
public long getlastDiskWrite(int i){
    if (i==-1)
        return this.lastDiskWrite[0];
    return this.lastDiskWrite[i];
}
}    

问题是我无法管理属性为每个对象分隔。我的意思是当对象使用生成时,我通过调用diskm.getlastDiskCheck(i-1)检查lastDickCheck变量。第一次它不会执行这段代码,因为返回值为零(0)。但之后,我将执行它每次即使对于不同的对象,只要他们的数组是。我想为每个对象使用记录每个(磁盘)的lastdiskcheck。

The problem is i can't manage to keep properties separate for each object. I mean when object usage is generated i check the lastDickCheck variable by calling diskm.getlastDiskCheck(i-1). The first time it won't execute this section of code because the return value is zero(0). But after that i will execute it every time even for the different object as long as they array is. I want to keep record of "lastdiskcheck" for each object "usage", for each (disk).

如果我更新磁盘C的lastDiskCheck变量:它也为磁盘D选择,我不是什么。

If i update lastDiskCheck variable for disk C: It picks it up for the disk D as well, which i don't what.

我该如何做?

推荐答案

我会尽力解释你的目标设置和吸气。在你的case,有不同的问题,需要解释。

I will try to explain you what is the goal of the setters and getters. In your case, there are different problems which need to be explained.

在你的第一个函数getDiskUsage,有一个私有方法。您只能从同一个类访问此方法。

In your first function getDiskUsage, there is a private method. You only can access to this method from the same class. I don't know if is your objective.

此外,如果你想访问三个不同的磁盘,我将实例3三个不同的对象,例如:DiskMembers [] disks = new DiskMembers [3]; // C D和E

Moreover, if you want to access three different disks, I would instance 3 three different objects, example: DiskMembers[] disks = new DiskMembers[3]; //C D and E

在这种情况下,对于每个对象,您将访问getters / setter。您的类DiskMembers的属性应该是:

In this case, for each object you will access the getters/setters. The attributes for your class DiskMembers should be:

private boolean _DiskCheck;
private boolean _DiskRead;
private boolean _DiskWrite;

每次修改磁盘时,都可以这样访问:disks [0] .set_DiskCheck )= true;

Each time that you modify the disk, you access of this way: disks[0].set_DiskCheck() = true;

此外,在每个实例中,你会有你想要的一切。要检查最后一个磁盘是什么,您可以添加另一个属性,如日期,其中保留上次修改日期。

In addition, in each instance you would have everything that you want. To check what is the last disk, you can add another attribute like date, where you keep the date of the last modify.

最后,我建议确保setter方法只保留值,不返回东西。在另一方面,你将有getter,返回private属性的值。另一个重要的事情是,如果你需要创建一个复杂的setter,最好创建更多的方法,你可以访问这个setter。

Lastly, I would recommend to ensure that a setter method only keep the value, not return something. In the other side, you will have the getter, that return the value of the private attribute. Another important thing is if you need to create a complex setter, it would be better to create more methods that you can access with this setter.

PD:尝试得到一个Java面向对象编程书,并开始阅读,你将提高你的水平在短时间内。

PD: try to get a Java Object-Oriented Programming book and start reading, you will improve your level in a short period of time.

这篇关于如何管理多个对象的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆