既然在不同的方法中可以有同名局部变量,为什么在输出时只能输出一个同名变量的值?
package com.imooc;
public class Telphone {
//属性(成员变量)有什么
float screen;
float cpu;
float mem;
int var;
//方法 干什么
void call() {
int localVar=20;
System.out.println("localVar:"+localVar);
System.out.println("var:"+var);
System.out.println("Telphone有打电话的功能!");
}
void sendMessage() {
int localVar=20;
System.out.println("localVar:"+localVar);
System.out.println("var:"+var);
System.out.println("screen:"+screen+"cpu:"+cpu+"mem:"+mem+"Telphoe有发短信的功能!");
}
}
package com.imooc;
public class InitailTelphone {
public static void main(String[] args) {
// TODO Auto-generated method stub
Telphone phone = new Telphone();
//phone.sendMessage();
//给实例变量赋值
phone.screen=5.0f;
phone.cpu=1.4f;
phone.mem=2.0f;
//调用对象的方法
phone.call();
System.out.println();
phone.sendMessage();
}
}
运行结果为:
Telphone有打电话的功能!
localVar:20
var:0
screen:5.0cpu:1.4mem:2.0Telphoe有发短信的功能!