简体   繁体   中英

How to access a variable from a constructor of a different class in java?

So I have 3 classes and I want to access one variable from two of them to get them summed up in the third one. Here is the code:

import java.util.*;

public class Car{
    String tip,marka;
    public double sborMestaCar = 0;
    double mesta, skorost;


public Car(String tip, String marka, double mesta, double skorost){
    Scanner sc = new Scanner(System.in);

    System.out.println("Vavedete tiput na kolata.");
    tip = sc.nextLine();

    System.out.println("Vavedete markata.");
    marka = sc.nextLine();

    System.out.println("Vavedete mestata.");
    mesta = sc.nextDouble();

    System.out.println("Vavedete max. skorostta.");
    skorost = sc.nextDouble();

    System.out.println("Suzdadenata kola e ot tip " + tip + ", markata e " + marka + ", ima " + mesta + " mesta i maksimalnata i skorost e " + skorost);

    sborMestaCar = sborMestaCar + mesta;
}
public Car(double sborMesta){
    sborMestaCar = sborMesta;

}

}

2nd one

import java.util.*;

public class Plane {
    String tip,marka;
    double mesta1k, mesta2k, skorost;
    public int sborMestaSam = 0;


public Plane( String tip, String marka, double mesta1k, double mesta2k, double skorost ){
    Scanner sc = new Scanner(System.in);


    System.out.println("Vavedete tiput na samoleta.");
    tip = sc.nextLine();

    System.out.println("Vavedete markata na samoleta.");
    marka = sc.nextLine();

    System.out.println("Vavedete mestata za purva klasa.");
    mesta1k = sc.nextDouble();

    System.out.println("Vavedete mestata za vtora klasa.");
    mesta2k = sc.nextDouble();

    System.out.println("Vavedete max. skorostta.");
    skorost = sc.nextDouble();

    System.out.println("Suzdadeniqt samolet e ot tip " + tip + ", markata e " + marka + ", ima " + mesta1k + " mesta v purva klasa, " + mesta2k +" mesta vuv vtora klasa" + " i maksimalnata i skorost e " + skorost);
}
    public Plane(double sborMesta){
        sborMestaSam = sborMesta;

}

}

And 3rd one.

import java.util.*;

public class TestPlane {
    public static void main(String[] args){

        String marka = "", tip = "";
        int mesta = 0, mesta1k = 0, mesta2k = 0, skorost = 0;
        int b = 1, c = 1;
        double sborMestaK = 0, sborMestaS = 0, obshtSbor = 0;



        Scanner sc = new Scanner(System.in);

        while(b == 1){
            Car car = new Car(tip, marka, mesta, skorost);
            System.out.println("Iskate li da suzdadete druga kola? 1 = Da 0 = Ne");
            b = sc.nextInt();
        }

        System.out.println("Vie zavurshihte suzdavaneto na kolite. Preminavane kum suzdavane na samoleti...");

        while(c == 1){  
            Plane plane = new Plane(tip, marka, mesta1k, mesta2k, skorost);
            System.out.println("Iskate li da suzdadete drug samolet? 1 = Da 0 = Ne");
            c = sc.nextInt();
        //  sborMesta = 
        }
        Car obshtiMestaK = new Car(sborMestaK);
        Plane obshtiMestaS = new Plane(sborMestaS);
        obshtSbor = obshtiMestaK.sborMestaCar + obshtiMestaS.sborMestaSam;
        System.out.println("Sbora ot mestata na vsi4ki sazdadeni prevozni sredstva e raven na " + obshtSbor);
    }

    }

You can look at the 3rd class at the end in the System.out.println part you can see that I have obshtSbor - the variable which is consisted from obshtiMestaK.sborMestaCar + obshtiMestaS.sborMestaSam. The program works BUT returns 0 as value for obshtSbor.

So first of all you are using wrong assignment, trying to change argument value to that of the field for both Car and Plane. Should rather be sborMestaCar = sborMesta and sborMestaSam = sborMesta.

Secondly, you are using 0 as an argument to both Car and Plane constructors double sborMestaK = 0, sborMestaS = 0, obshtSbor = 0; so 0+0=0. What did you expect to see?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM