JAVA中构造函数的参数传递给类中的实例变量
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中构造函数的参数传递给类中的实例变量的更多相关文章
- Java中是否可以调用一个类中的main方法?
前几天面试的时候,被问到在Java中是否可以调用一个类中的main方法?回来测试了下,答案是可以!代码如下: main1中调用main2的主方法 package org.fiu.test; impor ...
- “全栈2019”Java第八十七章:类中嵌套接口的应用场景(拔高题)
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- 当实体类中entity/DTO/VO等类中,有枚举值,应该怎么输出?
当实体类中entity/DTO/VO等类中,有枚举值,应该怎么输出? 问题: orderStatus 和 payStatus都是枚举类,并且枚举的个数达地10来个,我们不可能在模板页面(jsp/ftl ...
- Java组合与继承生成的类中构造函数的执行顺序
[程序实例] import java.util.*; class Meal{ Meal() { System.out.println("Meal Constructor"); } ...
- Java集合(1):Collections工具类中的static方法
与Arrays一样,Collections类中也有一些实用的static方法. (1) 排序操作 reverse(List list):反转指定List集合中元素的顺序 shuffle(List li ...
- String类中"=="、equals和普通类中"=="、equals的比较
package cn.method.demo; public class StringDemo2 { public static void main(String[] args) { String s ...
- JAVA类与对象---实例变量与类变量的区别,实例方法和类方法的区别
实例变量 实例变量声明在一个类中,但在方法.构造方法和语句块之外: 当一个对象被实例化之后,每个实例变量的值就跟着确定: 实例变量在对象创建的时候创建,在对象被销毁的时候销毁: 实例变量的值应该至少被 ...
- Java基础之对包,类,方法,变量理解(灵感)
包,类,方法,变量 灵感乍现 感觉就如电脑上的各个大小文档一般,只不过名称不同,用法不同,功效不同,就好比你要调用网上的一个图片,这个图片可以是变量,可以是方法,可以是类.你要调用可以把他幻化成接口, ...
- Java学习笔记——JDBC之PreparedStatement类中“预编译”的综合应用
预编译 SQL 语句被预编译并存储在 PreparedStatement 对象中.然后可以使用此对象多次高效地执行该语句. 预编译的优点 1.PreparedStatement是预编译的,对于批量处理 ...
随机推荐
- Git 之 问题集锦
准备:远程仓库名:origin 远程分支:master.tt 本地分支:master.test 1. error: src refspec *** does not match an ...
- [Scala]Scala学习笔记六 文件
1. 读取行 读取文件,可以使用scala.io.Source对象的fromFile方法.如果读取所有行可以使用getLines方法: val source = Source.fromFile(&qu ...
- Nodejs操作MongoDB数据库示例
//mongodb_demo.js /** cnpm install mongodb */ var MongoClient = require('mongodb').MongoClient; var ...
- caffe中的Local Response Normalization (LRN)有什么用,和激活函数区别
http://stats.stackexchange.com/questions/145768/importance-of-local-response-normalization-in-cnn ca ...
- [数据处理] LabelEncoder编码
原创博文,转载请注明出处! # LabelEncoder介绍 LabelEncoder是对不连续的数字或文本编号. # LabelEncoder例子 # -*- coding: utf-8 ...
- python中利用类创建的对象来保存信息
在类创建的对象中,一般都是以字典的方式来保存信息 class Student: def __init__(self, name, age, score): self.name = name self. ...
- ubuntu 下使用etcd
1.安装ETCD_VER=v3.1.0 DOWNLOAD_URL=https://github.com/coreos/etcd/releases/download curl -L ${DOWNLOAD ...
- 微信网页登录Tips
http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html 以这篇文章为例,一般都是用户在第三方app中登录时,由第三方去申请资源服务器的登录权限等.即 ...
- 解决js代码中加入alert()就成功执行,不加就不对的问题!
问题: jquery中的$(document).ready(function(){})中调用两个方法(1)利用ajax请求去后台查图书类别的方法(2)当页面上利用图书类别去查询图书返回页面,让图书类别 ...
- HAL层编写规范
andriod HAL模块也有一个通用的入口地址,这个入口地址就是HAL_MODULE_INFO_SYM变量,通过它,我们可以访问到HAL模块中的所有想要外部访问到的方法. 在Linux系统中,后缀 ...