Java中使用ArrayList实现继承

huangapple go评论69阅读模式
英文:

Inheritance in java using array list

问题

I'm using simple inheritance in java but i'm not able to get the values from the parent class. I'm following all the standards in java but it's returning empty. I know in order to access the parents class we've to use `super` keyword. But i'm getting an empty array for `price_ofProducts` in my program. Where I'm getting things wrong? Need a direction. Here's my code:

package project;

import java.util.ArrayList;

public class Main{

public static void main(String args[])
{
    Product p = new Product();
    p.name_ofProducts.add("laptop");
    p.price_ofProducts.add(5000);
    p.Quantity_ofProducts.add(3);
    p.name_ofProductCompany.add("dell");
    p.Product_idNumber.add(287);
    
    p.name_ofProducts.add("Moblie");
    p.price_ofProducts.add(3000);
    p.Quantity_ofProducts.add(3);
    p.name_ofProductCompany.add("Nokia");
    p.Product_idNumber.add(3567);
    
    p.name_ofProducts.add("I pad");
    p.price_ofProducts.add(9000);
    p.Quantity_ofProducts.add(5);
    p.name_ofProductCompany.add("hp");
    p.Product_idNumber.add(7845);
    
    p.name_ofProducts.add("ispot");
    p.price_ofProducts.add(600);
    p.Quantity_ofProducts.add(2);
    p.name_ofProductCompany.add("Nokia");
    p.Product_idNumber.add(233);
    
    p.name_ofProducts.add("Oven");
    p.price_ofProducts.add(10000);
    p.Quantity_ofProducts.add(5);
    p.name_ofProductCompany.add("Orient");
    p.Product_idNumber.add(766);
    
    p.name_ofProducts.add("AC");
    p.price_ofProducts.add(15000);
    p.Quantity_ofProducts.add(5);
    p.name_ofProductCompany.add("Haier");
    p.Product_idNumber.add(577);
    
    p.dispalyProduct();
    
    Transaction tr1 = new Transaction();
    tr1.Product_Number();
}
}

My Product: 

package project;
import java.util.*;

public class Product {
    
        protected int Quantity; 
    protected int price;
    protected String name;
    protected int id;
    protected String company;
    protected ArrayList<String> name_ofProducts= new ArrayList<String>();
    
    protected ArrayList<Integer> price_ofProducts = new ArrayList<Integer>();
    protected ArrayList<Integer> Quantity_ofProducts = new ArrayList<Integer>();
    protected ArrayList<String> name_ofProductCompany = new ArrayList<String>();
    protected ArrayList<Integer> Product_idNumber = new ArrayList<Integer>();
    
    public Product()
    {
            
    }
    public Product(int price, String name, int Quantity, String company, int id) 
    {
        
        this.Quantity=Quantity;
        this.price = price;
        this.name = name;
        this.company=company;
        this.id=id;
        
    }

    Product(ArrayList<Integer> price_ofProducts) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    // Setter Method
    public void setName(String name) {
        this.name = name;
    }
        
    public void setPrice(int price) {
        this.price = price;
    }
    public void setQuantity(int Quantity)
    {
        this.Quantity=Quantity;
    }
    public void setCompany(String company)
    {
        this.company=company;
    }
    public void setId(int id)
    {
        this.id=id;
    }
    // Getter Methods
    public String getName() {
        return name;
    }
    public int getPrice() {
        return price;
    }	
    public int getQuantity()
    {
        return Quantity;
    }
    public String getCompany()
    {
        return company;
    }
    public int getId()
    {
        return id;
    }

    public void dispalyProduct()
    {
        System.out.println("Name\tPrices\tQuantity\tcompany\tid_no");
        for(int i=0;i<name_ofProducts.size();i++)
        {
            System.out.println((i+1)+")"+name_ofProducts.get(i)+"\t"+price_ofProducts.get(i)+"\t"+Quantity_ofProducts.get(i)+"\t"+name_ofProductCompany.get(i)+"\t"+Product_idNumber.get(i));
        }
        
    }
}

My Transaction class:

package project;
import java.util.*;

public class Transaction extends Product{    
    private int product_Number;
    private int calculation;
    public Transaction()
    {
         
    }
    
    public void setProduct_Number(int product_Number)
    {
        this.product_Number=product_Number;
        
    }
    public int getProduct_Number()
    {
        return product_Number;
    }
    
