Java classes and objects after-school practice programming (JAVA2 Practical tutorial P111)
This chapter of the content of self-study, in the course of other exercises after doing teaching materials (as long as their careful) are no problem, but in the last computer programming problems when the problem arises, in the reading of the topic when you can feel that the problem is not difficult (indeed it is not difficult), I also quickly wrote out, but run the program, The results have been incorrect, the final change to change is not correct, the idea is right, step-by-stage implementation of the time also feel that there is a place out of the problem but is not out, and finally the textbook in front of the example of repeated look at the contrast, finally found the problem:
SYSTEM.OUT.PRINTLN ("CPU speed:" +CPU); SYSTEM.OUT.PRINTLN ("Capacity of the hard disk:" +HD);
In the PC class appeared this error, find the reason to think this error is good low, but summed up is this chapter knowledge not pass, to this chapter content inside a lot of relationship is vague and miscellaneous, still not clear, so I still need to spend time on this chapter above. Remind yourself to keep the notes in mind when you're doing the code again. In general this week self-study This chapter of the content is still very fruitful. Refuel yourself! Insist!
public class Test {/** * @param args */public static void main (string[] args) {//TODO auto-generated method stub CPU CPU =new CPU (); Create a CPU Object cpu.setspeed (2200); The CPU sets the speed to 2200 harddisk disk = new harddisk (); Create a Disk object Disk.setamount ($); Disk will be set to amount PC pc =new pc (); Create a PC object pc.setcpu (CPU); Pc.setharddisk (disk); Pc.show (); }}
public class PC {CPU CPU = new CPU (); Harddisk HD = new harddisk (); void SetCPU (CPU c) {this.cpu = C;} public void Setharddisk (harddisk h) {HD = h;} public void Show () {System.out.println ("CPU speed:" +cpu.speed); SYSTEM.OUT.PRINTLN ("Capacity of the hard disk:" +hd.amount);}}
public class CPU { int speed; public void setspeed (int m) { this.speed = m; } int GetSpeed () { return speed; }}
public class Harddisk {int amount;public void setamount (int m) {this.amount=m;} int Getamount () {return amount;}}
Output Result:
CPU Speed: 2200 HDD Capacity: 200
After-school exercises for Java classes and objects