• Keine Ergebnisse gefunden

Aufgabe3Permutations Aufgabe2TypeErasure Aufgabe1Covariance & Contravariance HinweiseundBemerkungenzum5.ÜbungsblattderVorlesung„FortgeschritteneAspekteobjektorientierterProgrammierung(SS2008)“

N/A
N/A
Protected

Academic year: 2022

Aktie "Aufgabe3Permutations Aufgabe2TypeErasure Aufgabe1Covariance & Contravariance HinweiseundBemerkungenzum5.ÜbungsblattderVorlesung„FortgeschritteneAspekteobjektorientierterProgrammierung(SS2008)“"

Copied!
2
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Prof. Dr. A. Poetzsch-Heffter Dipl.-Inform. Markus Reitz

Technische Universität Kaiserslautern Fachbereich Informatik AG Softwaretechnik

Hinweise und Bemerkungen zum 5. Übungsblatt der Vorlesung

„Fortgeschrittene Aspekte objektorientierter Programmierung (SS 2008)“

Aufgabe 1 Covariance & Contravariance

a) In Java verhalten sich die in einer throws-Klausel aufgeführten Exceptions kovariant. Als Besonderheit dürfen die in einem Supertyp im Rahmen einer throws-Klausel definierten Exceptions in einem Subtyp teilweise oder vollständig gestrichen werden.

Der Grund für diese Definition läßt sich anhand des folgenden Codefragments veranschaulichen

. . . t r y {

. . . }

c a t c h( E x c e p t i o n ex ) {

. . . }

. . .

Der oben angegebene try-catch Block „fängt“ alle Exceptions vom Typ Exception, sowie von Exception abgeleitete Typen wie beispielsweise IOException. Angenommen, innerhalb des try-Blocks wird eine Methode m aufgerufen, welche die Exception vom Type Exception erzeugt. Wird diese Methode in einem Subtyp durch eine Methode überschrieben, welche eine IOException erzeugt, so ist der obige try-catch Block weiterhin – wie vom Programmierer intendiert – funktionstüchtig. Falls nicht die zuvor genannte Kovarianz-Einschränkung gelten würde, ist dies im Allgemeinen nicht der Fall.

b) Durch den Einsatz von Checked Exceptions wird garantiert, daß eine auftretende Ausnahmesituation „vor Ort“

behandelt oder aber deren Behandlung explizit an den Aufrufer weiterdelegiert wird. Die Einhaltung dieser Be- dingung kann während der Übersetzungsphase durch den Compiler überprüft und somit auch erzwungen werden.

Durch die im Rahmen der throws-Klausel deklarierten Exceptions wird dem Nutzer der betre ff enden Methode explizit klar gemacht, um welche Ausnahmesituationen er sich kümmern muß.

Aufgabe 2 Type Erasure

a)

p u b l i c c l a s s C o m p a r a b l e T e s t i m p l e m e n t s C o m p a r a b l e { p u b l i c i n t compareTo ( O b j e c t a ) { r e t u r n(−1 ) ; } p u b l i c i n t compareTo ( O b j e c t b ) { r e t u r n( 1 ) ; } p u b l i c s t a t i c v o i d main ( S t r i n g a r g s [ ] ) {

C o m p a r a b l e T e s t C=new C o m p a r a b l e T e s t ( ) ; }

}

b) Die Methode public int compareTo(Object) würde doppelt mit unterschiedlichem Rückgabewert definiert.

Aus diesem Grund ist eine Übersetzung des angegebenen Quellcodes nicht möglich.

Aufgabe 3 Permutations

a)

i m p o r t j a v a . u t i l .∗;

i m p o r t s t a t i c j a v a . u t i l . A r r a y s .∗;

p u b l i c c l a s s P e r m u t a t o r<T> i m p l e m e n t s I t e r a b l e<C o l l e c t i o n<T>> { p r i v a t e f i n a l C o l l e c t i o n<? e x t e n d s T> d a t a ;

p u b l i c P e r m u t a t o r ( C o l l e c t i o n<? e x t e n d s T> c o l l e c t i o n ) { d a t a=c o l l e c t i o n ;

}

(2)

p u b l i c I t e r a t o r<C o l l e c t i o n<T>> i t e r a t o r ( ) { r e t u r n(new I t e r a t o r<C o l l e c t i o n<T> >() {

p r i v a t e I t e r a t o r<I t e r a b l e<I n t e g e r>> p e r m u t a t i o n s=new P e r m u t a t i o n s ( d a t a . s i z e ( ) ) . i t e r a t o r ( ) ; p u b l i c b o o l e a n h a s N e x t ( ) {

r e t u r n( p e r m u t a t i o n s . h a s N e x t ( ) ) ; }

@ S u p p r e s s W a r n i n g s ( " u n c h e c k e d " ) p u b l i c C o l l e c t i o n<T> n e x t ( ) {

i f ( ! h a s N e x t ( ) )

throw new U n s u p p o r t e d O p e r a t i o n E x c e p t i o n ( ) ; T [ ] r e s u l t=(T [ ] )new O b j e c t [ d a t a . s i z e ( ) ] ;

I t e r a t o r<? e x t e n d s T> i t e r a t o r=d a t a . i t e r a t o r ( ) ; f o r( I n t e g e r i n d e x : p e r m u t a t i o n s . n e x t ( ) )

r e s u l t [ i n d e x ]=i t e r a t o r . n e x t ( ) ; r e t u r n( a s L i s t ( r e s u l t ) ) ;

}

p u b l i c v o i d remove ( ) {

throw new U n s u p p o r t e d O p e r a t i o n E x c e p t i o n ( ) ; }

} ) ; }

p u b l i c s t a t i c v o i d main ( S t r i n g . . . a r g s ) {

f o r( C o l l e c t i o n<S t r i n g> c u r r e n t : new P e r m u t a t o r<S t r i n g>( a s L i s t ( "A" , "B" , "C" , "D" ) ) ) S y s t e m . o u t . p r i n t l n ( c u r r e n t ) ;

} }

b) Besprechung in der Übungsstunde.

2

Referenzen

ÄHNLICHE DOKUMENTE

For a given collection of elements of an arbitrary type T, the Permutator class provides an iterator over all permutations of the elements contained in the collection.

} idiom used within the next() method in the source code of class Permutations. Which value is returned by the following code fragment?.. b) Because of the type erasure

add ( new MemoryProblem ( ). the memory error does not occur and the list whose reference is stored in the variable result is populated with the fragments according to the given

Check the following source code fragments with respect to violation or fulfillment of the Behavioural Subtype property as introduced in the lecture. What is the complete

C5: Diese Regel ist erfüllt, da die Klasse StringDataSource keinen Konstruktor besitzt, und der Konstruktor ihrer Superklasse java.lang.Object ist leer (http: // www.docjar.com / html

Technische Universität Kaiserslautern Fachbereich Informatik AG Softwaretechnik. Hinweise und Bemerkungen

Technische Universität Kaiserslautern Fachbereich Informatik AG Softwaretechnik. Hinweise und Bemerkungen

catch :: Exception γ⇒ IO α → (γ→ IO α) → IO α try :: Exception γ⇒ IO α → IO (Either γ α) I Faustregel: catch für unerwartete Ausnahmen, try für erwartete I