Java Notes 5__ construction block, static block/Singleton design pattern/inheritance/final keyword/super keyword

Source: Internet
Author: User

 Public classMain {{//building blocks (called when constructing an object, prior to construction method execution)System.out.println ("I am a building block!") "); }    Static{//static blocks (executed at class load time and executed only once "execute before building block" eg: Database connection)System.out.println ("I am a static block! "); }     PublicMain () {System.out.println ("I am the construction Method!" "); }     Public Static voidMain (string[] args) {main M1=NewMain (); Main m2=NewMain (); }  }

/*** Singleton design mode: guarantees that a class has only one instance and provides a global access point to access it * 1. Construction method Privatization * 2. Declare an object of this class * 3. Provides a static method for externally obtaining an object instance * Two implementations: A hungry man type, lazy type * Application: Tool Class (Example database) */ Public classMain { Public Static voidMain (string[] args) {Singleton1 S1=singleton1.getinstance ();        S1.print (); Singleton1 S2=singleton1.getinstance (); Singleton1 S3=singleton1.getinstance (); System.out.println (S1==S2);//The output TRUE,S1,S2,S3 all point to the same objectSingleton2 T1=singleton2.getinstance ();        T1.print (); Singleton2 T2=singleton2.getinstance (); SYSTEM.OUT.PRINTLN (T1==T2);//output true,t1,t2 point to the same object    }}/*** Design the class into a single case design mode * A hungry man type*/classsingleton1{Private StaticSingleton1 s =NewSingleton1 ();//a hungry man, start with a new object    PrivateSingleton1 () {} Public voidprint () {System.out.println ("A Hungry man single-case design mode! "); }     Public StaticSingleton1 getinstance () {returns; }}/*** Design the class into a single case design pattern * lazy "thread-safe during multi-threaded access"*/classsingleton2{Private StaticSingleton2 s =NULL;//lazy, first not new, call again new    PrivateSingleton2 () {} Public voidprint () {System.out.println ("Lazy single-case design mode!" "); }     Public StaticSingleton2 getinstance () {if(s==NULL) {s=NewSingleton2 (); }        returns; }}

/*** Inheritance * 1.Java can only achieve single inheritance, that is, a class can have only one direct parent class * 2. Inheritance can inherit only non-private properties and methods (Public,protected,default) * 3. Construction methods cannot be inherited*/ Public classMain { Public Static voidMain (string[] args) {Homedog D2=NewHomedog ("Hels");//The constructor method of the parent class executes first (and only the default constructor method of the parent class is "no parameter" unless explicitly called)D2.desc (); }  }classdog{String name;  PublicDog () {System.out.println ("A dog born."); }     PublicDog (String name) {System.out.println ("A Dog:" +name+ "is born."); }     Public voiddesc () {System.out.println ("I ' m a dog."); }}classHomedogextendsdog{ PublicHomedog (String name) {Super(name);//represents a reference to the parent class "must be put in the first sentence"System.out.println ("A Homedog born."); }    /*** Method Override * 1. The two method return values, method names, and parameter lists that occur in the method override must be exactly the same (subclasses override the method of the parent class) * 2. The exception thrown by a subclass cannot exceed the exception thrown by the corresponding method of the parent class * 3. The access level of the subclass method cannot be lower than the parent class Access level of the method (example: Parent protected child public)*/     Public voiddesc () {System.out.println ("I am the way to rewrite it!" "); }    }classWilddogextendsdog{ PublicWilddog () {System.out.println ("A Wilddog born."); }}

/*** Super keyword can be done by: * 1. Call a property in the parent class * 2. Call a method in the parent class * 3. Calling a constructor method in a parent class*/ Public classMain { Public Static voidMain (string[] args) {blackduck D1=NewBlackduck ();    D1.desc (); }  }classduck{protectedString name; protected intPrice = 55;  Public voidcount () {System.out.println ("Duck Price" +Price ); }}classBlackduckextendsduck{ Public voiddesc () {System.out.println ("I ' m a blackduck,my price" +Super. Name); Super. Count (); }}

/*** What the final keyword can do: * 1. Declare a class (The final class, cannot be inherited) * 2. Declares a method (this method cannot be overridden by the quilt Class) * 3. Declares a constant (1. Direct Assignment 2. Assigning values in the constructor method)*/ Public classMain { Public Static voidMain (string[] args) {Girl G1=NewGirl (30);    G1.desc (); }  }Final classgirl{FinalString name = "Fish7"; Final intAge ;  PublicGirl (intAge ) {         This. Age =Age ; }     Public Final voiddesc () {System.out.println ("I ' m" +name+ ", My age is" +Age ); }}

Java Notes 5__ construction block, static block/Singleton design pattern/inheritance/final keyword/super keyword

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.