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. jsp页面图片显示不出来

    jsp页面就是MyJsp.jsp <body> This is my JSP page. <br> <img src="img/top.jpg"> ...

  2. Shell 命令行 从日志文件中根据将符合内容的日志输出到另一个文件

    Shell 命令行 从日志文件中根据将符合内容的日志输出到另一个文件 前面我写了一篇博文Shell 从日志文件中选择时间段内的日志输出到另一个文件,利用循环实现了我想要实现的内容. 但是用这个脚本的同 ...

  3. iOS - 正则表达式判断邮箱、身份证,车牌,URL等..是否正确:

    //邮箱 + (BOOL) validateEmail:(NSString *)email { NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Z ...

  4. 白话machine learning之Loss Function

    转载自:http://eletva.com/tower/?p=186 有关Loss Function(LF),只想说,终于写了 一.Loss Function 什么是Loss Function?wik ...

  5. Mac安装最新tensorflow遇到的坑,记录下方便后人

    之前其他mac电脑安装tensorflow时候一切顺利,一行命令sudo pip install tensorflow就高搞定了,但是今天在新mac上安装tensorflow时候出现了一个bug,搞了 ...

  6. Eclipse自动补全设置与Eclipse源代码下载

    以前使用VisualStudio和Sublime写代码的时候有很完善的代码提示,但是最近开始使用Eclipse弄JAVA的时候发现它的代码提示不是很习惯.上网找了一些资料,修改了代码提示的方式,记录在 ...

  7. 如何使用 MSBuild Target(Exec)中的控制台输出

    我曾经写过一篇文章 如何创建一个基于命令行工具的跨平台的 NuGet 工具包,通过编写一个控制台程序来参与编译过程.但是,相比于 基于 Task 的方式,可控制的因素还是太少了. 有没有什么办法能够让 ...

  8. Python模块汇总

    正则模块:re 日期和时间模块:datetime 和time模块 加密模块:hashlib 远程连接模块:paramiko 日志模块:logging 高级函数工具包:functools 多线程: 队列 ...

  9. {Reship}{Socket}C#简单应用

    This article come frome here ======================================================================= ...

  10. python ctypes 和windows DLL互相调用

    图片项目