    //@SuppressWarnings("resource")
    public void Product_Number()
    {
        /*
            Products Display
        */
        dispalyProduct();
        
        /*
            Declaring Variables
        */
        int quantity;
        int product_number=0;
        /*
            Performing Transaction
        */
        System.out.println("Enter the Number of product that you want to parchased");
        Scanner Input = new Scanner(System.in);
        product_number= Input.nextInt();
        
    System.out.println("Product Number is: "+product_number);
        System.out.println("Enter Quantity");
        quantity = Input.nextInt();
    System.out.println("Quantity Entered "+quantity);
    
    //System.out.println(super.price_ofProducts);
    switch (product_number)
    {
        case 1:
            try {

                System.out.println("Price of Product: " + super.price_ofProducts.get(1-1));
                calculation = super.price_ofProducts.get(1-1);
                System.out.println(calculation);
                calculation(calculation,quantity);
                System.out.println("It works.");
            } catch (Exception e) {
                System.out.println("Error: " + e);
            }
            break;
        case 2:
            try {
                System.out.println("Price of Product: " + super.price_ofProducts.get(2-1));
                calculation = super.price_ofProducts.get(2-1);
                calculation(calculation,quantity);
            } catch(Exception e) {
                System.out.println("Error: " + e);
            }
            break;
        case 3:
            try{
                
                System.out.println(super.price_ofProducts.get(3-1));
                calculation= super.price_ofProducts.get(3-1);
                calculation(calculation,quantity);
            } catch(Exception e){
                System.out.println("Error: " + e);
            }
            break;
        case 4:
            try{
                calculation= super.price_ofProducts.get(4-1);
                calculation(calculation,quantity);
            }
            catch(Exception e){
                System.out.println("Error: " + e);
            }
            break;
        case 5:
            try{
                calculation=super.price_ofProducts.get(5-1);
                calculation(calculation,quantity);
            }catch(Exception e){
                System.out.println("Error: " + e);
            }
            break;
        case 6:
            try{
                calculation=super.price_ofProducts.get(6-1);
                calculation(calculation,quantity);
            }catch(Exception e){
                System.out.println("Error: " + e);
            }
            break;      
        default:
            System.out.println("you entered wrong number");
            break;
    }
    }
    public void calculation(int a, int b)
    {
        int total;
        total

<details>
<summary>英文:</summary>

I&#39;m using simple inheritance in java but i&#39;m not able to get the values from the parent class. I&#39;m following all the standards in java but it&#39;s returning empty. I know in order to access the parents class we&#39;ve to use `super` keyword. But i&#39;m getting an empty array for `price_ofProducts` in my program. Where I&#39;m getting things wrong? Need a direction. Here&#39;s my code:

    package project;
    
    import java.util.ArrayList;
    
    public class Main{
    
    public static void main(String args[])
    {
    	Product p = new Product();
    	p.name_ofProducts.add(&quot;laptop&quot;);
    	p.price_ofProducts.add(5000);
    	p.Quantity_ofProducts.add(3);
    	p.name_ofProductCompany.add(&quot;dell&quot;);
    	p.Product_idNumber.add(287);
    	
    	p.name_ofProducts.add(&quot;Moblie&quot;);
    	p.price_ofProducts.add(3000);
    	p.Quantity_ofProducts.add(3);
    	p.name_ofProductCompany.add(&quot;Nokia&quot;);
    	p.Product_idNumber.add(3567);
    	
    	p.name_ofProducts.add(&quot;I pad&quot;);
    	p.price_ofProducts.add(9000);
    	p.Quantity_ofProducts.add(5);
    	p.name_ofProductCompany.add(&quot;hp&quot;);
    	p.Product_idNumber.add(7845);
    	
    	p.name_ofProducts.add(&quot;ispot&quot;);
    	p.price_ofProducts.add(600);
    	p.Quantity_ofProducts.add(2);
    	p.name_ofProductCompany.add(&quot;Nokia&quot;);
    	p.Product_idNumber.add(233);
    	
    	p.name_ofProducts.add(&quot;Oven&quot;);
    	p.price_ofProducts.add(10000);
    	p.Quantity_ofProducts.add(5);
    	p.name_ofProductCompany.add(&quot;Orient&quot;);
    	p.Product_idNumber.add(766);
    	
    	p.name_ofProducts.add(&quot;AC&quot;);
    	p.price_ofProducts.add(15000);
    	p.Quantity_ofProducts.add(5);
    	p.name_ofProductCompany.add(&quot;Haier&quot;);
    	p.Product_idNumber.add(577);
    	
    	p.dispalyProduct();
    	
    //        System.out.println(&quot;Price of Products Array: &quot; + p.price_ofProducts.get(1));
    	Transaction tr1 = new Transaction();
    	tr1.Product_Number();
    	
    	//p.displayProduct();
    	
    }
    }

My Product: 

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package project;
    import java.util.*;
    /**
     *
     * @author zfhas
     */
    public class Product {
    	
            protected int Quantity; 
    	protected int price;
    	protected String name;
    	protected int id;
    	protected String company;
    	protected ArrayList&lt;String&gt; name_ofProducts= new ArrayList&lt;String&gt;();
            
    	protected ArrayList&lt;Integer&gt; price_ofProducts = new ArrayList&lt;Integer&gt;();
    	protected ArrayList&lt;Integer&gt; Quantity_ofProducts = new ArrayList&lt;Integer&gt;();
    	protected ArrayList&lt;String&gt; name_ofProductCompany = new ArrayList&lt;String&gt;();
    	protected ArrayList&lt;Integer&gt; Product_idNumber = new ArrayList&lt;Integer&gt;();
    	
    	public Product()
    	{
                
    	}
    	public Product(int price, String name, int Quantity, String company, int id) 
    	{
    		
    		this.Quantity=Quantity;
    		this.price = price;
    		this.name = name;
    		this.company=company;
    		this.id=id;
    		
    	}
    
        Product(ArrayList&lt;Integer&gt; price_ofProducts) {
            throw new UnsupportedOperationException(&quot;Not supported yet.&quot;); //To change body of generated methods, choose Tools | Templates.
        }
    	// Setter Method
    	public void setName(String name) {
    		this.name = name;
    	}
            
    	public void setPrice(int price) {
    		this.price = price;
    	}
    	public void setQuantity(int Quantity)
    	{
    		this.Quantity=Quantity;
    	}
    	public void setCompany(String company)
    	{
    		this.company=company;
    	}
    	public void setId(int id)
    	{
    		this.id=id;
    	}
    	// Getter Methods
    	public String getName() {
    		return name;
    	}
    	public int getPrice() {
    		return price;
    	}	
    	public int getQuantity()
    	{
    		return Quantity;
    	}
    	public String getCompany()
    	{
    		return company;
    	}
    	public int getId()
    	{
    		return id;
    	}
    
    	public void dispalyProduct()
    	{
    		System.out.println(&quot;Name\tPrices\tQuantity\tcompany\tid_no&quot;);
    		for(int i=0;i&lt;name_ofProducts.size();i++)
    		{
    			System.out.println((i+1)+&quot;)&quot;+name_ofProducts.get(i)+&quot;&quot;+price_ofProducts.get(i)+&quot;\t&quot;+Quantity_ofProducts.get(i)+&quot;\t&quot;+name_ofProductCompany.get(i)+&quot;\t&quot;+Product_idNumber.get(i));
    		}
    		
    	}
    }

My Transaction class:

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package project;
    import java.util.*;
    
