从没有参数的构造函数中读取方法返回值

时间:2015-11-24 11:47:09

标签: java class oop methods

这是我的超类Masina,它包含一个没有参数的构造函数,一个带参数和一个复制构造函数

package proiect;
import java.util.Scanner;

public class Masina {
    int NrDeLocuri,Putere;
    float Pret;
    String Culoare;
    String Producator;

public Masina(){
            this.Producator = "Default";
            this.Pret = 0;
            this.Culoare = "Default";
            this.Putere = 0;
            this.NrDeLocuri = 0;
            }
public Masina(String prod,int pr,String cu,int pu,int nrl)
        {
            Producator=prod;
            Pret=pr;
            Culoare=cu;
            Putere=pu;
            NrDeLocuri=nrl;
            }
public Masina(Masina other)
        {
            this.Producator=other.Producator;
            this.Pret=other.Pret;
            this.Culoare=other.Culoare;
            this.Putere=other.Putere;
            this.NrDeLocuri=other.NrDeLocuri;
            }

public Masina citireDate()
{
    Masina m=new Masina();
    Scanner sc1 = new Scanner(System.in);
    System.out.println("Introduceti Producatorul : ");
    m.Producator = sc1.next();
    System.out.println("Introduceti Pretul: ");
    m.Pret = sc1.nextFloat();
    System.out.println("Introduceti Culoarea: ");
    m.Culoare = sc1.next();
    System.out.println("Introduceti Puterea : ");
    m.Putere = sc1.nextInt();
    System.out.println("Introduceti Numarul de Locuri : ");
    m.NrDeLocuri = sc1.nextInt();
    return m;

}




public String toString()
        {
            return "Producator: "+Producator+"\nPret: "+Pret+"\nCuloare: "+
                    Culoare+"\nPutere: "+Putere+"\nNumaraDeLocuri: "+NrDeLocuri;
            }
}

这是我创建一个Masina类型的对象的测试类,必须使用名为citireDate的Masina类中声明的读取方法读取数据。

package proiect;
import java.util.Scanner;
import java.util.ArrayList;


public class TestMasina {

    public static void main(String args[]){

        Masina a = new Masina();

        a.citireDate();
        System.out.println(a.toString());
    }

}

接下来是运行程序后的结果

Introduceti Producatorul : 
BMW
Introduceti Pretul: 
500
Introduceti Culoarea: 
Alba
Introduceti Puterea : 
200
Introduceti Numarul de Locuri : 
5
Producator: Default
Pret: 0.0
Culoare: Default
Putere: 0
NumaraDeLocuri: 0

我的阅读方法不会返回我给对象参数的值,例如" Culoare" " Putere"返回给出默认值。

2 个答案:

答案 0 :(得分:0)

你的问题在于:

Masina m=new Masina();

您正在创建Masina的实例。如果要更新当前对象的字段,您应该这样做:

this.Producator = sc1.next();   //change m. to this.

也适用于其他字段。

最重要的是,我想知道使用getter和setter的OO原则。你在课堂上编写你的逻辑,这对我来说很奇怪。您可以将字段设置为私有,并使用getter和setter访问它们。

答案 1 :(得分:0)

您没有得到预期的结果,因为函数citireDate()会返回一个新的Masina对象而您没有使用它,而是使用a对象

a = a.citireDate();
System.out.println(a.toString());

提示

1-将实例变量设为私有,

2 - 使用setter和getter访问实例变量

3 - 使用对象克隆而不是使用复制构造函数来创建类implements Cloneable

public class Masina implements Cloneable{
   public Object clone()throws CloneNotSupportedException{  
     return super.clone();  
  } 
}

使用它: -

try{  
Masina a = new Masina();
Masina b = (Masina)a.clone();  
}catch(CloneNotSupportedException c){}  
  System.out.println("Not supported");
}