-
创建一个Telephone类
package com.yiibai;
public class Telephone {
/**
* 成员变量定义在类中,可以被本类的所有方法使用
*/
float screen; //未赋值时,成员变量的默认值为0
float cpu;
float men;
/**
* 方法说明对象可以干什么
*/
void call(){
/**
* 局部变量定义在方法内部,只能被方法引用,局部变量必须要初始化
*/
int size=1; //局部变量无默认值,定义时必须有初始化值
System.out.println("rel有打电话的功能");
System.out.println(screen);
System.out.println(size);
}
void sendMessage(){
/**
* 局部变量定义在方法内部,只能被当前方法引用
* 局部和全员变量重名时,局部变量的优先级更高
*/
float screen=3.4f; //和局部变量重名了,即sendMessage的输出值为3.4f
System.out.println("电话可以发短信");
System.out.println(screen);
}
}
-
创建一个InitialTelephone类
package com.yiibai;
public class InitialTelephone {
public static void main(String[] args){
Telephone tell=new Telephone(); //类的实例化,得到一个对象,对象名为tell ,,对象的类型是类 即Telephone
tell.screen=4.5f;
tell.cpu=5.6f;
tell.men = 8.8f;
tell.call();
tell.sendMessage();
}
执行结果如下:
rel有打电话的功能
4.5
1
电话可以发短信
3.4