Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > it.comp.java > #9329

Re: perchè non funziona questo cast?

Path csiph.com!aioe.org!.POSTED.KClibdFDvSdkz/QY4hOaKw.user.gioia.aioe.org!not-for-mail
From "Dr.UgoGagliardelli" <do.not.spam@me.please>
Newsgroups it.comp.java
Subject Re: perchè non funziona questo cast?
Date Sun, 8 Sep 2019 08:39:25 +0200
Organization Aioe.org NNTP Server
Lines 52
Message-ID <ql27pb$1f0o$1@gioia.aioe.org> (permalink)
References <qkuaje$e5i$1@gioia.aioe.org> <qkvlfu$9eu$1@gioia.aioe.org> <ql0uu5$2mp$1@gioia.aioe.org>
NNTP-Posting-Host KClibdFDvSdkz/QY4hOaKw.user.gioia.aioe.org
Mime-Version 1.0
Content-Type text/plain; charset=iso-8859-15; format=flowed
Content-Transfer-Encoding 8bit
X-Complaints-To abuse@aioe.org
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0
Content-Language it-IT
X-Antivirus-Status Clean
X-Antivirus Avast (VPS 190907-4, 07/09/2019), Outbound message
X-Notice Filtered by postfilter v. 0.9.2
Xref csiph.com it.comp.java:9329

Show key headers only | View raw


Il 07.09.2019 21.03, Gioacchino ha scritto:
> Il 07/09/2019 09:14, Dr.UgoGagliardelli ha scritto:
>> Il 06.09.2019 21.04, Gioacchino ha scritto:
>>> Metodo (List <? T super Number>  numbers) ...
>>>
>>>
>>> poi in un ciclo:
>>>
>>> for ( Number n : numbers )
>>>
>>> etc
>>>
>>> e mi da errore, mi dice type mismatch e che non può castare (? super 
>>> number) to number , e mi propone
>>>
>>> for ( Object n : numbers )
>>>
>>>
>>> Certamente object è una superclasse, ma non posso usare la stessa 
>>> classe Number?
>> Ebbene no. Hai una lista che ammette le superclassi di Number, quindi 
>> solo Object.
> 
> 
> quindi Object e Number si possono assegnare a Object ,
> ma Object non si può assegnare a Number , giusto?
Ni.
L'espressione for ( Object n : numbers ) usa l'iteratore di numbers, 
ogni n dovra' essere un T di numbers, ovvero:
List<Number> numbers;
oppure
Number[] numbers;
ammettono sia:
for ( Object o : numbers )
che:
for ( Number o : numbers )
in entrambi i casi puoi inserire in numbers anche un oggetto che estenda 
Number.
Mentre puoi sempre fare un cast ad un tipo ammesso che che l'istanza di 
cui fai il cast estenda quel tipo.
Ad esempio:
for ( Object o : numbers ) {
   Number n = (o istanceof Number)?(Number)o:null;
ma anche:
   Number n = (o istanceof Integer)?(Integer)o:null;
oppure:
   Integer n = (o istanceof Integer)?(Integer)o:null;

Viceversa l'istanza di qualsiasi classe puo' sempre essere assegnata ad 
un Object, in virtu' del fatto che in Java la gerarchia di qualsiasi 
oggetto discende da Object.

Back to it.comp.java | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

perchè non funziona questo cast? Gioacchino <vannnnia1991@gmail.com> - 2019-09-06 21:04 +0200
  Re: perchè non funziona questo cast? Gioacchino <vannnnia1991@gmail.com> - 2019-09-06 21:06 +0200
    Re: perchè non funziona questo cast? "Dr.UgoGagliardelli" <do.not.spam@me.please> - 2019-09-07 09:42 +0200
  Re: perchè non funziona questo cast? "Dr.UgoGagliardelli" <do.not.spam@me.please> - 2019-09-07 09:14 +0200
    Re: perchè non funziona questo cast? Gioacchino <vannnnia1991@gmail.com> - 2019-09-07 21:03 +0200
      Re: perchè non funziona questo cast? "Dr.UgoGagliardelli" <do.not.spam@me.please> - 2019-09-08 08:39 +0200
        Re: perchè non funziona questo cast? Gioacchino <vannnnia1991@gmail.com> - 2019-09-08 12:36 +0200
  Re: perchè non funziona questo cast? 4ndre4 <4ndre4@4ndre4.invalid> - 2019-09-12 22:28 +0100
    Re: perchè non funziona questo cast? "Dr.UgoGagliardelli" <do.not.spam@me.please> - 2019-09-13 09:25 +0200
      Re: perchè non funziona questo cast? 4ndre4 <4ndre4@4ndre4.invalid> - 2019-09-14 12:33 +0100
        Re: perchè non funziona questo cast? "Dr.UgoGagliardelli" <do.not.spam@me.please> - 2019-09-16 14:51 +0200
    Re: perchè non funziona questo cast? Gioacchino <vannnnia1991@gmail.com> - 2019-09-13 15:44 +0200
      Re: perchè non funziona questo cast? 4ndre4 <4ndre4@4ndre4.invalid> - 2019-09-14 12:34 +0100
        Re: perchè non funziona questo cast? Gioacchino <vannnnia1991@gmail.com> - 2019-09-17 00:52 +0200
          Re: perchè non funziona questo cast? 4ndre4 <a.laforgia@gmail.com> - 2019-09-20 04:26 -0700
            Re: perchè non funziona questo cast? Gioacchino <vannnnia1991@gmail.com> - 2019-09-20 18:19 +0200

csiph-web