class VolcanoRobot1
{ String status;
int speed;
float temperature;
VolcanoRobot1(int speed,float temperature)
{ if(temperature > 660)
{ status = "returning home";
speed = 5;
temperature = 780;
}
}
void showAttributes()
{ System.out.println("Status:" + status);
System.out.println("Speed: "+ speed);
System.out.println("Temperature: "+ temperature);
}
public static void main(String[] args)
{ VolcanoRobot1 robot = new VolcanoRobot1(20,780);
robot.showAttributes();
}
}

以上程序运行结果如下:

 class VolcanoRobot1
{ String status;
int speed;
float temperature;
VolcanoRobot1(int speed,float temperature)
{ if(temperature > 660)
{ status = "returning home";
this.speed = speed;
this.temperature = temperature;
}
}
void showAttributes()
{ System.out.println("Status:" + status);
System.out.println("Speed: "+ speed);
System.out.println("Temperature: "+ temperature);
}
public static void main(String[] args)
{ VolcanoRobot1 robot = new VolcanoRobot1(20,780);
robot.showAttributes();
}
}

以上程序的运行结果:

 class VolcanoRobot1
{ String status;
int speed;
float temperature;
VolcanoRobot1(int speed1,float temperature1)
{ if(temperature1 > 660)
{ status = "returning home";
speed = 5;
temperature = 60;
}
}
void showAttributes()
{ System.out.println("Status:" + status);
System.out.println("Speed: "+ speed);
System.out.println("Temperature: "+ temperature);
}
public static void main(String[] args)
{ VolcanoRobot1 robot = new VolcanoRobot1(20,780);
robot.showAttributes();
}
}

以上程序运行结果:

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

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

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

JAVA中构造函数的参数传递给类中的实例变量的更多相关文章

  1. Java中是否可以调用一个类中的main方法?

    前几天面试的时候,被问到在Java中是否可以调用一个类中的main方法?回来测试了下,答案是可以!代码如下: main1中调用main2的主方法 package org.fiu.test; impor ...

  2. “全栈2019”Java第八十七章:类中嵌套接口的应用场景(拔高题)

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  3. 当实体类中entity/DTO/VO等类中,有枚举值,应该怎么输出?

    当实体类中entity/DTO/VO等类中,有枚举值,应该怎么输出? 问题: orderStatus 和 payStatus都是枚举类,并且枚举的个数达地10来个,我们不可能在模板页面(jsp/ftl ...

  4. Java组合与继承生成的类中构造函数的执行顺序

    [程序实例] import java.util.*; class Meal{ Meal() { System.out.println("Meal Constructor"); } ...

  5. Java集合(1):Collections工具类中的static方法

    与Arrays一样,Collections类中也有一些实用的static方法. (1) 排序操作 reverse(List list):反转指定List集合中元素的顺序 shuffle(List li ...

  6. String类中"=="、equals和普通类中"=="、equals的比较

    package cn.method.demo; public class StringDemo2 { public static void main(String[] args) { String s ...

  7. JAVA类与对象---实例变量与类变量的区别,实例方法和类方法的区别

    实例变量 实例变量声明在一个类中,但在方法.构造方法和语句块之外: 当一个对象被实例化之后,每个实例变量的值就跟着确定: 实例变量在对象创建的时候创建,在对象被销毁的时候销毁: 实例变量的值应该至少被 ...

  8. Java基础之对包,类,方法,变量理解(灵感)

    包,类,方法,变量 灵感乍现 感觉就如电脑上的各个大小文档一般,只不过名称不同,用法不同,功效不同,就好比你要调用网上的一个图片,这个图片可以是变量,可以是方法,可以是类.你要调用可以把他幻化成接口, ...

  9. Java学习笔记——JDBC之PreparedStatement类中“预编译”的综合应用

    预编译 SQL 语句被预编译并存储在 PreparedStatement 对象中.然后可以使用此对象多次高效地执行该语句. 预编译的优点 1.PreparedStatement是预编译的,对于批量处理 ...

随机推荐

  1. spinlock变量没有初始化

    http://blog.csdn.net/longwang155069/article/details/52224284

  2. Shell 命令行统计 apache 网站日志访问IP以及IP归属地

    Shell 命令行统计 apache 网站日志访问IP以及IP归属地 我的一个站点用 apache 服务跑着,积攒了很多的日志.我想用 shell 看看有哪些人访问过我的站点,并且他来自哪里. 因为日 ...

  3. ubuntu创建Centos7镜像&&配置运行环境

    1. 下载centos7镜像 sudo docker pull centos:7 2. 启动centos7容器并挂载本地目录 sudo docker -it -v /home/software:/ho ...

  4. Android 编程 高德地图 (实现显示地图以及定位功能)

    本文参考文章: http://www.apkbus.com/blog-904057-63610.html 本人实现的 定位代码:(具体配置省略,可见参考文章) package com.example. ...

  5. 【排序】希尔排序,C++实现

    原创博文,转载请注明出处! 本文代码的github地址 # 基本思路       希尔排序是”直接插入排序“的改进版,也称为“缩小增量排序”.基本原理:先将待排序的数组元素分成多个序列,然后对每个子序 ...

  6. lx 与cd 的计算方法

    Candela to lux calculation with distance in meters The illuminance Ev in lux (lx) is equal to the lu ...

  7. threejs 世界坐标与屏幕坐标相互转换

    屏幕坐标转世界坐标: let pX = (screenPoint.x / this.scene.renderer.domElement.clientWidth) * 2 - 1; let pY = - ...

  8. mysql 导入 csv文件中数据,只能导入第一行

    用workbench导入csv数据,只能导入数据的第一行,也就是标注每一列的列名的那一行.但问题是,每次导入完成时,系统提示已经导入了500条记录(这个文件中的确有500条记录),可是刷新数据库后打开 ...

  9. E519: Option not supported: fileencodings

    怒转,来自http://blog.chinaunix.net/uid-10671107-id-2943841.html,感谢分享. vim中文乱码,原来是编译就除了问题,看来还不一定是不是坑爹的red ...

  10. Time complexity--codility

    lesson 3: Time complexity exercise: Problem: You are given an integer n. Count the total of 1+2+...+ ...