Basic procedures for creating and instantiating Java classes

Source: Internet
Author: User

1  PackageCom.sanguosha.java;2 /*3 * Object-oriented implementation process4 * 1. Create a class and design a member of the class (the member variable is the property and the member method is the method)5 * 2. Create objects of classes through classes, also known as instantiation of classes6 * 3. Complete the function by invoking (object. property) and (object. method) .7  */8  Public classZoo {9      Public Static voidMain (string[] args) {Ten         //instantiation of the base data type One         intn=10; A         //instantiation of an array -         int[] arr=New int[]{}; -         //instantiation of a reference data type, A1 is the real object instantiated by the animal class theAnimal a1=NewAnimal (); -         //Call Properties -A1.age=3; -A1.name= "Tiny"; +System.out.println ("The name of the animal is:" +a1.name+ "The Age of the animal:" +a1.age); -         //Calling Methods +A1.eat ();//the way to do this is to export animals to eat. AA1.sleep ();//the method is to output animals to sleep at          -A1.getname ();//get the name of the animal -System.out.println (A1.getname ());//As with the A1.name effect as follows -A1.setname ("no");//Modify the name of the animal -System.out.println (A1.name);//As with the A1.getname () effect. -          in     } - } to classanimal{//To create an animal class +     //properties of the class - String name; the     intAge ; *     //methods of the class $      Public voideat () {Panax NotoginsengSystem.out.println ("The Animal Eats the Thing"); -     } the      Public voidsleep () { +System.out.println ("Sleeping Animals"); A     } the      PublicString GetName () {//Get animal names +         returnname; -     } $      Public voidSetName (String N) {//Modify the name of the animal $Name=N; -     } -      the}

Operation Result:

Basic procedures for creating and instantiating Java classes

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.