    /**
     *
     * @author zfhas
     */
    public class Transaction extends Product{	
    	private int product_Number;
    	private int calculation;
    	public Transaction()
    	{
                 
    	}
    	
    	public void setProduct_Number(int product_Number)
    	{
    		this.product_Number=product_Number;
    		
    	}
    	public int getProduct_Number()
    	{
    		return product_Number;
    	}
    	
    	//@SuppressWarnings(&quot;resource&quot;)
    	public void Product_Number()
    	{
                    /*
                        Products Display
                    */
    		dispalyProduct();
                    
                    /*
                        Declaring Variables
                    */
    		int quantity;
    		int product_number=0;
                    /*
                        Performing Transaction
                    */
    		System.out.println(&quot;Enter the Number of product that you want to parchased&quot;);
    		Scanner Input = new Scanner(System.in);
    		product_number= Input.nextInt();
                    
            System.out.println(&quot;Product Number is: &quot;+product_number);
    		System.out.println(&quot;Enter Quantity&quot;);
    		quantity = Input.nextInt();
            System.out.println(&quot;Quantity Entered &quot;+quantity);
            
            //System.out.println(super.price_ofProducts);
            switch (product_number)
            {
                case 1:
                    try {
    
                        System.out.println(&quot;Price of Product: &quot; + super.price_ofProducts.get(1-1));
                        calculation = super.price_ofProducts.get(1-1);
                        System.out.println(calculation);
                        calculation(calculation,quantity);
                        System.out.println(&quot;It works.&quot;);
                    } catch (Exception e) {
                        System.out.println(&quot;Error: &quot; + e);
                    }
                    break;
                case 2:
                    try {
                        System.out.println(&quot;Price of Product: &quot; + super.price_ofProducts.get(2-1));
                        calculation = super.price_ofProducts.get(2-1);
                        calculation(calculation,quantity);
                    } catch(Exception e) {
                        System.out.println(&quot;Error: &quot; + e);
                    }
                    break;
                case 3:
                    try{
                        
                        System.out.println(super.price_ofProducts.get(3-1));
                        calculation= super.price_ofProducts.get(3-1);
                        calculation(calculation,quantity);
                    } catch(Exception e){
                        System.out.println(&quot;Error: &quot; + e);
                    }
                    break;
                case 4:
                    try{
                        calculation= super.price_ofProducts.get(4-1);
                        calculation(calculation,quantity);
                    }
                    catch(Exception e){
                        System.out.println(&quot;Error: &quot; + e);
                    }
                    break;
                case 5:
                    try{
                        calculation=super.price_ofProducts.get(5-1);
                        calculation(calculation,quantity);
                    }catch(Exception e){
                        System.out.println(&quot;Error: &quot; + e);
                    }
                    break;
                case 6:
                    try{
                        calculation=super.price_ofProducts.get(6-1);
                        calculation(calculation,quantity);
                    }catch(Exception e){
                        System.out.println(&quot;Error: &quot; + e);
                    }
                    break;		
                default:
                    System.out.println(&quot;you entered wrong number&quot;);
                    break;
            }
    	}
            public void calculation(int a, int b)
    	{
                int total;
                total = a*b;
                billDisplay(total);
    	}
    	public void billDisplay(int total)
    	{
                System.out.println(&quot;your total bill = &quot;+total);
                billrecive(total);
    	}
    	public void billrecive(int total)
    	{
                @SuppressWarnings(&quot;resource&quot;)
                Scanner Input = new Scanner(System.in);
                int enteredMoney=Input.nextInt();
                int i=0;
                while(i&gt;1)
                {	
                    if(enteredMoney==total)
                    {
                        displayRecipt();
                        break;
                    }
                    else if (enteredMoney&lt;total)
                    {
                        System.out.print(&quot;Enter More= &quot;);
                        int enteredMoney1=Input.nextInt();
                        enteredMoney+=enteredMoney1;
                        continue;
                    }	
                    else if(enteredMoney&gt;total)
                    {
                        enteredMoney=enteredMoney-total;
                        System.out.println(&quot;Your remaining are : &quot;+enteredMoney);
                        displayRecipt();
                        break;
                    }
                }
    	}
            public void displayRecipt()
            {
                    System.out.println(&quot;billing done&quot;);
                    dispalyProduct();
            }
    	public void addProduts(String name,int price,int quantity,String company, int id)
    	{
    		name_ofProducts.add(name);
    		price_ofProducts.add(price);
    		Quantity_ofProducts.add(quantity);
    		name_ofProductCompany.add(company);
    		Product_idNumber.add(id);
    	}		
    }

</details>


# 答案1
**得分**: 1

```Product p``````Transaction tr1``` 是两个完全独立的对象实例

你在 ```p``` 中设置了所有的产品然后通过 ```tr1``` 查询用户当你询问用户要选择哪个产品时,```tr1``` 不具有任何相同的产品数据或任何数据)。之所以能够显示产品仅因为你调用了 ```p.displayProduct```。

<details>
<summary>英文:</summary>

```Product p``` and ```Transaction tr1``` are two completely separate object instances.

You set up all of your products in ```p``` and then query the user via ```tr1```. ```tr1``` doesn&#39;t have any of the same product data (or any data at all) when you ask the user which one to pick. It only displays the products at all because you call ```p.dispalyProduct```  

</details>



huangapple
  • 本文由 发表于 2020年9月4日 06:15:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63732358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定