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. PostgreSQL逻辑复制使用记录

    之前逻辑复制刚刚出来的时候就使用过,但是没有进行整理,这次一个项目需要逻辑复制的自动迁移,再次拾起逻辑复制. 在此之前有两个疑问: 1)同一个表,既有流复制,又有逻辑复制,这样数据会有两份吗? --不 ...

  2. Android深入理解JNI(二)类型转换、方法签名和JNIEnv

    相关文章 Android深入理解JNI系列 前言 上一篇文章介绍了JNI的基本原理和注册,这一篇接着带领大家来学习JNI的数据类型转换.方法签名和JNIEnv. 1.数据类型的转换 首先给出上一篇文章 ...

  3. 快速生成树RTSP

    Note: 数据分组:

  4. mstsc Windows局域网内远程桌面连接

    1.检查被连接计算机的远程桌面连接功能是否开启  控制面板->系统和安全->系统->远程设置->远程桌面->勾选"仅允许运行使用网络级别身份验证的远程桌面的计算 ...

  5. Android 编程 AMapLocationClientOption 类中的 setNeedAddress 方法用处 (高德地图 com.amap.api.location.AMapLocationClientOption 中的类)

    最近在用高德地图来写Android App, 其中有一些 方法是不太理解的,这里写一下 对  高德地图  com.amap.api.location.AMapLocationClientOption ...

  6. js之数码时钟加随机变色

      HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  7. CentOS下glibc更新

    使用java -agentpath=xxx时报该错,提示glibc版本过低:version `GLIBC_2.14' not found (required by /data/jjns/UEISecu ...

  8. HDU2604 Queuing 矩阵初识

    Queues and Priority Queues are data structures which are known to most computer scientists. The Queu ...

  9. BZOJ2936 Codevs3634 POI1999 积水 【并查集】*

    BZOJ2936 Codevs3634 POI1999 积水 题目描述 有这样一块土地,它可以被划分成N×M个正方形小块,每块面积是一平方英寸,第i行第j列的小块可以表示成P(i,j).这块土地高低不 ...

  10. Hive中日期处理

    1.日期函数UNIX时间戳转日期函数:from_unixtime() 函数 格式 返回值 说明 from_unixtime from_unixtime(bigint unixtime[, string ...