p325toStringP325toStringP327finalize P328断点P335零钱通
--
object的源代码返回return getClass().getName()+@+Intefer.toHexstring(hashCode());
全类名(包名+类名)
package com.Object; public class ToString { public static void main(String[] args) { Monster monster =new Monster("xiao","巡山",999); System.out.println(monster.toString()+" "+monster.hashCode()); //因为子类没有重写,所以调用的是object的toString方法 //(3)当直接输出一个对象时, toString 方法会被默认的调用, 比如System.out.println(monster) ; 就会默认调用 monster.toString() } } class Monster{ private String name; private String job; private double salary; public Monster(String name, String job, double salary) { this.name = name; this.job = job; this.salary = salary; } } package com.Object; public class ToString { public static void main(String[] args) { Monster monster =new Monster("xiao","巡山",999); System.out.println(monster.toString()+" "+monster.hashCode()); //因为子类重写,所以调用的是自己的toString方法 } } class Monster { private String name; private String job; private double salary; public Monster(String name, String job, double salary) { this.name = name; this.job = job; this.salary = salary; } //重写toString 方法, 输出对象的属性 //使用快捷键即可alt+insert -> toString @Override public String toString() { //重写后,一般是把对象的属性值输出,当然程序员也可以自己定制 return "Monster{" + "name='" + name + "t" + ", job='" + job + "t" + ", sal=" + salary +"}"; } }
-
重写调用自己的,没有调用Monser的
P327finalize
package com.Object; public class Finalize { public static void main(String[] args) { Car c1 = new Car("bmv"); c1=null;//这时,car对象就是垃圾,垃圾回收器就会销毁回收对象(释放空间) //在销毁之前会调用对象的finalize()方法,程序员就可以写入自己的业务逻辑 //如果程序员不重写的话,就调用Object类的默认finalize()方法 System.out.println("程序退出");//最后只会输出这条语句 //垃圾回收机制有自己的算法,并非生成垃圾就立刻回收 } } class Car{ private String name; public Car(String name) { this.name = name; } @Override protected void finalize() throws Throwable { System.out.println("我们销毁汽车bwm"); } public String getName() { return name; } public void setName(String name) { this.name = name; } }
P328断点测试(debug)
--
P329断掉调试案例(点击左键在左边行的前,序号和代码间位置)
然后左键点击debug,出现Debugger界面
--
如果进入jdk源码,1. force Setp into (F7) 2.配置一下,setting-bulid,execution,development----debugguer---setting-取消java和javax
-
可以在debug运行时候,再下断点,也可以对源码下断点P332
P333断点调试课后练习
-
主要是看源码,理解断点调试的逻辑(个人觉得用来看代码的运行过程非常的神奇!!)
P335零钱通
-
//myscanner,if a==1,收益,
过程编程版
package com.Object.debug; import java.text.SimpleDateFormat; import java.util.Scanner; import java.util.Date; public class SCT { public static void main(String[] args) { boolean loop =true; Scanner scanner=new Scanner(System.in); String key=""; //明细思路,1做成数组,2对象,3String拼接 double money=0; double balance=0; String note=""; String detials="=======零钱通明细=======n"; do{ System.out.println("tttt菜单"); System.out.println("tttt1零钱明细"); System.out.println("tttt2收入明细"); System.out.println("tttt3消费明细"); System.out.println("tttt4退出"); System.out.println("输入范围1-4"); key= scanner.next(); Date date=null;//Date是java.util.Date,表示日期 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");//用于日期格式化 //使用分支控制 switch (key) { case "1": System.out.println(detials); break; case "2": System.out.println("收入金额:"); money = scanner.nextDouble(); //money的值需要校验 balance += money; date = new Date();//获取当前日期 if(money <=0){ System.out.println("收入金额不能小于0"); break; } detials += "n收入金额:t" + money + "t" + sdf.format(date) + "t" + balance; System.out.println(detials); break; case "3": System.out.println("消费金额:"); money = scanner.nextDouble(); //money的值需要校验 System.out.println("消费说明"); note = scanner.next(); if(money <=0 || money > balance){ System.out.println("消费金额应该在0-"+balance); break; } balance -= money; date = new Date();//获取当前日期 detials += "n" + note + "t-" + money + "t" + sdf.format(date) + "t" + balance; break; case "4": String choice = ""; while (true){ System.out.println("确实退出吗,y/n"); choice=scanner.next(); if(choice.equals("y") || "n".equals(choice)){ break//要确认一遍,知道y/s才跳出循环 } } if(choice.equals("y")) { loop = false; } break; default: System.out.println("输入有错误"); }while(loop); System.out.println("退出零钱通"); } }
oop版本
先封装
package smallchange; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class Oop { //完成各种菜单,并可以选择 //属性 boolean loop =true; Scanner scanner=new Scanner(System.in); String key=""; //明细思路,1做成数组,2对象,3String拼接 double money=0; double balance=0; String note=""; String detials="=======零钱通明细=======n"; Date date=null;//Date是java.util.Date,表示日期 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");//用于日期格式化 public void mainMeau (){ do{ System.out.println("tttt菜单"); System.out.println("tttt1零钱明细"); System.out.println("tttt2收入明细"); System.out.println("tttt3消费明细"); System.out.println("tttt4退出"); System.out.println("输入范围1-4"); key= scanner.next(); Date date=null;//Date是java.util.Date,表示日期 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");//用于日期格式化 //使用分支控制 switch (key) { case "1": this.detial(); break; case "2": this.income(); break; case "3": this.pay(); break; case "4": this.exit(); break; default: System.out.println("输入有错误"); } }while(loop); System.out.println("退出零钱通"); } public void main(){ System.out.println("显示零钱通菜单"); } public void detial(){ System.out.println(detials); } public void income(){ System.out.println("收入金额:"); money = scanner.nextDouble(); //money的值需要校验 balance += money; date = new Date();//获取当前日期 if(money <=0){ System.out.println("收入金额不能小于0"); return; } detials += "n收入金额:t" + money + "t" + sdf.format(date) + "t" + balance; } //消费 public void pay(){ System.out.println("消费金额:"); money = scanner.nextDouble(); //money的值需要校验 System.out.println("消费说明"); note = scanner.next(); if(money <=0 || money > balance){ System.out.println("消费金额应该在0-"+balance); return; } balance -= money; date = new Date();//获取当前日期 detials += "n" + note + "t-" + money + "t" + sdf.format(date) + "t" + balance; } //退出 public void exit(){ String choice = ""; while (true){ System.out.println("确实退出吗,y/n"); choice=scanner.next(); if(choice.equals("y") || "n".equals(choice)){ break;//要确认一遍,知道y/s才跳出循环 } } if(choice.equals("y")) { loop = false; } } }
调用
package smallchange; public class Test { public static void main(String[] args) { new Oop().mainMeau(); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)