Java 在另一个类中调用一个类的一个变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4073039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling one variable of a class in another class?
提问by MuraliN
Hi I am new to the java programming. I have a instance variable in a class which I should call to another class.It should not be static as per the requirements.The code given below## `
嗨,我是 Java 编程的新手。我在一个类中有一个实例变量,我应该调用另一个类。根据要求它不应该是静态的。下面给出的代码## `
public class Card {
private String no;
private String text;
public Vector totalCards = new Vector();
public String getNo() {
totalCards.addElement(no);
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getText() {
totalCards.addElement(text);
return text;
}
public void setText(String text) {
this.text = text;
}
}
I need to pass this "totalCards" vector in another class without making it as a static.How can I pass this value.Can anybody help me. Any suggestions appreciated.
我需要在另一个类中传递这个“totalCards”向量,而不是将其设为静态。我该如何传递这个值。有人可以帮助我。任何建议表示赞赏。
采纳答案by Michael McGowan
It's a bit unclear exactly what your issue is, but you first need to have an instance of Card. The totalCards Vector will then live in that Card object.
有点不清楚您的问题究竟是什么,但您首先需要有一个 Card 实例。totalCards Vector 将存在于该 Card 对象中。
Card myCards = new Card();
Now the object that has access to myCards can access the Vector with:
现在可以访问 myCards 的对象可以通过以下方式访问 Vector:
myCards.totalCards
However, it's considered a better practice by many to make totalCards private and make a getter for it:
但是,许多人认为将 totalCards 设为私有并为其创建 getter 是一种更好的做法:
myCards.getTotalCards();
回答by pyCoder
You can simply pass totalCards reference to other class because is public. Tell us more about client class. Thanks.
您可以简单地将 totalCards 引用传递给其他类,因为它是公共的。告诉我们更多关于客户端类的信息。谢谢。
回答by zerodin
Since the variable "totalCards" is public, it can be directly accessed via an instance of Card.
由于变量“totalCards”是公开的,因此可以通过 Card 的实例直接访问它。
回答by Amir Afghani
public class Card {
private String no;
private String text;
/* initializing totalCards here is bad, why are you doing this here? If each
card has a list of totalCards, consider doing this in the constructor of a
Card */
private Vector<Card> totalCards = new Vector();
public String getNo() {
//getters should not have side effects like addElement...
totalCards.addElement(no);
return no;
}
public Vector<Card> getCards() {
return totalCards;
}
public void setNo(String no) {
this.no = no;
}
public String getText() {
//getters should not have side effects like addElement...
totalCards.addElement(text);
return text;
}
public void setText(String text) {
this.text = text;
}
}
回答by thejh
The other class needs to have an instance of Card
. For example by creating a new instance:
另一个类需要有一个Card
. 例如通过创建一个新实例:
public class TheOtherClass {
private Card myCard = new Card();
public void doSomething() {
myCard.totalCards.doAnotherThing();
}
}
By the way: It's considered as bad style to access properties of other classes directly - try to use setters and getters:
顺便说一句:直接访问其他类的属性被认为是不好的风格 - 尝试使用 setter 和 getter:
public class Card {
private Vector<Card> totalCards = new Vector();
public void getTotalCards() {
return totalCards;
}
}
回答by yozhik
You simply write in your class:
你只需在你的课堂上写:
public class AnotherClass
{
public Class obj1 = new Class();
public String getNo()
{
Vector v1 = obj1.totalCards;
return v1; //or what do you want
}