zoukankan      html  css  js  c++  java
  • JAVA中构造函数的参数传递给类中的实例变量

     1 class VolcanoRobot1
     2    {  String status;
     3       int speed;
     4       float temperature;
     5       VolcanoRobot1(int speed,float temperature)
     6         {   if(temperature > 660)
     7            { status = "returning home";
     8              speed = 5;
     9              temperature = 780;
    10             }   
    11         }
    12         void showAttributes()
    13          { System.out.println("Status:" + status);
    14            System.out.println("Speed: "+ speed);
    15            System.out.println("Temperature: "+ temperature);
    16           }
    17        public static void main(String[] args)
    18            { VolcanoRobot1 robot = new VolcanoRobot1(20,780);
    19              robot.showAttributes();
    20             }    
    21      }

    以上程序运行结果如下:

     1 class VolcanoRobot1
     2    {  String status;
     3       int speed;
     4       float temperature;
     5       VolcanoRobot1(int speed,float temperature)
     6         {   if(temperature > 660)
     7            { status = "returning home";
     8              this.speed = speed;
     9              this.temperature = temperature;
    10             }   
    11         }
    12         void showAttributes()
    13          { System.out.println("Status:" + status);
    14            System.out.println("Speed: "+ speed);
    15            System.out.println("Temperature: "+ temperature);
    16           }
    17        public static void main(String[] args)
    18            { VolcanoRobot1 robot = new VolcanoRobot1(20,780);
    19              robot.showAttributes();
    20             }    
    21      }

    以上程序的运行结果:

     1 class VolcanoRobot1
     2    {  String status;
     3       int speed;
     4       float temperature;
     5       VolcanoRobot1(int speed1,float temperature1)
     6         {   if(temperature1 > 660)
     7            { status = "returning home";
     8              speed = 5;
     9              temperature = 60;
    10             }   
    11         }
    12         void showAttributes()
    13          { System.out.println("Status:" + status);
    14            System.out.println("Speed: "+ speed);
    15            System.out.println("Temperature: "+ temperature);
    16           }
    17        public static void main(String[] args)
    18            { VolcanoRobot1 robot = new VolcanoRobot1(20,780);
    19              robot.showAttributes();
    20             }    
    21      }

    以上程序运行结果:




    以上程序说明:在创建对象的时候,(用new)构造函数的参数在初始化类的实例变量时:

    如果构造函数的参数列表的参数名与实例变量的参数名一样时,需要利用“this”来进行指代实例变量;

    如果希望将构造函数的参数的值传递给实例变量需要用赋值语句进行传递。

  • 相关阅读:
    乘法九九表
    #include <time.h>
    【bzoj2060】[Usaco2010 Nov]Visiting Cows拜访奶牛 树形dp
    【codevs1380】没有上司的舞会 树形dp
    【bzoj1060】[ZJOI2007]时态同步 树形dp
    【bzoj2435】[NOI2011]道路修建 树形dp
    【bzoj3573】[HNOI2014]米特运输 树形dp
    【bzoj4027】[HEOI2015]兔子与樱花 树形dp+贪心
    【codevs1163】访问艺术馆 树形dp
    【bzoj1864】[ZJOI2006]三色二叉树 树形dp
  • 原文地址:https://www.cnblogs.com/lubocsu/p/5083487.html
Copyright © 2011-2022 走看看