标签:phtow 同一个 对象 System phone 内存 println color out
两个对象使用同一个方法的内存图
public class PhoneDemo02 { public static void main(String[] args) { phone phone = new phone(); System.out.println(phone.brand); System.out.println(phone.color); System.out.println(phone.price); System.out.println("******************"); phone.brand = "苹果"; phone.color = "土豪金"; phone.price =1.3; System.out.println("******************"); System.out.println(phone.brand); System.out.println(phone.color); System.out.println(phone.price); System.out.println("******************"); phone.call("秦始皇"); phone.sendMessage(); System.out.println("******************"); phone phtow = new phone(); System.out.println(phtow.brand); System.out.println(phtow.color); System.out.println(phtow.price); System.out.println("******************"); phtow.brand = "oppo"; phtow.color = "金色"; phtow.price =9.9; System.out.println("******************"); System.out.println(phtow.brand); System.out.println(phtow.color); System.out.println(phtow.price); System.out.println("******************"); phtow.call("汉高祖"); phtow.sendMessage(); } }
对象调用方法时,根据对象中方法标记(地址值),去类中寻找方法信息。这样哪怕是多个对象,方法信息 只保存一份,节约内存空间。
public class PhoneDemo03 { public static void main(String[] args) { phone phone = new phone(); System.out.println(phone.brand); System.out.println(phone.color); System.out.println(phone.price); System.out.println("******************"); phone.brand = "苹果"; phone.color = "土豪金"; phone.price =1.3; System.out.println("******************"); System.out.println(phone.brand); System.out.println(phone.color); System.out.println(phone.price); System.out.println("******************"); phone.call("秦始皇"); phone.sendMessage(); System.out.println("******************"); phone phtow =phone; System.out.println(phtow.brand); System.out.println(phtow.color); System.out.println(phtow.price); System.out.println("******************"); phtow.brand = "oppo"; phtow.color = "金色"; phtow.price =9.9; System.out.println("******************"); System.out.println(phtow.brand); System.out.println(phtow.color); System.out.println(phtow.price); System.out.println("******************"); phtow.call("汉高祖"); phtow.sendMessage(); } }
标签:phtow,同一个,对象,System,phone,内存,println,color,out 来源: https://www.cnblogs.com/x3449/p/16425653.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。