类创建好了,如何使用呢?
代码展示
package cn.ly.combinecode;
/*
* 通常情况下,一个类并不能直接使用,
* 需要根据类创建一个对象,才能使用
* 1.导包 也就是指出需要使用的类,在什么位置。
* import 包名称 类名称;
* 对于和当前类属于同一个包的情况,可以省略导包语句不写
* 2.创建
* 格式 类名称 对象名=new 类名称();
* 3.使用 分为两种情况
* 使用成员变量:对象名.成员变量名
* 使用成员方法:对象名.成员方法名(参数)如果有参数记得带上
* */
public class Dem02Student {
public static void main(String[] args) {
Student student=new Student();
student.eat();
System.out.println( student.age);
System.out.println(student.name="lili");
}
}
结果

练习
手机类
package cn.ly.combinecode;
public class Phone {
String brand;
double price;
String color;
public void call(String who){
System.out.println("给"+who+"打电话");
}
public void senMesage(){
System.out.println("群发短信");
}
}
如何调用
package cn.ly.combinecode;
public class Demo03Phone {
public static void main(String[] args) {
Phone phone=new Phone();
System.out.println(phone.brand="vivo");
System.out.println(phone.color="玫瑰金");
phone.call("李华");
}
}
结果

详细的图解

完整的创建对象,访问成员变量、成员方法,内存当中,栈内存、堆内存、方法区所发生的情况。
当变成两个对象,会有什么不同吗?

方法必须要进栈才能执行。方法运行完要出栈。
两个引用指向同一个对象

1660

被折叠的 条评论
为什么被折叠?



