微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何访问另一个类的对象

如何解决如何访问另一个类的对象

所以我在一个主程序中得到了这个。如下所示,我首先尝试通过将单个 Employee 对象提高 10% 来编辑薪酬“45.0”。在这里,我确保我的数学计算是正确的。在另一个类中,我需要编写一个代码,将 10% 添加到所有员工中。

temp.obj.getData() 显示主类中输入的所有员工数据,而新对象 e 为空。有人知道如何将数据从主程序获取到对象 e 或不创建对象 e。我的问题是,当我对对象 e 执行相同的操作时,薪酬的计算都是 0。我认为这是由于 pay = 0 。我似乎在从主类中的对象获取数据(支付值)时遇到问题。有人可以帮忙吗?谢谢如果需要更多代码问我。

主要程序:

            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");
            }

其他类中的方法

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!");
} 

解决方法

您必须使用 this 方法将您自己的实例作为参数传递。如果我理解正确,您需要在另一个类中使用 etemp 数组,该类具有 pay 方法。如果是这样,您就不必创建更多对象。

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");
            }

其他类中的方法:

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!");
} 

告诉我有没有帮到你。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?