[英]How to get an attribute/variable from a different class
I'm programming in a class and I need to have a variable from a different class. 我在一个类中编程,所以我需要一个来自其他类的变量。 How can I do this? 我怎样才能做到这一点?
package domein;
public class Speler
{
private String naam;
private String kleur;
private Sector sector;
private int Sector;
private int krediet = 10;
private int extraSchattingWaarde = 0;
private int nummer;
public Speler(String naam, String kleur, Sector sector)
{
setNaam(naam);
setKleur(kleur);
setSector(sector);
}
public String getNaam()
{
return this.naam;
}
public void setNaam(String naam)
{
//controle of het leeg is??
this.naam = naam;
}
public Sector getSector()
{
return this.sector;
}
private void setSector(Sector sector)
{
//tussen 1 en 4
this.sector = sector;
}
public String getKleur()
{
return this.kleur;
}
private void setKleur(String kleur)
{
//controle of het de beschikbare kleuren zijn
this.kleur = kleur;
}
public int getKrediet()
{
return this.krediet;
}
public void setKrediet(int krediet)
{
this.krediet = krediet;
}
public int getExtraSchattingWaarde()
{
return this.extraSchattingWaarde;
}
public void setExtraSchattingWaarde(int waarde)
{
this.extraSchattingWaarde = waarde;
}
} }
This is the class where I need to get the variables and some methods. 这是我需要获取变量和一些方法的类。 How can I make this class global? 我如何才能使该课程成为全球性的课程?
Just a thing : this line is wrong private int Sector;
只是一件事:这行是不正确的private int Sector;
because you can not use a class name as your variable name. 因为您不能使用类名作为变量名。 This should hide your class visibility. 这应该隐藏您的班级可见性。 I assume this is an error and I continue the explanation. 我认为这是一个错误,我将继续进行解释。
In an other class you can instanciate this class and call the values. 在另一个类中,您可以实例化该类并调用值。 For example: 例如:
public class MyClass {
private Speler mySpeler = new Speler("AAA", "BBB", 3);
public MyClass() {}
public void myMethod() {
System.out.println(mySpeler.getKleur());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.