In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "Java singleton pattern and final and abstract class and interface instance analysis". The editor shows you the operation process through the actual case, and the operation method is simple, fast and practical. I hope this article "Java singleton pattern and final and abstract class and interface instance analysis" can help you solve the problem.
1. Singleton mode 1. The realization of hungry Han style and lazy Chinese style
Steps:
Privatize the constructor
Create objects within the class
Expose a static public method
two。 Hungry class School1 {/ / hungry Chinese private String name; / / directly create the object private static School1 sc = new School1 ("Tsinghua University") inside the class; / / privatize the constructor private School1 (String name) {this.name = name;} / / provide a public static method to return the object public static School getInstance () {return sc;} 3. Lazy class School2 {/ / lazy private String name; / / create an object directly inside the class private static School2 sc; / / privatize the constructor private School2 (String name) {this.name = name } / / provide a public static method to return the object / / if the object is null create object or return object public static School2 getInstance () {if (sc = = null) {sc = new School2 ("Tsinghua University");} return sc;}} 4. The difference between lazy Chinese style and hungry Chinese style
The timing of creating an object is different: the hungry type creates the object when the class is loaded, and the lazy type creates it when it is used.
There is no thread safety problem in the hungry Han style, but there is a thread safety problem in the lazy Chinese style.
The hungry Chinese style creates the object when the class is loaded, so there is a waste of resources, but the lazy type does not have this problem.
The use of 2.final 1. Basic introduction
Final can modify classes, properties, methods, and local variables.
two。 Working with scen
① does not want the parent class to be inherited.
② should not want the methods of the parent class to be overridden by the subclass.
When ③ does not want a property of the class to be modified
④ doesn't want a local variable to be modified.
Final class A {}; / / when you do not want the parent class to be inherited. Class B {public final void display () {}; / when you do not want the method of the parent class to be overridden by the subclass. Public final int public void show B = 10 suffix / when you do not want an attribute of the class to be modified, public void show () {Avocc = 20 suffix / when you do not want a local variable to be modified}} 3. Usage considerations and details discussion
Attributes modified by 1.final are also called constants, which are generally used as XX_XX
When defining a 2.final-decorated property, you must assign an initial value in the following location
The value is assigned directly when ① is defined.
② is assigned in the constructor.
③ is assigned in the code block. (normal attributes are initialized in ordinary code blocks, and static attributes are initialized in static code blocks)
Class A {/ / is directly assigned when defined. Public final int a = 10 leading / {/ / a = 10 leading / assign in the code block. / /} / public A () {/ a = 10 position / assign a value in the constructor. / /}}
3. If the property decorated by final is static, the initialized position can only be
The value is assigned directly when ① is defined.
② is assigned in a static code block.
Class A {/ / is directly assigned when defined. Public final static int a = 10 static / static {/ / a = 10 position / assign in a static code block. / public A () {/ / final modified attribute is static, then the initialized position cannot be the constructor / / a = 10
The 4.final class cannot inherit, but objects can be instantiated.
5.final decorated methods cannot be overridden, but can be inherited.
6.final cannot modify the constructor.
The combination of 7.final and static will not cause the class to load, so it is more efficient.
8. Wrapper class (Integer,Double,Float,Boolean,String, etc. Are all final decorated)
3. Abstract class 1. Basic introduction
Classes modified with the abstract keyword are called abstract classes, and abstract can also be used to modify a method, that is, an abstract class.
Abstract class A {abstract void display (); / / when there is an abstract method in a class, the class is also defined as an abstract class} 2. Usage considerations and details discussion
1. Abstract classes cannot be instantiated.
two。 An abstract class can have no abstract method.
3. When a class has an abstract method, the class is also defined as an abstract class.
4.abstract can only decorate classes and methods, not properties and others.
5. An abstract class can have any member, but an abstract method cannot have an implementation body.
6. If a class inherits an abstract class, you must implement all the methods of the abstract class.
7. Abstract methods cannot be modified with private,static,final because these keywords are contrary to rewriting
4. Interface 1. Basic introduction
Interface interface name {
/ / attribute
/ / method (can be abstract method, static method, default method)
/ / before JDk7.0, all methods of the interface are abstract methods
}
Class class name implements interface {
Own attribute
Own method
Abstract methods of interfaces that must be implemented
}
Public interface UsbInterface {int a = 10 / actually public final static int a = 10; void connect (); void close (); / / Abstract method default public void display () {/ / default method System.out.println ("method in the interface is called ~");} public static void show () {/ / static method System.out.println ("static method in the interface is called ~") }} 2. Notes and details discussion
1. Interface cannot be instantiated
two。 All the methods in the interface are public methods, and the abstract methods in the interface can be modified without abstract.
3. A normal class implements the interface. You must implement all the methods of the interface, and you can use alt+enter to solve the
4. When an abstract class implements an interface, it can not implement the abstract methods of the interface.
5. When a class implements an interface, the class can call all the properties and default methods in the interface, but not the static methods in the interface.
Public class MySql implements UsbInterface {@ Override public void connect () {System.out.println ("MySql called"); display (); System.out.println (a);} @ Override public void close () {System.out.println ("MySql is disabled");}} 3. The difference between implementing interfaces and inheritance
1. Inheritance is to inherit all the properties of the parent class, and the child class inherits all the attributes of the parent class, and implementing the interface is like learning other skills that are not available.
two。 The value of inheritance mainly lies in solving the reusability and maintainability of the code.
3. The value of interface mainly lies in: design specification. More flexible.
4. Inheritance is the relationship that satisfies is-a, and the interface only needs to satisfy the relationship of like-a.
5. The interface decouples the code to some extent.
4. Interface and inheritance show how to access the attribute public class Interface01 {} interface A {int x = 10;} class B {int x = 20;} class C extends B implements A {public void display () {System.out.println (A.x + "+" + super.x) The properties of the} / / interface are accessed directly through the interface name, and the parent class accesses public static void main (String [] args) {new C (). Display ();}} 5 through super. Polymorphic characteristics of interfaces
1. Polymorphic parameter
Interfaces can accept different objects, and references to interfaces can point to objects of the class that implements the interface.
Public class InterfaceDetails {public static void main (String [] args) {MySql mySql = new MySql (); Oracle oracle = new Oracle (); t (mySql); t (oracle); UsbInterface UIf = mySql;} public static void t (UsbInterface mb) {mb.close (); mb.connect ();}}
two。 Polymorphic array
Public interface UsbInterface {void work ();} public class Phone implements UsbInterface {@ Override public void work () {System.out.println ("the phone is working.") ;} public void call () {System.out.println ("the phone is making a call.") ;} public class Camera implements UsbInterface {@ Override public void work () {System.out.println ("camera is working.") ;} public class Interface02 {public static void main (String [] args) {UsbInterface [] uIF = new UsbInterface [2]; Phone phone = new Phone (); Camera camera = new Camera (); uIF [0] = phone; uIF [1] = camera; for (int I = 0; I < 2; iTunes +) {uIF [I] .work () If (UIF [I] instanceof Phone) {((Phone) uIF [I]) .call ();}
3. There is a phenomenon of polymorphic transmission in the interface.
Interface AA {void display ();} interface BB extends AA {} class CC implements BB {@ Override public void display () {}} about "Java singleton pattern and final and abstract class and interface instance analysis", thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.