定义类的步骤:
- 定义类名
- 编写类的属性
- 编写类的方法
public class 类名{
//定义属性部分(成员变量)
属性1的类型 属性1;
属性2的类型 属性2;
....
属性n的类型 属性n;
//定义方法部分
方法1;
方法2;
....
方法m;
}
package com.imooc;
//定义一个类
public class Telphone{
//属性(有什么)
float screen;
float cpu;
float mem;
//方法 (干什么)
void call()
{
System.out.println("Telphone有打电话功能");
}
void sendMessage()
{
System.out.println("Telphone有发信息功能");
}
}
使用对象的步骤
1.创建对象
类名 对象名=new 类名();
Telphone phone =new Telphone();
2.使用对象
引用对象的属性:对象名.属性
phone.screen=5;//给screen属性赋值为5
引用对象的方法:对象名.方法名()
phone.sendMessage();//调用sendMessage()方法
float型变量赋值时需要加f,否则默认浮点数时double型