通过创建对象将值分配给其他类的数组

时间:2015-06-27 14:35:10

标签: java arrays class object

import java.util.Scanner;

public class product
{
int productId[];
String productName,productType[];
float productPrice[];

public static void main(String...args)
{
    shop ob=new shop();
    ob.startshop();
}
}

class shop
{   
product obj;

void startshop()
{
    obj=new product();
    obj.productId()={1001,1002,1003,1004,1005,1006,1007,1008,1009,1010};
    obj.productName={"Cadbury","Parker Vector","Nescafe","kissan                   jam","Herbal oil","Garnier man's","Lays chips","Bourn bourn biscuits","Bournvita","Pepse"};
        obj.productType={"Chocolate","Stationary","Coffee","Jam","Oil","Face wash","chips","Biscuits","Health Supplement","Cold Drink"}
        obj.productPrice={20,150,80,65,30,79,10,20,140,24};
        Scanner oj=new Scanner(System.in);
        System.out.println(obj.productName);
        boolean c=true;
        while(c==true)
        {
            System.out.println("Please press\n1 for view all products\n2 for         view product detail\n3 to exit shop");
            int ch=oj.nextInt();
            switch(ch)
            {
                case 1:viewAllProducts();
                    break;
                case 2:System.out.println("Enter product id to search      starting from 1001 to 1010");
                    int i=oj.nextInt();
                    viewProductDetail(i);
                    break;
                    case 3:System.out.println("You are out of shop :)");
                    c=false;
                    break;
            }
        }
    }
    void viewAllProducts()
    {   System.out.println("ID\t\tNAME\t\tTYPE\t\tPRICE");
        for(int i=0;i<10;i++)
        {
            System.out.println(obj.prductId[i]+"\t\t"+obj.productName[i]+"\t\t+"+obj.product    Type[i]+"\t\t"+obj.productPrice[i]);
        }
    }
    void viewProductDetail(int id)
    {
        int k=15;
        for(int i=0;i<10;i++)
        {
        if(obj.productId[i]==id)
        k=i;
        }
        if(k<10)
        {
            System.out.println("Product id "+obj.productId[k]);
            System.out.println("Product name "+obj.productName[k]);
            System.out.println("Product type "+obj.productType[k]);
            System.out.println("Product price "obj.productPrice[k]);
        }
    }
}       

在此代码中,由于以下行代码正在给出错误

  

obj.productId()= {1001,1002,1003,1004,1005,1006,1007,1008,1009,1010};         obj.productName = {&#34;吉百利&#34;,&#34; Parker Vector&#34;,&#34; Nescafe&#34;,&#34; kissan   果酱&#34;,&#34;草药油&#34;,#34;卡尼尔男人&#34;,&#34;奠定筹码&#34;,&#34; Bourn bourn   饼干&#34;&#34; Bournvita&#34;&#34; Pepse&#34;};         obj.productType = {&#34;巧克力&#34;&#34;固定&#34;&#34;咖啡&#34;&#34;果酱&#34;&#34;油&#34 ;, &#34;人脸   洗&#34;,&#34;筹码&#34;,&#34;饼干&#34;,&#34;健康补品&#34;,&#34;冷饮&#34;};         obj.productPrice = {20,150,80,65,30,79,10,20,140,​​24};

请告诉我,如果我使用错误的语法来初始化其他类的实例变量。

3 个答案:

答案 0 :(得分:1)

是的,你在这里使用了错误的语法。 obj.productId()将是一个函数,但在您的情况下,它是类Product的参数,只需写obj.productId即可。

但您必须记住,创建参数后不能使用int array[] = {example}语法。初始化参数时必须使用它。

你可以做到

float productPrice[] = {"0.0","1,1","2.2"};

但不是

float productPrice[];
productPrice = {"0.0","1,1","2.2"};

我希望这会对你有所帮助:)。

答案 1 :(得分:0)

obj.productId()={1001,1002,1003,1004,1005,1006,1007,1008,1009,1010};此语法错误,productId不是函数,请在没有括号的情况下使用它。

示例 -

obj.productId= new int[]{1001,1002,1003,1004,1005,1006,1007,1008,1009,1010};

答案 2 :(得分:0)

我已按以下方式修改了您的问题,我得到了预期的结果。请仔细阅读。

package product;

/**
*
* @author sunil
*/

import java.util.*;


public class product
{
int productId[];
String productName[],productType[];
int productPrice[];

public static void main(String args [])
{
shop ob = new shop();
ob.startshop();
}

}

class shop{   

product obj;

void startshop()
{
obj = new product();
obj.productId = new int[]  
{1001,1002,1003,1004,1005,1006,1007,1008,1009,1010};
obj.productName= new String[]{"Cadbury","Parker Vector","Nescafe","kissan  
jam","Herbal oil","Garnier man's","Lays chips","Bourn bourn 
biscuits","Bournvita","Pepsi"};
    obj.productType = new String[] 
{"Chocolate","Stationary","Coffee","Jam","Oil","Face 
wash","chips","Biscuits","Health Supplement","Cold Drink"};
    obj.productPrice = new int[]{20,150,80,65,30,79,10,20,140,24};
    Scanner oj=new Scanner(System.in);
    System.out.println(Arrays.toString(obj.productId));
    System.out.println(Arrays.toString(obj.productName));
    System.out.println(Arrays.toString(obj.productType));
    System.out.println(Arrays.toString(obj.productPrice));
    boolean c=true;
    while(c==true)
    {
        System.out.println("Please press\n1 for view all products\n2 for 
    view product detail\n3 to exit shop");
        int ch=oj.nextInt();
        switch(ch)
        {
            case 1:viewAllProducts();
                break;
            case 2:System.out.println("Enter product id to search starting 
    from 1001 to 1010");
                int i=oj.nextInt();
                viewProductDetail(i);
                break;
                case 3:System.out.println("You are out of shop :)");
                c=false;
                break;
          }
       }
    }
    void viewAllProducts()
    {   System.out.println("ID\t\tNAME\t\tTYPE\t\tPRICE");
    for(int i=0;i<10;i++)
    {
        System.out.println(obj.productId[i]+"\t\t"+obj.productName[i] 
+"\t\t+"+obj.productType[i]+"\t\t"+obj.productPrice[i]);
    }
}
void viewProductDetail(int id)
{
    int k=15;
    for(int i=0;i<10;i++)
    {
    if(obj.productId[i]==id)
    k=i;
    }
    if(k<10)
    {
        System.out.println("Product id "+obj.productId[k]);
        System.out.println("Product name "+obj.productName[k]);
        System.out.println("Product type "+obj.productType[k]);
        System.out.println("Product price "+obj.productPrice[k]);
      }
   }
}