Java -- referencing a method in different class
I am coming from C programming, so I m new to the OOP concept. My question is this:
I am trying to test a bunch of 开发者_开发百科modules, so I decided to write a wrapper class for the common functionality, so I can call these methods from other classes. But where I am confused is if I make the other classes extends
from myWrapperClass
, I cannot still use the methods of the wrapper classes unless they are defined static
(and most tutorials says try to stay away from static if possible) or I need to create a new instance of the wrapper class using new
.
Then what is the point of using extends
? If that is the way I have to go, is there any short cut using them instead of writing myclass.wrapperclassfunction()
each time?
Thanks in advance.
so I can call these methods from other classes. but where I am confused is. if I make the other classes extends from myWrapperClass, I cannot still use the methods of the wrapper classes unless they are defined static
This is false.
You can absolutely do this.
public class MyWrapperClass {
public void commonMethod() {
}
}
public class YourClass extends MyWrapperClass {
public void anotherMethod() {
commonMethod();
// other stuff
}
}
If the common functionality is identical methods in various modules (subclasses), then you can just factor it out of the modules into the common superclass and just have the method defined once there. If the method is implemented in a different way in every subclass but you want to call it in the same way regardless of which subclass instance you have, then you can declare it as abstract
in the superclass and have an implementation in each subclass. If it's something in between — all but a few subclasses have the same implementation — then you can factor out the most common implementation into the superclass and override it in the exceptional subclasses.
[edit summary of issue of OP]:
You need to review the distinction between primitive and object types in Java.
A primitive type can simply be declared and used:
int f; // Java (or is c? :)
f = 10;
An object type must be allocated. (For C programmers: Java objects are dynamically allocated. Think heap, not stack.)
Foo myFoo = new Foo(); // Java
struct foo *myFoo = (struct foo *) malloc(sizeof(struct foo)); // good old C
-- initial answer --
Your point of confusion is this:
Reading between the lines, it seems that you think that by merely informing the type system that an extension of the base exists, you should be able to magically start calling methods on that type.
And this is why you have settled on static methods: they don't require creating an instance -- something which apparently you refuse to do!
Let's say you have something along the lines of Kai's answer (which is correct btw) above.
Then:
/** using Kai's classes */
public void foobar() {
YourClass foo = new YourClass();
foo.commonMethod(); // try it ..
}
Review what you must know of simply defining and using a struct
in C
.
Now forget this:
typedef struct {
int foo;
char bar[3];
} extended;
extended s; // this is not just declaring s; it is also allocating it
s.foo = 10; // hey, no malloc. nasty Java insists on new ..
In Java, when you say:
SomeClass s; // s declared but not instantiated (think 'allocated')
you must then instantiate the object.
s = new SomeClass();
make sure you declare your wrapperclassfunction is not private in your wrapperclass.
public class wrapperClasss{
private wrapperMethod(){}
}
public class Myclass extends wrapperClasss{
private myMethod(){
wrapperMethod();//this is a compile erro
}
}
精彩评论