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
4.1k views
in Technique[技术] by (71.8m points)

java - How to access an object of another class

So I got this in a main program. As visible below I am first trying to edit the pay "45.0" by a raise of 10% of a single Employee object. Here I am making sure that my math calculation are correct. In another class I need to write a code to add the 10% to all the Employees.

temp.obj.getData() shows all the Employee data entered in the main class while the new object e is empty. Does someone know how I can get the data from the main program to object e or without creating object e. My problem is that when I am doing the same operations on object e the calculation of the pay are all 0. I think this is due to pay = 0 . I seem to have a problem of getting the data (pay values ) from the objects in main class. Can someone help? Thanks if there is the need of more code ask me.

MAIN PROG:

            etemp[15] = new Employee(134, "Nicole", 45.0); 
            queue.put(etemp[15]);
            if(queue!=null){
                //Attempt 1  : Editing pay of a single employee
                System.out.println("not empty!");
                double x = etemp[15].getSalary();
                double calc = (x*0.10)+x;
                etemp[15].setPay(calc);
                System.out.println("Raise: "+etemp[15].getData());
                //Attempt 2 : Using a method to edit pay of ALL employee
                queue.pay(10);
            } else {
                System.out.println("empty");
            }

METHOD IN OTHER CLASS:

public void pay(int p){   
    int id = 0;
    String k = "";
    double pay = 0;        
    Employee e = new Employee(id, k, pay);
    CircQueue q = new CircQueue(20);
    if(rear!=null){
        Node temp=front;
        do{
            for (int i= 0; i<currNodes; i++)
            {
                System.out.println(temp.obj.getData()); 
                q.put(e);
                q.listAll();
                double x = e.getSalary();
                double calc = (x*0.10)+x;
                System.out.println("Raise: "+calc);
                temp = temp.next;
            }
        }while(!(temp==rear.next));
    } else System.out.println("Empty queue!");
} 

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

1 Reply

0 votes
by (71.8m points)

You have to use thismethod to pass your own instance as an argument. If I understood correctly, you need to use the etemp array in the other class, the one having the pay method. If that is right, you don't have to create more objects.

etemp[15] = new Employee(134, "Nicole", 45.0); 
            queue.put(etemp[15]);
            if(queue!=null){
                //Attempt 1  : Editing pay of a single employee
                System.out.println("not empty!");
                double x = etemp[15].getSalary();
                double calc = (x*0.10)+x;
                etemp[15].setPay(calc);
                System.out.println("Raise: "+etemp[15].getData());
                //Attempt 2 : Using a method to edit pay of ALL employee
                queue.pay(this,10); //Adding our own instance
            } else {
                System.out.println("empty");
            }

METHOD IN OTHER CLASS:

public void pay(MainClass m, int p){ 

    // Here you can use m as the instance, it will have the etemp. Normally, atributes are private, so you have to implement some kind of getters to reach to that array
    int id = 0;
    String k = "";
    double pay = m.getOriginalPay(id); // For example     
    Employee e = new Employee(id, k, pay);
    CircQueue q = new CircQueue(20);
    if(rear!=null){
        Node temp=front;
        do{
            for (int i= 0; i<currNodes; i++)
            {
                System.out.println(temp.obj.getData()); 
                q.put(e);
                q.listAll();
                double x = e.getSalary();
                double calc = (x*0.10)+x;
                System.out.println("Raise: "+calc);
                temp = temp.next;
            }
        }while(!(temp==rear.next));
    } else System.out.println("Empty queue!");
} 

Tell me if I helped you.


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

...