Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

is there any way pass arguments to class method without overloading ?  RSS feed

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all
i have simple class say : ( note this is not valid java syntax only to explain my problem )
public class Foo{

private String s = "";

private String ss = "";

private int i = 0;


public String get(somekind Of Type arg[]){

s = arg[0];

ss = arg[1];

i = arg[2];

return "Done";

}


}

now the function call

Foo foo = new Foo();

foo.get();

foo.get("my");

foo.get("my","name");

foo.get("my,"name",1);



now i know if i would like it to work i had to do in my class method overloading , but is there any way to avoid it?

you see the variables already have default values so if they don't get filled with the method args they will have something .

thanks
 
Dana Bothner-By
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you adverse to overloading? This is a situation where it makes sense:
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Meir Yan :
you see the variables already have default values so if they don't get filled with the method args they will have something .


Since in the method argument, you are changing the value of each instance variables, why don't you assign them in constructor by overloading the constructor.




Naseem
[ August 04, 2006: Message edited by: Naseem Khan ]
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well this is one of my many problems im facing to convert some in house server scripting lang to jsp
this involved allot of pages to convert ( 1000 +) and im trying to avoid how much then i can to do modification to the original code .. any way in this in house script land i can call function like that :

fn.myFunc
or
fn.myFunc("name");
or
fn.myFunc("name",1,3);
and so on ....
this function can take up to 10 arguments or less , im trying to avoid the need to write 10 functions in my class for every types .
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
avoid how much then i can to do modification to the original code
Why? Scripting languages aren't Java. The little scripting I have done, I get the impression that scripting languages are not even object oriented. So why are you translating the old system? Have a good look at its functionality and then decide that translating the old system really is easier than writing a new fully OO system.
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why not, the impression i got from jsp that i can implant OO and java principles
this jsp do have strong possibilities not like simple scripting lang
i did build complex class's already in jsp .
 
Dana Bothner-By
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Meir Yan:
well this is one of my many problems im facing to convert some in house server scripting lang to jsp.


My friend, you may get much citicism because of that statement. Badly written code often happens when people try to do it all in JSP. Ideally, you should be separating the presentation (JSP) from the rest of the application code. So are you? If not, things can get messy.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!