从另一个类访问变量时出错

时间:2017-05-16 13:05:16

标签: java android

我正在尝试使用另一个类的变量。但是,它在以下代码中显示错误:

// namespace unserializer
function reconstructNamespace(ns: any, serialized: string): void {

        // closure as TS is doing it will be needed
  let closure: string = "";
  let exports: string = "";

  // this always sucks... create a string representation of the closure as TypeScript is doing
  function stringifyObject(obj, isObject: boolean = false) {

     for (let key in obj) {

       let objKeyType: string = typeof(obj[key]);

                 if (objKeyType === "string" && obj[key].substr(0, __UNSERIALIZE.length) === __UNSERIALIZE) {

          let valType = obj[key].substring(obj[key].indexOf("<") + 1, obj[key].indexOf(">"));
          let val = obj[key].substr(obj[key].indexOf(">") + 1);

          switch (valType) {

             case "Date":
                closure += (isObject ? key + ": " : "var " + key + " = ") + "new Date(\"" + val + "\")";
                closure += (isObject ? ",\n" : ";\n");
                break;

             case "Function":
                closure += (isObject ? key + ": " : "var " + key + " = ") + val + "\n";
                break;

             default:
                 throw new Error("Invalid type");
                 break;

          }

         } else if (objKeyType === "string") {
          closure += (isObject ? key + ": " : "var " + key + " = ") + "\"" + obj[key] + "\"";
          closure += (isObject ? ",\n" : ";\n");
       } else if (objKeyType === "number" || objKeyType === "boolean") {
          closure += (isObject ? key + ": " : "var " + key + " = ") + obj[key];
          closure += (isObject ? ",\n" : ";\n");
       } else if (objKeyType === "object") {
          closure += (isObject ? key + ": " : "var " + key + " = ") + " {\n";
          stringifyObject(obj[key], true);
          if (closure[closure.length - 2] === ",") {
            closure = closure.slice(0, -2) + "\n}\n";
          } else {
            closure += "};\n";
          }
       }

     }

  }

这是吸气剂:

public class ItemDetailActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState, String[] args) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_item_detail);
      Toolbar toolbar = (Toolbar) findViewById(R.id.detail_toolbar);
      setSupportActionBar(toolbar);
      DummyContent Position = new DummyContent();
      int picCheck = Position.getPos();
    }
}

你能告诉我这里的问题是什么吗?

错误:

  

错误:(31,32)错误:类DummyContent中的方法getPos无法应用于给定类型;

     

必需:int

     

发现:没有参数

     

原因:实际和正式的参数列表长度不同

1 个答案:

答案 0 :(得分:4)

下面:

int picCheck = Position.getPos();

打算使用:

public int getPos(int position)

请注意,一行需要一个int参数;并且另一条线不能给出一条。

这里的真正的答案是:编译器消息已经告诉你了。 Required int, found: no arguments

含义:java编译器消息大多数时候都很容易阅读。

因此,答案超出了简单的遗留问题:如果您的Java技能处于难以理解此类消息的水平,那么您现在很可能过度负担做Android编程。