使用另一个类的变量 [英] use variable from another class

查看:105
本文介绍了使用另一个类的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类。我想使用第二个类中的第一个类中的 english french (类Question)。

I have two classes. I want to use english and french from the first class in the second class (class Question). Please help me to fix that code because it shows me an error.

代码块:

package josephtraduire;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileParser implements IParser{

    @Override
    public void parseFile () throws IOException{
        String french="";
        String english="";

        try( BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\User\\Desktop\\text.txt"))){
            String line;        
            while ((line = br.readLine())!=null){
                    String[] pair = line.split(";");
                    french=(pair[0]);
                    english=(pair[1]);
            }
        }        
    }            

}

package josephtraduire;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Scanner;

public class Question extends FileParser {

String mot ;
String reponse ;
String name;
int nb;
int nbquest ;
String traduction;

Question (String name , int nb){
    this.name=name;
    this.nb=nb;
}

Question() throws IOException{

    InputStreamReader isr = new InputStreamReader(System.in); 
    BufferedReader in = new BufferedReader(isr); 
    { 
        System.out.println("Entrer votre nom "); 
        String nom = in.readLine(); 
        name=nom;
    }    

    do {
        System.out.println("Rentrez le nombre de question :    ( max 20 )");
        Scanner nombrequest = new Scanner(System.in);
        nbquest = nombrequest.nextInt();
        nb=nbquest;
    }  while ( nbquest>20 ||nbquest<=0);

}

public void Play () throws IOException{

    int i=0;   
    boolean bool=true;    
    int score=0;
    String continuer; 
    Date maDate = new Date();
    String a = maDate.toString() ;

    while (i<nbquest && bool) {
        InputStreamReader isr = new InputStreamReader(System.in); 
        BufferedReader in = new BufferedReader(isr); 

        System.out.println("donner la traduction de "+french);
        String reponseee  = in.readLine();     
        traduction=reponseee;  

        if(bool=true ){ 
            if(traduction.equals(english)){
                score++;
                System.out.println("Bravo! Bonne reponse");
            }else{
                System.out.println("Mauvaise reponse");
            }
        }


推荐答案

I我们将忽略源代码并且一般地回答这个问题。您可以直接或使用getter访问对象的实例变量。我将在这个例子中使用getters。

I'm going to ignore the source code and answer this in general terms. You can access the instance variables of an object, either directly or using getters. I will use getters in this example.


实例变量 - 只要类生活,它们是
类的主体,但不在任何方法或
构造函数

Instance variables - live for as long as the class lives, they are declared within the body of the class but not within any method or constructor

方法变量 - 只要方法生效,不再是,通常
做一些工作,然后走了,他们在一个
方法/构造函数中声明

Method variables - live for as long as the method and no longer, usually do some job and then are gone, they are declared within a method/constructor



public class ClassThatWantsFields {

    public String combineFields(ClassWithFields classWithFields){
        //if I have access to the object classWithFields then I have access to its
        //public methods (and possibly also protected and default access; but this is outside the scope of this question)
        return classWithFields.getEnglish()+classWithFields.getFrench();
    }

    public static void main(String[] args){
        ClassWithFields classWithFields=new ClassWithFields();
        ClassThatWantsFields classThatWantsFields=new ClassThatWantsFields();

        System.out.println(classThatWantsFields.combineFields(classWithFields));
    }

}


public class ClassWithFields {
    private String English; //these are instance variables, they live for as long as the object lives
    private String French;

    private String preservedMayFly; 

    public ClassWithFields(){
        English="A language called English";
        French="A language called French";

        //mayfly is a method variable, it will be gone once the constructor 
        //exits, anything you want to keep for the life of the object should 
        //NOT be a method variable
        String mayfly="I won't live long";

        //preservedMayFly is an instance variable and will live as long as
        //the object
        preservedMayFly=mayfly+"but I can be used within the method"
    }

    public String getEnglish() {
        return English;
    }

    public String getFrench() {
        return French;
    }



}

这篇关于使用另一个类的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