Reputation: 7
I have to create two classes, let's call them A and B .
public class A {
double Number1;
double Number2;
A(double Number1, Number2){
this.Number1=Number1;
this.Number2=Number2;
}
}
Now i have a void main class where someone enter their Number 1 and Number 2
public static void main(String[] args) {
A game=new A(555, 999);
}
Now, i Want to Create another Class Called B, and i want the Numbers 555, and 999 to be transferd/to use the same values into class B like
public class B{
///double a= 555;
///double c= 777;
}
I need these operators a, and c to fetch data from public static void main class Can someone explain me how to do this? Thanks.
Upvotes: 1
Views: 6913
Reputation: 623
You need getter methods in you classes to query the values. In your case for the member variables Number1 and Number2.
Here is an example for you, consider the change of upper-/lowercase names:
public class A {
// value of number 1
// member variables should start lowercase
// they should be private so they are not modified from the outside
private double number1;
// value of number 2
private double number2;
// constructor:
public A(double number1, double number2) {
// in a custom constructor you should
// alwas run the superconstructor at first:
super();
setNumber1(number1);
setNumber2(number2);
}
// setter/getter for number 1:
// search the internet for "camelcase" description
// (lower/upper cases in method/member names)
public void setNumber1(double theNumber) {
this.number1 = theNumber;
}
public void getNumber1() {
return this.number1;
}
// do the same for number 2
}
Now, you can use these getters to retrieve the data. Build class B the same way and you go like this:
public static void main (String[] args) {
A a = new A(555, 666);
B b = new B(a.getNumber1(), a.getNumber2());
}
Useless to say that in this example class A and class B don't differ, so class B is nonsense. But if your classes are going have different signatures (member variables, methods and so an), this is a proper way.
Upvotes: 0
Reputation: 1772
One basic principal of OOP is that we have certain objects with certain properties build upon classes. These classes 'act as blueprints' for objects which should use their behaviour.
As I mentioned in a comment you could create a constructor who takes an object of class A
as a parameter:
public class B
{
private double value1;
private double value2;
public B( A aObject )
{
value1 = aObject.getVal1();
value2 = aObject.getVal2();
}
// rest of the class...
}
with a class A:
public class A
{
private double val1;
private double val2;
public A( double val1, double val2 )
{
this.val1 = val1;
this.val2 = val2;
}
public double getVal1()
{
return val1;
}
public double getVal2()
{
return val2;
}
}
and a main method:
public static void main( String[] args )
{
A aObject = new A( 100, 100 );
B bObject = new B( aObject );
}
Doing so would alow objects of class A
to exist on their own but objects of class B
always need a 'foundation' in form of another object from which they can copy their values.
Also take a small read on encapsulation
Upvotes: 1
Reputation: 137
You can extend A class on B. The code bellow will help you out.
public class ClassA {
double number1;
double number2;
public ClassA(double number1, double number2) {
this.number1 = number1;
this.number2 = number2;
}
}
public class ClassB extends ClassA {
public ClassB(double number1, double number2) {
super(number1, number2);
}
public double getNumber1() {
return number1;
}
public double getNumber2() {
return number2;
}
}
If you access the classB.getNumber1() method you will get your results.
Upvotes: 0
Reputation: 4266
One way to do this is to add some get methods into A
:
public double getNumber1() {
return Number1;
}
public double getNumber2() {
return Number2;
}
And add set methods into B
:
public void setA(double a) {
this.a = a;
}
public void setC(double c) {
this.c = c;
}
Then instantiate B
in your main class:
B b = new B();
And set the values then:
b.setA(game.getNumber1());
b.setC(game.getNumber2());
Now the values of a
and c
of your instance of B
are 555
and 999
.
Or perhaps:
public B(double a, double c) {
this.a = a;
this.c = c;
}
and pass the values into the constructor:
B b = new B(game.getNumber1(), game.getNumber2());
Upvotes: 1