Java主方法中未显示对象属性

问题描述 投票:0回答:1

即使将对象作为参数传递,我也无法在主要方法中显示电话号码和地址变量。在打印时,仅显示客户ID,客户名称和总计变量。我试图在我的Order类构造函数中调用getter和setter方法,但似乎没有任何效果。这是我的代码:

主类:

    public class CustomerOrder {

    public static void main(String[] args) {
        Customer c = new Customer(1, "Bob");
        c.setPhone_number("#######");
        c.setAddress("9823 Random Street Address");
        Order one = new Order(c);
        int order_id = one.getOrder_id();
        one.add(order_id, 1);
        one.add(order_id, 3);
        one.add(order_id, 4.50);
        System.out.println(one);
    }

}

客户类别:

public class Customer {
    private int customer_id;
    private String customer_name;
    private String phone_number;
    private String address;


    public Customer(int customer_id, String customer_name) {
        this.customer_id = customer_id;
        this.customer_name = customer_name;
    }

    public Customer() {} // default constructor

    public int getCustomer_id() {
        return customer_id;
    }

    public void setCustomer_id(int customer_id) {
        this.customer_id = customer_id;
    }

    public String getCustomer_name() {
        return customer_name;
    }

    public void setCustomer_name(String customer_name) {
        this.customer_name = customer_name;
    }

    public String getPhone_number() {
        return phone_number;
    }

    public void setPhone_number(String phone_number) {
        this.phone_number = phone_number;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String toString() {
        return customer_id + " " + customer_name + " " + phone_number + " " + address;
    }
}

订单类别:

import java.util.Random;

public class Order extends Customer {
    Random rand = new Random(); // Random object

    private int order_id;
    private double order_total;

    public Order(Customer customer){ //constructor 
        super(customer.getCustomer_id(), customer.getCustomer_name());

    }

    public int getOrder_id() {
        return order_id;
    }

    public void setOrder_id() {
        this.order_id = rand.nextInt(5000); // sets a random number for the order id
    }

    public void add(int order_id, double order_total)
    {
        this.order_id = order_id;
        this.order_total += order_total;
    }

    public double getOrderTotal() {
        return order_total;
    }

    public String toString() {
        return "Customer ID: " + getCustomer_id() + "\nCustomer Name: " + getCustomer_name() + "\nCustomer Phone Number: " + getPhone_number() + "\nCustomer Address: " + getAddress() + "\nGrand Total: $" + getOrderTotal();
    }
}

任何帮助将不胜感激,谢谢。

java class constructor getter setter
1个回答
0
投票

看起来您似乎对面向对象编程缺乏了解。 Order扩展Customer并没有任何意义。您真正想要的是Order仅引用一个Customer

之所以没有在输出中看到所需的内容,是因为您在Customer c实例上设置了数据,但是在实例化Order实例时,实际上并没有对值做任何事情。仅仅因为Company extends Customer并不意味着它固有地具有实例值。

我不会解决您的整个问题,因为这听起来像是作业,但我会给您,您可以告诉我是否有帮助。

class Customer {
   // variabels with getters/setters
}

class Order {

   private Customer customer;

   // other properties with getters/setters

   @Override
   public String toString() {
     return "...." + "...." + this.getCustomer().getAddress() + "....";
   }
}

Customer customer = new Customer(....)
customer.setBla(...)
customer.setFoo(...)

Order order = new Order();
order.setCustomer(customer);
order.setWhatever(...);
System.out.println(order);
© www.soinside.com 2019 - 2024. All rights reserved.