问题描述
我有3个班级处理数据成员:
保存
记忆
编号
每个人都有Ctor,二传手,吸气剂以及他们需要的各种其他东西。
和数据成员。
我有一个类:
计算器
必须对上述类中存储的变量进行操作。
我不想在Calculator中创建这些类的对象,因为我已经从main()创建了这样的对象!!!!
那么我该如何访问它们?
我尝试将功能更改为静态,
例如:
public static double 添加(){
return Number.getNumber()+ Save.getSave();
}
(在计算器中,这是一个抽象类...)
那个我将如何在抽象类中访问静态方法,但它不会让我,因为它们处理数据成员 - 我甚至无法将方法更改为静态而不会出现错误...
如果我要从类Number创建一个新对象,它将拥有自己的数据成员,而我想访问由创建的对象中的数据成员main()!!!
我可以检索一个引用并以某种方式使用它吗?
感谢您的帮助。
I have 3 classes handling data members:
Save
Memory
Number
each with a Ctor, setters, getters, and various other things they require.
And data members.
I have a class:
Calculator
which has to operate on the variables stored in the aforementioned classes.
I don''t want to create objects from those classes in Calculator, since I already created such objects from main()!!!!
So how can I access them?
I tried changing the functions to static,
for instance:
public static double Add() { return Number.getNumber() + Save.getSave(); }
(in Calculator, which is an abstract class...)
That would be how I''d access static methods in abstract classes, but but it wouldn''t let me, since they handle data members - I couldn''t even change the methods to static without getting an error...
If I were to create a new object from, say, class Number, it would have its own data members, whereas I want to access the data members in the object created by main()!!!
Can I retrieve a reference and use it somehow, or something like that?
Thanks for helping.
这篇关于访问另一个非静态类中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!