简体   繁体   English

无法从类方法(Java)访问对象变量

[英]Cannot access object variable from class method(Java)

A homework assignment requires me to make a bag data structure in Java by implementing java.util.Collections. 一项家庭作业需要我通过实现java.util.Collections在Java中创建bag数据结构。 The bag data must be stored in an array. 行李数据必须存储在数组中。 I cannot seem to be able to get my class methods to access the array I give each object in the class. 我似乎无法使我的类方法访问我为类中的每个对象提供的数组。

Here's the code giving me the issue: 这是给我问题的代码:

import java.util.*;
import java.lang.*;

class Bag<T> implements Collection<T> {

//MAIN METHOD///////////////////////////////

public static void main(String[] args) {

Bag<Integer> bravo = new Bag<Integer>();

System.out.println(bravo.size());

}///////////////////////////////////////////


//CONSTUCTOR///////////////////////////
public Bag() {

T[] bagarray = (T[])new Object[10];

}
///////////////////////////////////////


//METHODS/////////////////////////////////////////////////////////////////////////////////

public int size() {

int temp;

temp = bagarray.length;

return temp;

}

During compile I'm given an cannotfindsymbol error for bagarray. 在编译期间,我收到了bagarray的cannotfindsymbol错误。 Yet, I have written code before performing this exact same maneuver. 但是,在执行完全相同的操作之前,我已经编写了代码。

It has to be some minute detail, but I've been racking my brain for a while on this problem. 它必须是一些细节,但是我已经花了一段时间来解决这个问题。 Where am I going wrong? 我要去哪里错了?

The array should be an instance variable; 该数组应该是一个实例变量; it is currently a local in the constructor. 当前是构造函数中的本地对象。

Local variable declaration: 局部变量声明:

//CONSTUCTOR///////////////////////////
public Bag() {

T[] bagarray = (T[])new Object[10];

}

Member variable declaration: 成员变量声明:

T[] bagarray;
//CONSTUCTOR///////////////////////////
public Bag() {

bagarray = (T[])new Object[10];

}

You want the latter of the two. 您需要两者中的后者。

Java 矢量<object> class方法访问<div id="text_translate"><p>我正在尝试使用矢量来举办我的课程。 这些类继承了另一个 class 的方法,也有自己的方法。 我遇到的问题是使用带有对象的向量来调用 object 中 class 的方法。我认为它会是这样的:</p><pre> public static void vSort(Vector<Object> vector) { vector[0].generate(); }</pre><p> 生成是我与学生 class 在 object 中创建的自定义方法。</p><p> 一个更好的例子</p><pre>public class Method { protected String name; public void method() { // some code } } public class Student extends Method { protected String last; public void setUp() { // some code } } public class Main { public static void main(String[] args) { Vector<Object> vector = new Vector<Object>(); Student stu = new Student(); // pretend this generates something vector.add(stu); }</pre><p> 我遇到的问题是有很多像学生这样建立在方法上的课程。 如果我不能使用 Object 这对我来说没问题,但我需要访问方法 class 中的代码。</p></div></object> - Java vector<object> class method access

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从另一个 Java 类访问方法 - Cannot access method from another java class 从Java中该类的对象中访问类变量 - access a class variable from within an object in that class in java Java - 无法从子类对象访问抽象方法 - Java - Cannot Access Abstract Method from Subclass Object Android | JAVA | 无法访问方法返回的变量 - Android | JAVA | Cannot Access Variable returned by a method Java无法访问内部类中的受保护变量 - Java cannot access a protected variable in inner class Java,无法访问同一类中的方法 - Java, cannot access method within same class 无法访问 object 作为 class 变量的实例方法 - no access to instance method of an object as a class variable java中其他class的方法如何访问变量 - How to access the variable in the method of other class in java Java 矢量<object> class方法访问<div id="text_translate"><p>我正在尝试使用矢量来举办我的课程。 这些类继承了另一个 class 的方法,也有自己的方法。 我遇到的问题是使用带有对象的向量来调用 object 中 class 的方法。我认为它会是这样的:</p><pre> public static void vSort(Vector<Object> vector) { vector[0].generate(); }</pre><p> 生成是我与学生 class 在 object 中创建的自定义方法。</p><p> 一个更好的例子</p><pre>public class Method { protected String name; public void method() { // some code } } public class Student extends Method { protected String last; public void setUp() { // some code } } public class Main { public static void main(String[] args) { Vector<Object> vector = new Vector<Object>(); Student stu = new Student(); // pretend this generates something vector.add(stu); }</pre><p> 我遇到的问题是有很多像学生这样建立在方法上的课程。 如果我不能使用 Object 这对我来说没问题,但我需要访问方法 class 中的代码。</p></div></object> - Java vector<object> class method access 无法从另一个类访问变量 - Cannot Access Variable From Another Class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM