The third experiment and the hands-on brain

Source: Internet
Author: User

//The first one to move the brain Packagehands on the brain 3; Public classClassandobjecttest { Public Static voidMain (string[] args) {//create an instance of a class that defines an instance of an object variable referenceMyClass obj =NewMyClass (); //invoking the public method of a class through an object variableObj.mymethod ("Hello"); //Assigning a value to a propertyObj.setvalue (100); //the current value of the output propertySystem.out.println (Obj.getvalue ()); //direct access to object public fieldsObj.information = "Information"; //the current value of the Output object public fieldSystem.out.println (obj.information); }}/*** Examples of custom Java classes*/classMyClass {//Public Fields     PublicString information = ""; //Customizing Public Java instance Methods     Public voidMyMethod (String argu) {System.out.println (ARGU); }    //Define properties: Private field +get method +set method    Private intvalue;  Public intGetValue () {returnvalue; }     Public voidSetValue (intvalue) {         This. Value =value; }} Run as follows://The second one , the brain. Packagehands on the brain 3;classroot{Static{System.out.println ("Static initialization block of root"); } {System.out.println ("Root's normal initialization block"); }     PublicRoot () {System.out.println ("Root parameterless constructor"); }}classMidextendsroot{Static{System.out.println ("Static initialization block for mid"); } {System.out.println ("Normal initialization block for mid"); }     PublicMid () {System.out.println ("No parametric constructor for mid"); }     PublicMid (String msg) {//calling overloaded constructors in the same class through this         This(); System.out.println ("Mid with parametric constructor, its parameter value:" +msg); }}classLeafextendsmid{Static{System.out.println ("Static initialization block of the leaf"); } {System.out.println ("Plain initialization block of the leaf"); }         PublicLeaf () {//A constructor that invokes a string argument in the parent class through Super        Super("Java Initialization sequence Demo"); System.out.println ("The constructor that executes the leaf"); }} Public classteststaticinitializeblock{ Public Static voidMain (string[] args) {NewLeaf (); }} is as follows:

The third experiment and the hands-on brain

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.