I'm going to trace name, friends and numFriends. I start at the first line,
create a new person with the name Sara. So name will be set to the name I
passed in. Friends is empty, numFriends is 0. And then we're done with the
constructor. We do the same thing for Chenghan. And now we add a friend to
Sara. So in addFriend, friends will get longer, it'll be friends plus Chenghan
and numFriends will go up by one. And then that method's done, so we go up to
the next line. In sara.unfriend, we shorten friends, we replace Chenghan in a
space with nothing. So friends goes back to being nothing and then we're done.
Something went wrong here, though. I clearly have no friends in the list here.
But here, my number of friends is still 1. We saw when that happened. In
unfriend, we updated the list of friends, but didn't update the number of
friends.
Voy a rastrear el nombre, amigos y número de amigos. Comienzo en la primera línea,
genero una nueva Persona con el nombre de Sara. El nombre se va a establecer al nombre
que pasé, amigos esta vacío, «numFriends» es 0. Y luego terminamos con el
constructor. Hacemos lo mismo para Cheng Han. Ahora le agregamos a un amigo a
Sara. Así que en «addFriend», la lista de amigos crecerá, y será friends más Cheng-Han
y «numFriends» incrementará por 1. Y luego este método se ha terminado, por lo que subimos a
la siguiente línea. En «sara.unfriend», reducimos la lista de amigos, reemplazamos a Cheng-Han y
un espacio con nada. Entonces «friends» vuelva a ser nada y entonces acabamos.
Aunque aquí algo salió mal. Es evidente que no tengo amigos en esta lista.
Sin embargo aquí, mi número de amigos sigue siendo 1. Vimos cuando eso sucedió. En
«unFriend», actualizamos la lista de amigos, pero no actualizamos el número de
amigos.
Je vais tracer name, friends
et numFriends.
Je commence à la première ligne,
crée une nouvelle personne
avec comme nom de Sara.
Donc, name sera défini sur
le nom que j'ai transmis.
Friends est vide, numFriends est 0
et puis nous en avons fini
avec le constructeur.
Nous faisons la même chose pour Cheng-Han.
Et maintenant nous ajoutons un ami à Sara.
Donc, dans addFriend,
friends devient plus long,
il deviendra friends + Cheng-Han
et numFriends montera d'une unité.
Cette méthode est terminée,
nous passons alors à la ligne suivante.
Dans sara.unfriend,
nous réduisons les amis,
nous remplaçons Cheng-Han et
un espace avec rien.
Donc, friends redevient rien
et nous avons fini.
Cependant, quelque chose
s'est mal passé ici.
Je n'ai clairement aucun
ami dans la liste ici.
Mais ici, mon nombre
d'amis est toujours 1.
Nous avons vu quand cela est arrivé.
Dans unfriend, nous avons
mis à jour la liste des amis,
mais n'avons pas mis à jour
le nombre d'amis.
name、friends、numFriendsをトレースします
1行目から始めましょう
Saraという名前のnew Personを作ります
nameには私が渡した名前がセットされます
Friendsは空でnumFriendsはゼロとなり
コンストラクタの処理は終わりました
ChengーHanにも同じことをします
Saraに友人を加えます
addFriendの中でfriendsは長くなりますね
friends+chenghanとなり
numFriendsは1増えます これでメソッドは
終了です では次の行に移りましょう
sara.unfriendの中でfriendsを短縮したので
chenghanと“ ”を入れ替えます
friendsは何もない状態に戻って
メソッドが終わります
何かおかしいですね
このリスト上では私に友人はいません
でも私の友人の数は1となっています
どこで間違えたか分かりますね
unfriendでfriendsのリストを更新した時
numFriendsを更新しなかったのです
저는 name, friends, 그리고 numFriends를 추적할 겁니다. 이 줄부터 시작하겠습니다.
사라라는 이름을 가진 사람의 이름을 생성합니다. 그래서 name은 제가 전달한 이름으로
설정됐을 겁니다. Friends는 비었고 numFriends는 0 입니다. 그렇게 하면
생성자 설정은 끝났습니다. 챙한도 같게 해줍니다. 그리고 사라한테
친구를 추가시켜 줍니다. addFriend에서 friends는 더 길어질 겁니다. 저장된 값은 바로 friends
+ 챙한이고 numFriends는 하나씩 증가할 겁니다. 메소드가 끝났으니 다음
줄로 가봅시다. sara.unFriend에서 친구를 없앱니다, 그래서 챙한이 있던 자리는
없는 자리로 설정이 됩니다. 그래서 friend는 비어 있는 값이 되고 끝납니다.
그러나 여기서 무언가가 잘못되었습니다. 저는 분명히 이 목록에는 친구가 없습니다.
하지만 여기 제 친구의 수는 아직도 1 입니다. 어디에 저런 문제가 발생했는지 알겁니다. unFriend
입니다. 친구 목록을 업데이트 했지만 친구의 수를 업데이트
하지 않았습니다.
Tôi định sẽ trace name, friends, và numFriends. Bắt đầu ở dòng đầu tiên nhé,
ta tạo ra một person mới với tên Sara. Vậy là name sẽ được set bằng name mà
tôi truyền vào. Friends chưa có gì, numFriends bằng 0. Và ta đã làm xong một
cái constructor. Chúng ta cũng làm tương tự với Chenghan. Và bây giờ ta add thêm một friend cho
Sara. Nên trong addFriend, biến friends sẽ dài ra, nó sẽ là friends + Chenghan
và numFriends sẽ tăng thêm 1. Sau khi làm xong method đấy rồi, ta chuyển sang
dòng tiếp theo. Ở trong sara.unfriend, ta cắt bớt friends đi, và thay Chenghan bằng
một dấu space và dấu "" rỗng. Vậy là friends chả còn lại gì cả và ta xong rồi.
Có gì đấy không ổn ở đây. Rõ ràng là tôi đã ko có bạn bè nào trong cái list ở đây cả.
Còn đây, số lượng bạn bè của tôi vẫn giữ nguyên là 1. Ta xem lại lúc nó xảy ra.
Ở trong unfriend, ta đã update list friends rồi, nhưng chưa update số lượng
friends.