|
|
![]() |
| Thread Tools | Search this Thread |
#1
| |||
| |||
Static method in Abstract class Hello, Firstly, I understand the reasons why an interface or abstract class (in the NET / C # / java terminology) can not have abstract static methods. My question is more focused on the best design solution. What I want is a set of helper classes that all have their own methods such as static, if I get the objects A, B and C from a third party vendor, I can have classes assistance with methods such as Code: AHelper.RetrieveByID (string id); AHelper.RetrieveByName (string name); AHelper.DumpToDatabase (); Code: AHelper AHelper a = new (); a.DumpToDatabase (); |
#2
| |||
| |||
Re: Static method in Abstract class If I were you I would try to avoid static. IMHO I always ended up a kind of synchronization problems on the road with the static. Having said that you have a classic example of generic programming using jigs. I will adopt the solution based on models presented in one of the java modules. |
#3
| |||
| |||
Re: Static method in Abstract class In you case the solution will be this code Code: public static T RetrieveByID <T> (string iden) (Var flnm = getFieldNamesBasedOnType (typeof (T)); qyres webservice.query qy = ("SELECT" + flnm + "FROM" Tyepof + (T). Name + "WHERE iden = '" + iden + "'"); return (T) qy.records [0]; ) |
#4
| |||
| |||
Re: Static method in Abstract class Given your response I think as follows: - You could just have a static method that takes a type parameter and performs the usual logic based on gender. - You can create a virtual method in your abstract base, where you specify the SQL in the concrete class. While containing all the common code that is required by both (eg the execution of the command and return the property) while encapsulating the specialist bits (eg SQL) in subclasses. I prefer the second option, although its course down to you. If you need me to go into details, please let me know and I'll be happy to edit / update. |
#5
| |||
| |||
Re: Static method in Abstract class You can not overload methods by varying only the type of return. You can use different names: Code: static Aobj GetAObject (string iden); static Bobj GetBObject (string iden); Code: AorBobj class (String iden; AorBobj (string iden) (this.iden = iden;) static public AorBobj RetrieveByID (string iden) (Return new AorBobj (iden); ) public static explicit operator Aobj (AorBobj ab) AObjectQuery (return (ab.iden); ) public static explicit operator Bobj (AorBobj ab) BObjectQuery (return (ab.iden); ) ) Code: var a = (Aobj) AorBobj.RetrieveByID (8); var b = (Bobj) AorBobj.RetrieveByID (8); |
#6
| |||
| |||
Re: Static method in Abstract class How are Objects and AHelper related? Is AHelper.RetrieveByID (), the same logic as BHelper.RetrieveByID (). If yes, why not approach a utility class (class with public static methods only and no state) static [return type] Helper.RetrieveByID (ObjectX x). |
#7
| |||
| |||
Re: Static method in Abstract class Personally, I might be wondering why each type of need a static method before even thinking again Why not create a utlity class with static methods they need to share? (Eg ClassHelper.RetrieveByID (string id) or ClassHelper <ClassA>. RetrieveByID (string id) In my experience with these kinds of "roadblocks" the problem is not the limits of language, but the limits of my design. |
![]() |
|
Tags: abstract class, java, programming language, static method |
Thread Tools | Search this Thread |
|
![]() | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Problem with abstract class and interface | Vodka | Software Development | 5 | 08-03-2010 12:20 PM |
Abstract class method overloading problem | ISAIAH | Software Development | 5 | 16-01-2010 10:18 AM |
Difference among concrete class and abstract class | Roxy_jacob | Software Development | 4 | 07-12-2009 01:22 PM |
Abstract class in Java | SoftWore | Software Development | 3 | 07-11-2009 01:58 PM |
Abstract class and Interface in .net | RogerFielden | Software Development | 3 | 04-05-2009 06:07 PM |