类和对象
类
类是一组相关属性和行为的集合。可以看成是一类事物的模板,使用事物的属性特征和行为特征来描述该类事物。(人)
属性:该事物的状态信息。(名字、身高、体重)
行为:该事物能做什么。(学习)
对象
对象是一类事物的具体体现,对象是类的一个实例,必然具备该类事物的属性和行为。(小明)
类和对象的关系
类是对一类事物的描述,是抽象的。(手机的设计图)
对象是一类事物的实例,是具体的。(真正的手机:小米手机)
类是对象的模板,对象是类的实体。
堆、栈、方法区
堆区:
1.存储的全部是对象,每个对象都包含一个与之对应的class的信息。(class的目的是得到操作指令)
2.jvm只有一个堆区(heap)被所有线程共享,堆中不存放基本类型和对象引用,只存放对象本身 。
栈区:
1.每个线程包含一个栈区,栈中只保存基础数据类型的值和对象以及基础数据的引用
2.每个栈中的数据(基础数据类型和对象引用)都是私有的,其他栈不能访问。
3.栈分为3个部分:基本类型变量区、执行环境上下文、操作指令区(存放操作指令)。
方法区:
1.又叫静态区,跟堆一样,被所有的线程共享。方法区包含所有的class和static变量。
2.方法区中包含的都是在整个程序中永远唯一的元素,如class,static变量。
一个对象
public class Phone {
String brand; //品牌
double price; //价格
String color; //颜色
public void call(String name){
System.out.println("给" + name + "打电话");
}
public void sendMessage(){
System.out.println("群发短信");
}
}
public class XiaoMi{
public static void main(String[] args) {
Phone phone = new Phone();
System.out.println(phone.brand); //null
System.out.println(phone.price); //0.0
System.out.println(phone.color); //null
phone.brand = "小米";
phone.price = 1999.0;
phone.color = "土豪金";
System.out.println(phone.brand);
System.out.println(phone.price);
System.out.println(phone.color);
phone.call("雷军"); //给雷军打电话
phone.sendMessage(); //群发短信
}
}

两个对象使用同一个方法
public class Test{
public static void main(String[] args) {
Phone xiaoMi = new Phone();
System.out.println(xiaoMi.brand); //null
System.out.println(xiaoMi.price); //0.0
System.out.println(xiaoMi.color); //null
xiaoMi.brand = "小米";
xiaoMi.price = 1999.0;
xiaoMi.color = "土豪金";
System.out.println(xiaoMi.brand); //小米
System.out.println(xiaoMi.price); //1999.0
System.out.println(xiaoMi.color); //土豪金
xiaoMi.call("雷军"); //给雷军打电话
xiaoMi.sendMessage(); //群发短信
System.out.println("------------------");
Phone huaWei = new Phone();
System.out.println(huaWei.brand); //null
System.out.println(huaWei.price); //0.0
System.out.println(huaWei.color); //null
huaWei.brand = "华为"; //华为
huaWei.price = 2999.0; //2999.0
huaWei.color = "红色"; //红色
System.out.println(huaWei.brand);
System.out.println(huaWei.price);
System.out.println(huaWei.color);
huaWei.call("任正非"); //给任正非打电话
huaWei.sendMessage(); //群发短信
}
}

两个引用指向同一个对象
public class Test{
public static void main(String[] args) {
Phone xiaoMi = new Phone();
System.out.println(xiaoMi.brand); //null
System.out.println(xiaoMi.price); //0.0
System.out.println(xiaoMi.color); //null
xiaoMi.brand = "小米";
xiaoMi.price = 1999.0;
xiaoMi.color = "土豪金";
System.out.println(xiaoMi.brand); //小米
System.out.println(xiaoMi.price); //1999.0
System.out.println(xiaoMi.color); //土豪金
xiaoMi.call("雷军"); //给雷军打电话
xiaoMi.sendMessage(); //群发短信
System.out.println("------------------");
Phone huaWei = xiaoMi;
System.out.println(huaWei.brand); //小米
System.out.println(huaWei.price); //1999.0;
System.out.println(huaWei.color); //土豪金
huaWei.brand = "华为";
huaWei.price = 2999.0;
huaWei.color = "红色";
System.out.println(huaWei.brand); //华为
System.out.println(huaWei.price); //2999.0
System.out.println(huaWei.color); //红色
huaWei.call("任正非"); //给雷军打电话
huaWei.sendMessage(); //群发短信
}
}

使用对象类型作为方法的参数
public class Test{
public static void main(String[] args) {
Phone xiaoMi = new Phone();
xiaoMi.brand = "小米";
xiaoMi.price = 1999.0;
xiaoMi.color = "土豪金";
method(xiaoMi);
}
private static void method(Phone param) {
System.out.println(param.brand); //小米
System.out.println(param.price); //1999.0;
System.out.println(param.color); //土豪金
}
}

当一个对象作为参数,传递到方法当中时。实际上传递进去的是对象的地址值。
使用对象类型作为方法的返回值
public class Test{
public static void main(String[] args) {
Phone two = getPhone();
System.out.println(two.brand); //小米
System.out.println(two.price); //1999.0
System.out.println(two.color); //土豪金
}
private static Phone getPhone() {
Phone one = new Phone();
one.brand = "小米";
one.price = 1999.0;
one.color = "土豪金";
return one;
}
}

当使用一个对象类型作为方法的返回值时,返回值其实就是对象的地址值。

浙公网安备 33010602011771号