java - 我正在尝试从子类访问私有(private)变量

标签 java object queue private

这是我的子类 RECORD 中的代码的开头。

类记录{

private int shares;
private int pricePerShare;

// constructor
Record(int sharesNewValue, int pricePerShareNewValue) {
    shares = sharesNewValue;
    pricePerShare = pricePerShareNewValue;
}

// inspectors
public int getShares() {
    return shares;
}

public int getPricePerShare() {
    return pricePerShare;
}

// modifiers
public void setShares(int sharesNewValue) {
    shares = sharesNewValue;
}

public void setPricePerShare(int pricePerShareNewValue) {
    pricePerShare = pricePerShareNewValue;
}
}

我想访问不同类中的主方法中的份额值。我将 RECORD 类链接到另一个名为 QUEUE 的子类。在我的主要方法中,我有一个指向 QUEUE 的链接:

 class Lab04a {
                 public static Queue Q = new Queue(); 
              }

稍后在代码中,我需要从 Record 类中的 SHARES 变量中减去 int 值,但因为它是 Record 类型,所以我不知道如何执行此操作!

我不确定我在解释这一点时是否足够清楚,如果您还有任何其他问题,我将非常乐意回复。

谢谢。

由于我无法连贯地陈述我在这个实验作业中想要完成的任务,我将完整地发布我的其他两门类(class):

 class Queue {

private int count; // number of elements in the queue
private int head; // index of head element, -1 if queue is empty
private int tail; // index of tail element, -1 if queue is empty
private int MAXSIZE = 1; // Physical size of the queue. DO NOT CHANGE!
private Record[] array; // circular array to store the elements of the queue

// constructor
Queue() {
    count = 0;
    head = -1;
    tail = -1;
    array = new Record[MAXSIZE];
}

// inspectors
public boolean empty() {
    // Returns true if the queue is empty. Otherwise returns false.
    return (count != 0);
}

public int size() {
    // Returns the number of elements in the queue
    return count;
}

public Record front(){
    // Returns the head element of the queue if the queue is not empty.
    // Otherwise returns a Record with its data parts set to -1.
    if (count == 0)
        return new Record(-1, -1);
    else
        return array[head];
}

public Record rear(){
    // Returns the tail element of the queue if the queue is not empty.
    // Otherwise returns a Record with its data parts set to -1.
    if (count ==0)
        return new Record(-1, -1);
    else
        return array[tail];

}

public String toString() {
    // Returns the elements of the queue
    String str = "< ";
    int h = head;
    for (int i = 0; i < count; i++){
        str += "(" + array[h].getShares() + ", " + array[h].getPricePerShare() + ") ";
        h = (h+1) % MAXSIZE;
    }
    str += ">";
    return str;
}

// modifiers
public boolean dequeue() {
    // Removes the head element of the queue. 
    if (count == 0)
        return false;
    if (count == 1) {
        count = 0;
        head = -1;
        tail = -1;
    } 
    if (count > 1){
        head = (head + 1) % MAXSIZE;
        count--;
    }
    return true;
}

public void enqueue(Record element) {
    // Enqueues element to the tail of the queue.
    //if max size is reached, it doubles the size to allow for more values
    if (count == MAXSIZE) {
        Record[] array2 = new Record[MAXSIZE * 2];
        for (int i = 0; i < count; i++) {
            array2[i] = array[i];
        }//closes for loop
        array = array2;
        MAXSIZE *= 2;
    }
    tail = (tail + 1) % MAXSIZE;
    array[tail] = element;
    if (count == 0)
        head = tail;
    count++;
}//close enqueue method

}//关闭类(class)

然后这是我的主要父类:

 class Lab04a {
public static Queue Q = new Queue(); // creates global object
public static Record R = Record;

public static void main(String args[]) {

    Scanner scan = new Scanner(System.in);

    int option, buyPrice, buyShares, sellPrice, sellShares, totalShares, totalValues, totalSellPrice;
    option = 0;
    totalShares = 0;
    totalValues = 0;
    Queue Q2 = Q;

    while (option != 3) {
        System.out.print("Enter option (1:buy, 2:sell, 3:quit): ");
        option = scan.nextInt();
        if (option == 1) {
            System.out.print("Enter shares to buy and price per share: ");
            buyShares = scan.nextInt();
            buyPrice = scan.nextInt();
            Record r = new Record(buyShares, buyPrice);
            Q.enqueue(r);
            totalShares = totalShares + buyShares;
            totalValues = totalValues + (buyShares * buyPrice);

        }// ends if



        if (option == 2) {
            System.out.print("Enter shares to sell and price per share: ");
            sellShares = scan.nextInt();
            sellPrice = scan.nextInt();
            totalSellPrice = sellPrice * sellShares;

            if (sellShares > totalShares) {
                System.out.println("You do not own enough shares for this sale.");
            }
            for (int i = sellShares; i > 0; ) {
                if (sellShares == Q.front().getShares()) {
                    i -= Q.front().getShares();
                    Q.dequeue();
                }
                if (sellShares < Q.front().getShares()){

                    Record minus; 
                    minus = Q.front() - sellShares;

                    Q.front().setShares(minus);

                    Q.front().setShares(Q.front().getShares());
                    i -= sellShares;
                }
            }


        }// ends if


        // Prints content of Queue
        System.out.println("Queue: " + Q.toString());

        System.out.println("Total Shares: " + totalShares);
        System.out.println("Total Shares Value: $" + totalValues);
        System.out.println();

    }// ends while loop
    System.out.println(Q.toString());
}// ends main method

}

最佳答案

如果我理解你的问题,你可以添加访问器和 mutator方法(或 getter 和 setter)

private int shares;
private int pricePerShare;
public int getShares() {
  return shares;
}
public void setShares(int shares) {
  this.shares = shares;
}
public int getPricePerShare() {
  return pricePerShare;
}
public void setPricePerShare(int pricePerShare) {
  this.pricePerShare = pricePerShare;
}

编辑

要使用它,

 Record record = Q.front(); // <-- I assume your Q contains Record(s).
 if (record.getShares() >= sellShares) {
   record.setShares(record.getShares() - sellShares); // <-- for example
 }

关于java - 我正在尝试从子类访问私有(private)变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24645741/

相关文章:

java - 在JMS队列中实现ActiveMQ的粘性 session (消息组)

java - 单击它时获取动态生成的 TextView 的文本

java 。数组每个值的简单 TimerTask

javascript - 如何比较 2 个数组中的值以过滤掉元素

javascript - 为什么我无法向 div 添加事件监听器?

java - 将数据传递给 onActivityResult 的问题

java - 创建无限数量的项目

java - 如何修复工具栏在选择另一个选项卡之前不更改标题的问题?

java - 如何使用较小的 JAR 导出 eclipse RCP 产品

java - Web 应用程序、集群 tomcat 和 mongoDB - 如何实现持久队列