跨多个类JAVA访问变量

时间:2018-04-16 00:15:34

标签: java class methods

我是JAVA的新手,正在编写一个程序,它在两个不同的类之间传递一个变量。我似乎无法弄清楚如何将变量的数据从一个类发送到另一个类然后再返回?这样的事情有可能吗?

例如:

Public Class A {
    Scanner input = new Scanner(System.in);
    System.out.println("Please input the weight:");
            bagWeight=input.nextDouble();
    System.out.println("The total weight is: "+total);
}

Public Class B {
    double number = 3;
    public method(double number, double bagWeight) 
    {
        total = bagWeight*number;
        return total;
    }
}

1 个答案:

答案 0 :(得分:0)

当然这是可能的:D有很多方法可以这样做。

例如,您可以轻松地执行此操作:

import React, {Component} from 'react'
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
} from 'react-native';

import createStore from './../../Redux/store';
import {StackNavigator} from 'react-navigation';
import { Provider } from 'react-redux';

import MainView from '../Main/Main';
import MainSecondLvlView from '../Main/MainSecondLvl';
import BalanceView from './../Charts/BalanceView'
import MenuView from './Menu'

const store = createStore();

const RootStack = StackNavigator({
    Main: { screen: MainView },
    MainSecondLvl: { screen: MainSecondLvlView},
    Menu:{ screen: MenuView }
},
{
    initialRouteName: 'Main',
    headerMode: 'none', 
});

export default class App extends Component {

    render(){
        return(
            <Provider store={store}>
                <RootStack navigation={this.props.navigation}/>
            </Provider>
            );
    }
}