类就是马原中的“物质”,英文中的"class"。
对象就是具体的“个体。
”我是一年一班的同学,老师按照花名册随机抽取我和同学进行提问。“这个过程就是面向对象。
创建一个手机类。
1.创建对象
2.描写类的属性
3.描写类方法
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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;//给对象5赋值
引用对象的方法:对象名.方法名()
phone.sendMessage();//调用sendMessage()方法
类是以代码形式放在内存当中的,把文件的信息放在内存当中,此时这些信息的集合就叫做做对象,这个过程叫做实例化
····················································································································································
public class IntailTelphone {
public static void main(String[] args ){
TelPhone phone=new TelPhone();
phone.sendMessage();
phone.screen=5.0f;
phone.cpu=1.4f;
phone.mem=2.0f;
phone.sendMessage();
}
}
······················································································································································