Now, all of this is fine and works well.
There's no bug in here, but there's nothing in there which prevents misuse of my class.
For instance, nothing prevents me from entering numbers in here which make no sense at all.
For instance, I could enter -1 as the hour or -2 as the minutes or -3 as the seconds.
This is not a valid time or we would have to come up with
entirely new definition of what the valid time is.
However, we can still instantiate such an object and even printed out comes as -1, -2, -3.
So here we're still talking three numbers, but since Python does not provide static checking of types,
that is checking of types when the program is being compiled or rather executed,
there's noting that prevents me from passing objects which have a completely different type.
So I could pass a string in here for instance and then hours would be initialized with the string--
this means that I would get a time object in here, which is entirely invalid.
So my situation is--I'm passing some string here in the hours attribute.
And now for a quiz, so if I do this, do I get an error when initializing the time object
or when printing the time object or never.
Ora, tutto questo va bene e funziona.
Non ci sono bug qui dentro, ma non c'e' nulla che impedisca un uso improprio della mia classe.
Per esempio, niente mi impedisce di inserire numeri che non abbiano molto senso,
per esempio potrei mettere -1 nelle ore o -2 nei minuti o -3 nei secondi.
Questo non e' un orario valido oppure dovremo stabilire
un concetto completamente nuovo di cosa sia un orario valido.
Comunque, potremmo instanziare un tale oggetto e stamparlo a video con -1,-2,-3.
Percio' qui stiamo parlando di 3 numeri, ma dato che Python non offre una verifica statica sui tipi,
cioe' una verifica sui tipi in fase di compilazione invece che in esecuzione,
non c'e' nulla che mi impedisca di far passare oggetti che siano di tipo completamente diverso.
Cosi' io potrei per esempio passare una stringa qui e le ore verrebbero inizializzate con la stringa,
nel senso che io potrei avere un oggetto Time qui che sia completamente errato.
Quindi il mio problema e' questo: sto passando una stringa qui nell'attributo hours.
Ed ora il quiz: dunque se faccio cosi' otterro' un errore, inizializzando l'oggetto Time,
o quando stampero' l'oggetto Time, oppure mai?
これで すべてが素晴らしく機能します
ここにはバグがありません
しかしクラスの誤用を防ぐものは何もありません
たとえば私が無意味な数字を入力しようとしても
誰も邪魔をしません
たとえばhoursを-1としてminutesを-2とし
secondsを-3と入力することもできます
これは有効な時間ではないので
有効な時間の定義を考え出さなければなりません
しかし このようなオブジェクトのインスタンスを
作成できますし
-1:-2:-3となるように出力もできます
3つの数字について話していますが
まだPythonはtypeの静的チェックを提供していません
これがプログラムをコンパイルまたは
実行した時のtypeをチェックします
私はまったく異なるtypeを持ったオブジェクトを
自由に渡せるので
この文字列をインスタンスに渡せますし
hoursをこの文字列で初期化できます
つまり ここでまったく無効なTimeオブジェクトを
得るということです
この状況はhours属性のここに
文字列を渡していることになります
小テストです
私が以上のようにしたら
エラーが出るのは時間を初期化した時でしょうか?
それともTimeオブジェクトを出力した時でしょうか?
またはエラーなしですか?
现在一切运行顺利。
没有bug了,但还没有机制来防止对这个类的错误使用。
即没有防范输入无效数字的机制。
比如,我输入小时是 -1,分钟-2,秒 -3,
这个不是有效时间,我们需要再给出
新的有效时间的定义。
然而这样一个对象仍值得一提,它可以输出成-1:-2:-3
这参数仍然是三个数字,但因为python不支持静态的类型检查,
只有程序编译或执行时才能检查,
所以没法防止我传递不同类型的对象。
比如说我可以传递个字符串,然后初始化hours=它,
意味着我得到了一个完全无效的时间对象。
现在问题来了,我传递了一些字符串给hours这些属性。
小测下,如果这样做,是在初始化时间对象时出错呢,
还是在打印时出错,又或是没错?