• java课堂作业(六)


    思路分析:直接按照题目写

     1 class Account {
     2     private String id;
     3     private int password;
     4     private String name;
     5     private String personId;
     6     private String email;
     7     private double balance;
     8     
     9     public Account(){
    10         id = "1234567890123456";
    11         password = 123456;
    12         name = "张三";
    13         personId = "123456789012345678";
    14         email = "12345678@gmail.com";
    15         balance = 0.0;
    16     }
    17     
    18     public Account(String id,int password,String name,
    19             String personId,String email,double balance){
    20         this.id = id;
    21         this.password = password;
    22         this.name = name;
    23         this.personId = personId;
    24         this.email = email;
    25         this.balance = balance;        
    26     }
    27     
    28     public double deposit(double cun){
    29         return balance+=cun;
    30     }
    31     
    32     public double withdraw(double qu){
    33         return balance-=qu; 
    34     }
    35 }

    在做这道题目的时候,发现了一个特别有意思的问题

     1 class A{
     2     public String name;
     3     public void setName(String name){
     4         this.name = name;        
     5     }
     6     public String getName(){
     7         return name;
     8     }
     9 }
    10 class ZhiChuanDiTest {
    11     public static void call(A a1) {
    12         A a2 = new A();
    13         a2.setName("cba");
    14         a1.setName("abc");
    15         a1 = a2 ;
    16         }
    17 
    18     public static void main(String[] arg) {
    19         A test = new A();
    20         call (test) ;
    21         System.out.println("test"+test.getName());
    22         } 
    23 }

    运行结果:

    原因分析:

    在call()方法中,我们new了一个对象a2,名字设置成cba,然后把a2的引用传递给了参数引用a1(地址值而非真正引用),a1和a2现在的地址值相同,指向同一块内存区域。

    我们在main方法中new了一个test,然后用test调用call函数,这个过程中,test引用中保存的地址值传递给形参a1,a1和test指向同一块内存区域,把name设置成了abc

    最后a1放弃指向test那一块内存 转而指向了 a2 的那一块 内存 ,所以 最后输出 test 的 name ,abc

    只影响了a1 而没有影响到 test

    思路分析:照要求写

     1 class BJMiFen{
     2     private String mm;
     3     private int fl;
     4     private boolean dt;
     5     
     6     public BJMiFen(){
     7         mm = "酸";
     8         fl = 1;
     9         dt = true;
    10     }
    11     
    12     public BJMiFen(String mm,int fl,boolean dt){
    13         this.mm = mm;
    14         this.fl = fl;
    15         this.dt = dt;        
    16     }
    17     
    18     public void set(String mm,int fl,boolean dt){
    19         this.mm = mm;
    20         this.fl = fl;
    21         this.dt = dt;
    22     }
    23     
    24     public String toString(){
    25         String a;
    26         if(dt==true){
    27             a = "带汤";
    28         }else{
    29             a = "不带汤";
    30         }
    31         return "面码:"+mm+"  "+"分量:"+fl+"两"+"  "+"是否带汤:"+a;
    32     }
    33     
    34 }
    35 public class BJMiFenTest {
    36 
    37     /**
    38      * @param args
    39      */
    40     public static void main(String[] args) {
    41         // TODO Auto-generated method stub
    42         BJMiFen b1 = new BJMiFen();
    43         b1.set("酸辣", 5, true);
    44         System.out.println(b1.toString());
    45     }
    46 
    47 }

    输出:

  • 相关阅读:
    面向对象SOLID原则-设计模式-第2篇
    python 设计模式 开篇 第1篇
    区块链 第1篇 什么是区块链 区块链与比特币的关系 区块链的发展历史 应用领域
    HTTP协议六种请求:GET,HEAD,PUT,DELETE,POST
    python垃圾回收机制
    类的MRO属性 C3算法
    Vue第六篇 element-ui 项目管理工具npm webpack 启Vue项目vue-cli
    《EffctiveJava》泛型
    Kafka总结
    Windows 关闭端口号
  • 原文地址:https://www.cnblogs.com/dongwenbo/p/3287976.html
Copyright © 2020-2023  润新知