用数组表实现java中的继承

qacovj5a  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(273)

我在java中使用简单的继承,但是我不能从父类中获取值。我遵循java中的所有标准,但它返回空值。我知道为了进入家长课程我们必须使用 super 关键字。但是我得到了一个空数组 price_ofProducts 在我的节目里。我哪里搞错了?我需要一个方向。这是我的密码:

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();

//        System.out.println("Price of Products Array: " + p.price_ofProducts.get(1));
    Transaction tr1 = new Transaction();
    tr1.Product_Number();

    //p.displayProduct();

}
}

我的产品:

/*
 * 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<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."); //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("Name\tPrices\tQuantity\tcompany\tid_no");
        for(int i=0;i<name_ofProducts.size();i++)
        {
            System.out.println((i+1)+")"+name_ofProducts.get(i)+""+price_ofProducts.get(i)+"\t"+Quantity_ofProducts.get(i)+"\t"+name_ofProductCompany.get(i)+"\t"+Product_idNumber.get(i));
        }

    }
}

我的事务类:

/*
 * 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("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 = a*b;
            billDisplay(total);
    }
    public void billDisplay(int total)
    {
            System.out.println("your total bill = "+total);
            billrecive(total);
    }
    public void billrecive(int total)
    {
            @SuppressWarnings("resource")
            Scanner Input = new Scanner(System.in);
            int enteredMoney=Input.nextInt();
            int i=0;
            while(i>1)
            {   
                if(enteredMoney==total)
                {
                    displayRecipt();
                    break;
                }
                else if (enteredMoney<total)
                {
                    System.out.print("Enter More= ");
                    int enteredMoney1=Input.nextInt();
                    enteredMoney+=enteredMoney1;
                    continue;
                }   
                else if(enteredMoney>total)
                {
                    enteredMoney=enteredMoney-total;
                    System.out.println("Your remaining are : "+enteredMoney);
                    displayRecipt();
                    break;
                }
            }
    }
        public void displayRecipt()
        {
                System.out.println("billing done");
                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);
    }       
}
sg24os4d

sg24os4d1#

Product p 以及 Transaction tr1 是两个完全独立的对象示例。
你的产品都是在 p 然后通过 tr1 . tr1 当您询问用户选择哪个产品时,没有任何相同的产品数据(或任何数据)。它只显示产品,因为你打电话 p.dispalyProduct

相关问题