首页 > 其他分享 >CS61B_discussion3_week2_2

CS61B_discussion3_week2_2

时间:2023-05-07 16:34:53浏览次数:34  
标签:week2 CS61B baz same foobar discussion3 ___ Foo public

 1 public class Horse {
 2     Horse same;
 3     String jimmy;
 4     
 5     public Horse(String lee) {
 6         jimmy = lee;
 7     }
 8     
 9     public Horse same(Horse horse) {
10         if (same != null) {
11             Horse same = horse;
12             same.same = horse;
13             same = horse.same;
14     }
15         return same.same;
16     }
17     
18     public static void main(String[] args) {
19         Horse horse = new Horse("youve been");
20         Horse cult = new Horse("horsed");
21         cult.same = cult;
22         cult = cult.same(horse);
23         System.out.println(cult.jimmy);
24         System.out.println(horse.jimmy);
25     }
26   }

输出的结果是

horsed
youve been

 原因在于,在第14行结束后,局部变量same(就是指向horse的那个same)就被销毁了,此时15行的same是cult 的same, return same.same 就是return cult自己。

 

 1 public class Foo {
 2     public int x, y;
 3     public Foo (int x, int y) {
 4         this.x = x;
 5         this.y = y;
 6     }
 7     
 8     public static void switcheroo (Foo a, Foo b) {
 9         Foo temp = a;
10         a = b;
11         b = temp;
12     }
13     
14     public static void fliperoo (Foo a, Foo b) {
15         Foo temp = new Foo(a.x, a.y);
16         a.x = b.x;
17         a.y = b.y;
18         b.x = temp.x;
19         b.y = temp.y;
20     }
21     
22     public static void swaperoo (Foo a, Foo b) {
23         Foo temp = a;
24         a.x = b.x;
25         a.y = b.y;
26         b.x = temp.x;
27         b.y = temp.y;
28     }
29     
30     public static void main (String[] args) {
31         Foo foobar = new Foo(10, 20);
32         Foo baz = new Foo(30, 40);
33         switcheroo(foobar, baz); //foobar.x: ___ foobar.y: ___ baz.x: ___ baz.y: ___
34         fliperoo(foobar, baz); //foobar.x: ___ foobar.y: ___ baz.x: ___ baz.y: ___
35         swaperoo(foobar, baz); //foobar.x: ___ foobar.y: ___ baz.x: ___ baz.y: ___
36         System.out.println();
37     }
38   }

10 20 30 40

30 40 10 20 

10 20 10 20 

33 行我有点不太明白。

其实传入到函数Switcheroo的时候,a 和 b 都仅仅是指向foobar and baz,所以  a = b 就仅仅只是改变了a 的指向罢了,从指向foobar 到指向 baz.

 

标签:week2,CS61B,baz,same,foobar,discussion3,___,Foo,public
From: https://www.cnblogs.com/xuenima/p/17379329.html

相关文章

  • [HNCTF 2022 WEEK2]
    easy_unser<?phpinclude'f14g.php';error_reporting(0);highlight_file(__FILE__);classbody{private$want,$todonothing="ican'tgetyouwant,ButyoucantellmebeforeIwakeupandchangemymind"......
  • CS61B_值得注意的知识
    1.GoldenRuleofEquals Forprimitives,theline inty=x copiesthebitsinsidethe x boxintothe y box.Forreferencetypes,wedotheexactsamething.Intheline WalrusnewWalrus=oldWalrus;,wecopythe64bitaddressinthe oldWalrus boxi......
  • CS61B_lab02
    题目描述:dcatenate(IntListA,IntListB):返回一个由A的所有元素组成的列表,后面是B的所有元素。可能会修改A。Don'tuse'new'。publicstaticIntListdcatenate(IntListA,IntListB){if(A==null){returnB;}IntListptr=......
  • JYB_Word_Week2-2
    1总结linux安全模型三A认证,资源分配:Authentication:认证,验证用户身份;Authorization:授权,不同的用户设置不同的授权;Accouting|Audition:审计。1.1Linux中每个用户通过Userid(U......
  • 2024考研408Week2
    一、本周总结:使用时间:(目标45h,未达到)总计20h27min,数学9h2min,专业课2h,英语9h25min.去省行交流的第一周自己有点浮躁,学习效率甚至不如上一周,需要调整。二、存在问题:1.数学、......
  • CS61B学习笔记_Lecture4 References, Recursion, and Lists
    还是得先熟悉java的语法规则,准备先回归CS61B了。。。Bits: 计算机将信息储存为内存,用bits(0或1)序列表示这些信息。(一般简写为“b”,注意不要与字节Byte搞混,字节一般用“B......
  • CS61B学习笔记_Project0
    1GameRules1.4x4网格,每个位置为空或者填有带有一个2的正整数次幂数字的贴图;2.第一次移动前,随机选择一个空位填入带有数字2或4的贴图,其中填充2的概率为75%,填充4的概率......
  • week2
    #-*-coding:utf-8-*-importnumpyasnpimportpandasaspdimportmatplotlib.pyplotaspltimportseabornassnsinputfile=r"D:\DeskTop\data.csv"#输......
  • 2023寒假训练week2
    Day1SMUWinter2023Round#5(Div.2)A.Lucky?1.字符转数字2.相加并比较#include<bits/stdc++.h>usingnamespacestd;strings;inta[10];intmain(){ int......
  • HGAME 2023 Week2 Pwn YukkuriSay题解
    HGAME2023Week2PwnYukkuriSay题解检查保护:拿到文件先checksec一下:64位程序,开启canary和nx保护,没有开启PIE(可以使用绝对地址了)继续往下看,先不着急打开ida,我们先运......