Simple Java Class Development general principles:
- The name of the class must be meaningful, and then how to say, let people see it.
- All properties within a class must use private encapsulation and provide the Setter,getter method
- There can be more than one construction method in a class, but you must keep a parameterless construction method
- No output statements are allowed in the class, all outputs must be given to the
- Class requires a method that can obtain complete information about the object, commonly called GetInfo (), which returns a string type of data
classBook {PrivateString name; Private intPrice ; Private intnum; PublicBook () {} PublicBook (String na,intPriintN) {name=na; Price=pri; Num=N; } PublicBook () {} Public voidSetName (String N) {name=N; } Public voidSetprice (intp) { price=p; } Public voidSetnum (intn) {num=N; } PublicString GetName () {returnname; } Public intGetPrice () {returnPrice ; } Public intGetnum () {returnnum; } Public intGetmonney (intPriceintnum) { returnprice*num; } PublicString GetInfo () {return "Title:"+name+"\ n"+"Unit Price:"+price+"\ n"+"Quantity:"+num+"\ n"; }} Public classTest2 { Public Static voidMain (String args[]) {book Monney=NewBook ("the old man and the Sea", -,3); System. out. println (Monney.getinfo ()); }}
Java Basic Learning Path (iv) defining a simple Java class