Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

java - How to manage variables for multiple objects

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];
}
}    

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).

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

How can i do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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

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;

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.

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: try to get a Java Object-Oriented Programming book and start reading, you will improve your level in a short period of time.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...