質問

I'm porting some code from Java (I know nothing about this language) to C. By the fact that Java is a C-like language, I have no problem in converting most statements. But I have no idea what some parts of the code mean. It calls a java class as a function and pass as parameter:

Assume the classes to be:

public class foo { 
    public foo(Typex x) { //etc }
}

public class baa {
    public baa(Typex x) { //etc }
}

From another class it's called as: new foo(baa())

What does it mean?

役に立ちましたか?

解決

This is wrong ! new foo(baa())

You cannot do this in Java, instead what you need to do is

new foo(new baa().bar()) . 

This means you first create a reference (Object) of baa and call bar() method of that reference. Remember new keyword in Java is to create a new reference out of a class. It calls a Constructor method of the class and allocates memory for that reference.

Further in above case it passes whatever returned from bar() method as an argument to the foo class and in turn create a reference of foo class too.

This is a good start : [1]: http://docs.oracle.com/javase/tutorial/java/javaOO/index.html

他のヒント

It's called a constructor. See here

public class Foo { 
   public foo(Typex x) { //etc }
}

public class Baa {
 public baa(Typex x) { //etc }
}

Foo f = new Foo(x);  // Creates a new instance of Foo.
Baa b = new Baa(x);  // Creates a new instance of Baa.

It's a constructor - this little thing that makes creating new object easier. You can do

Object Name = new Object(Param1, Param2);

instead of

Object Name = new Object();
Name.Param1 = foo;
Name.Param2 = bar;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top