Java Basic Learning Path (iv) defining a simple Java class

Source: Internet
Author: User

Simple Java Class Development general principles:

    1. The name of the class must be meaningful, and then how to say, let people see it.
    2. All properties within a class must use private encapsulation and provide the Setter,getter method
    3. There can be more than one construction method in a class, but you must keep a parameterless construction method
    4. No output statements are allowed in the class, all outputs must be given to the
    5. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.