Home > Archive > Java Help > January 2005 > Interface
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| elrond_III 2005-01-27, 4:03 pm |
| With interface it is often meant that you should list all your method
signitures!
method signature: the mathod name, its arguments, and the return type,
usually in the following form:
+toStringCopy(str1:String,str2:String):S
tring
This is too called interface because with it you can see the way how you
can "communicate" with an object (means which public methods you can
call...).
however it is enougth if you write your public methods:
Mean something like:
class B {
B()
public void m1()
public void m2(name:String)
//bla bla bla
}
PS.:
If you don't allready know:
In Java a interface is a class with just abstract methods, it is used to
force classes to implement a specific set of methods. Because then you
know a class has them, you can call this class with the type and
functions of the interface:
interface A{
abstract public void a();
abstract public int b();
}
class C implements A {
}
class D {
public static void main() {
C c = new C();
E.handleA(c); <--- i give E a object of type C
}
}
class E {
public static void handleA(A temp) { <-- but E just can handle type A
temp.a(); thats not an problem, since
temp.b(); C implements A...
}
}
--
--------------------------------------------------------------
Elrond_III -==: Hack the World :==-
g:R->R; [a,b] in D(g) and g[a,b] in [a,b] and for all x1,x2 in [a,b]
|g(x1)-g(x2)|<=L*|x1-x2| with L <= 1 then one s in [a,b] exist with
s=g(s)
-"Banachscher Fixpunktsatz"
| |
| thufir 2005-01-29, 3:57 am |
| elrond_III wrote:
[..]
> PS.:
> If you don't allready know:
> In Java a interface is a class with just abstract methods, it is used
to
> force classes to implement a specific set of methods. Because then
you
> know a class has them, you can call this class with the type and
> functions of the interface:
[..]
What I've got is: "public class Sword implements weapon{" so that in
class Monster there's a "List<Weapon> weapons = new List<Weapon>;"
which gives some flexability. Then a Sword instance can be changed by
iterating through the list and calling methods on an element. I'm not
sure if this is a common, or even "good", technique, though.
--
Thufir Hawat
| |
|
| > What I've got is: "public class Sword implements weapon{" so that in
> class Monster there's a "List<Weapon> weapons = new List<Weapon>;"
> which gives some flexability. Then a Sword instance can be changed by
> iterating through the list and calling methods on an element. I'm not
> sure if this is a common, or even "good", technique, though.
> --
> Thufir Hawat
>
It is good programming practice, in my experience, to use the abstraction of
a class if that is all you need. It is related to never downcasting from the
abstract type to the actual type to get something done.
It is better for example to use a java.util.List instead of a Vector,
ArrayList, LinkedList, or other implementation so you can alter performance
in the list initialization and not have to alter the many places in your
code you pass that instance. If all you are using are List's things (or
Weapon's in your case) then treat a Sword and whatever other Weapon
implementations you have as Weapons in the list, which you will probably
instantiate like:
List<Weapon> weapons = new ArrayList<Weapon>();
If you find yourself needing a part of Sword that is not in Weapon from an
object in that list, consider whether it should be known to Weapon and
therefore to all implementing classes of Weapon, or just to the specific
implementation of Sword.
I hope I wasn't too confusing,
--Paul
|
|
|
|
|