使用java中的对象,分2步:
1.创建一个对象;
2.使用对象的属性和方法。
见下面的示例代码:
package com.imooc; //1.定义一个类 public class Telphone { //2.属性(成员变量)有什么 float screen; float cpu; float mem; //3.方法干什么 void call(){ System.out.println("Telphone有打电话的功能!"); } void sendMessage(){ System.out.println("screen:"+screen+" cpu:"+cpu+" mem:"+mem+" Telphone有发短信的功能!"); } }
package com.imooc; public class InitialTelphone { public static void main(String[] args) { // TODO Auto-generated method stub Telphone phone = new Telphone(); phone.sendMessage(); //给实例变量赋值 phone.cpu = 1.4f; phone.mem = 2.0f; phone.screen = 5.0f; //调用对象的方法 phone.sendMessage(); } }