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