Solar @ Dongen: http://solar.searchy.net/ - Penpal International: http://ppi.searchy.net/
Verwijderd
Zijn generics niet wat je zoekt?
http://www.deitel.com/art...uctionToJavaGenerics.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| public abstract class CommonObject { public abstract CommonObject Function(); } public class NotSoCommonObject extends CommonObject { public CommonObject Function(){ return NotSoCommonObject(); } } NotSoCommonObject obj = new NotSoCommonObject(); obj = (NotSoCommonObject)obj.Function(); |
Maar waarom moet je later nog casten naar de sub-class van CommonObject. Je zegt dat je alleen dingen uit de CommonObject class gebruikt, waarom gebruik je dan ook niet overal dat type?
[ Voor 19% gewijzigd door Woy op 28-04-2009 16:57 ]
“Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.”
Heb je ook een stukje code hiervan? Ik kan me namelijk nogal slecht een voorstelling maken van het beschreven probleem. In java is het bij het overerven van een methode gewoon mogelijk om een 'narrower' returntype op te geven. De volgende code werkt gewoon:Keiichi schreef op dinsdag 28 april 2009 @ 16:25:
Dit heb ik gedaan, en Class_1.function() gecalled en gecast. Hierop kreeg ik een ClassCastExption.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| public class Super { public Super function(){ //doStuff return null;// return iets van super } } public class Child extends Super{ @Override public Child function() { //do stuff return null; // return iets van child } } |
Ken Thompson's famous line from V6 UNIX is equaly applicable to this post:
'You are not expected to understand this'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| public abstract class CommonObject<E extends CommonObject> { public abstract <E> function(); } public class NotSoCommonObject extends CommonObject<NotSoCommonObject> { public CommonObject Function(){ return NotSoCommonObject(); } } NotSoCommonObject obj = new NotSoCommonObject(); obj = obj.Function(); |
'Nae King! Nae quin! Nae Laird! Nae master! We willna' be fooled agin!'
Ken Thompson's famous line from V6 UNIX is equaly applicable to this post:
'You are not expected to understand this'
Dit is een voorbeeld code hoe het nu in elkaar zit:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| class CommonObject { CommonObject() { } public CommonObject function() { CommonObject tmp = new CommonObject(); // Do something with tmp return tmp; } public String AnotherFunction() { return "Something specific done to CommonObject attributes"; } } class Child_1 extends CommonObject{ Child_1() { super(); } } class CommonTest { public static void main(String args[]) { Child_1 c1 = new Child_1(); Child_1 another_instance = (Child_1)c1.function(); System.out.println("Hello! " + another_instance.AnotherFunction()); } } |
Dit is een testcase op basis van waarmee ik bezig ben, maar dit zou wel het idee moeten schetsen. Het idee lijkt ontzettend onlogisch, maar het waarom is terug te vinden in de grote code, schiet me daar dus niet op af
in function() gebeuren dus alleen dingen die betrekking hebben op attributen gedefineerd in 'CommonObject' en op basis daarvan moet dan een nieuwe Child_1 object worden gegeven. Er is natuurlijk altijd die optie op in alle child classes (Het zijn er in het echt iets van 10 stuks) function() te schrijven, maar vanwege eerder genoemd feit is het er niet de plaats voor lijkt me.
Solar @ Dongen: http://solar.searchy.net/ - Penpal International: http://ppi.searchy.net/
Ik blijf dus wel erg benieuwd naar 'de grote code' want ik ben erg benieuwd naar het waarom. Ik vermoed namelijk dat er een verkeerde oplossingsrichting gekozen is. Het komt op mij heel vreemd over dat een instantie van een object een instantie van dat object kan maken. Het lijkt nu een beetje alsof een object de factory van zichzelf is. De enige usecase die ik daar zo snel voor kan verzinnen is een clone(), maar dat zit al standaard in java.
Een oplossing zou trouwens kunnen zijn om het volgende te doen:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| public class Super { public Super constructAnotherInstance() { return new Super(); } public Super function(){ Super result = constructAnotherInstance(); //dostuff return result; } } public class Child extends Super{ public Child constructAnotherInstance() { return new Child(); } @Override public Child function() { Child result = (Child)super.function(); //do stuff return result; } } |
Ken Thompson's famous line from V6 UNIX is equaly applicable to this post:
'You are not expected to understand this'
Volgende keer zal ik m'n code weer even langs een compiler of IDE halen, dat was ik dit keer even vergeten.Janoz schreef op woensdag 29 april 2009 @ 09:22:
Je code is niet helemaal correct. In het NotSoCommonObject is de naam van de function fout en het returntype niet juist.
'Nae King! Nae quin! Nae Laird! Nae master! We willna' be fooled agin!'
.Net programmeurs
Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.