5: Object-oriented summary

Source: Internet
Author: User

Advanced class Features


staitc: keyword that is used to decorate the members of a class (properties, methods, inner classes).

For example, each Chinese person shares the name of the country and does not need to allocate a separate variable for each Chinese instance object that represents the name of the country.

    1. In the case of access permitting, the members of the static adornment can be accessed through the class name, which can also be used without creating an object instance.
    2. The member of the static modifier is called a class member and is shared for all instances of the class
    3. The static property of the class can only be accessed inside the static method, and the non-static property of the class cannot be accessed. However, a non-static method can access the static member
    4. Because you do not need an instance to access the static method, you cannot have this and super inside the static method
    5. Static initialization: A statically decorated block of code that executes when the class is loaded and executed once as initialization of a static property

Design Patterns : Like the classic game, different chess games with different games, to avoid ourselves to think and explore

List design Template: That is, the class of the single-state design mode, that is, to take certain measures to ensure that the software system, a class can only exist an object instance.

The specific operation:

    1. An instance cannot be created outside the class by using the new constructor. So privatize the constructor.
    2. Because an instance of the class cannot be created outside of the class, it can only be created inside the class
    3. To make the instance available directly to the outside of the class, use the static modifier (because static decorated members can be accessed directly from the class name, without the new method)
    4. Properties cannot be modified outside the class: privatize this property. A common get method is also provided to access

Eg:

private static person p=new person ();

public static Person Getp () {return p};

Final : Keywords that can be decorated with classes, properties, and methods to represent the final.

The 1.Final decorated property is the final attribute, so it cannot be modified, that is, the constant

2.Final Retouching method is the final method, so cannot be modified, that is, cannot rewrite

The 3.Final decorated class is the final class, so it cannot be extended, that is, it cannot inherit

Attention:

must be initialized at declaration time, can be initialized in a non-static code block, or in a constructor.

Eg:

Class a{

Final int age; {age=12;} Initialize in static code block

Final string Name= "";//string type plus quotation marks

Final int sex;

Aa () {sex=1;}} Class in the constructor.

Abstract : Keywords that are used to decorate classes and methods.

    1. Method of Abstract Modification: No method body, only declaration of method
    2. Abstract modified classes: Abstractions, cannot be instantiated

Attention:

1. A class with an abstract method must be an abstract class, but there can be no abstract method in an abstract class

2. You cannot use abstract to modify private methods, construction methods, static methods

Usage:

1) abstract class not instantiated

2) Concrete classes create instances of subclasses by inheriting abstract classes

3) When subclasses inherit abstract classes, override abstract methods of abstract classes

4) Subclasses must override all abstract methods of the abstract class and its parent class to instantiate . If not overridden, the output is the default method in the abstract class. Abstract class inheritance abstract class can not write abstract methods, has been inherited.

   testabstract {

public static void {

Person p1 = New Student (); //Polymorphic

P1.talk ();

P1.work ();

Application Scenarios:

    1. In some cases, a parent class simply knows how its subclasses should be, but does not know exactly how these subclasses implement these methods (for example, a calculation formula where the circles and rectangles have an area, and the parent class can define a method of an empty area, when comparing the size of the two plots)
    2. Abstract a class from multiple classes with the same attributes, using this abstract class as a template for subclasses, thus avoiding the arbitrariness of sub-class design

Interface : Sometimes it is necessary to derive a subclass from several classes, inheriting their properties and methods. However, multiple inheritance is not supported in Java, that is, there can be only one parent class. With an interface, you can implement multiple inheritance.

Interfaces define the specifications that some classes need to follow, the interfaces do not care about the internal data of these classes, and do not care about the implementation details of the methods in these classes, which only stipulate that certain methods must be provided in these classes.

In essence, an interface is a special kind of abstract class that contains only the definitions of constants and methods, without the implementation of variables and methods.

    1. Using the interface declaration
    2. An interface is a collection of abstract methods and constants, and no other components
    3. The default decoration in an interface method is: public Abatrac
    4. The default decoration for constants in an interface is: public static final

The naming conventions for constants: multiple words, each with uppercase, and each using _ connection.

    1. Implementing an interface using the Implements keyword
    2. A class can implement multiple interfaces, an interface can inherit, or it is meant to inherit

Modifier class class name extends parent class implements interface 1, interface 2 ...

{The class body part, if you inherit an abstract class, you need to implement an abstract method of inheritance; To implement an abstract method in an interface}

Attention:

    1. Interfaces cannot be decorated with private and protect, usually with public
    2. Abstract classes can define constructors! ----> interface is not a constructor!

Summary:

STAITC is intended to share properties, methods, and inner classes, and can be accessed without instantiating the class name.

The list pattern is only one instance of an object, but cannot be instantiated outside the class, only internally instantiated, so the constructor is privatized, the property is privatized, and the Get method is provided. Use static in front, example also use stait, for example

private static person p=new person ();

Abstract A modified method is an abstract method that does not have a method body, and a decorated class abstract class cannot be instantiated. Abstract classes are known to have methods, but specifically how to implement them to subclasses.

An interface is an abstract class with only abstract methods and constants, it can implement multiple inheritance, it does not care about the details, just provide the necessary things.


Inner class : Classes defined inside a class

    1. How do I access the inner class object outside of the class?

① for non-static inner classes: You must first create an external class object, and then create an inner class object

Outerclass oc=new outclass ();

Innerclass ic=oc.new INNERCLSS ();

Ic.test ();

② static inner class: The interior is decorated with static, so the object that creates the class can have no external class object

Staticinnerclass sic=new Outclass.staticinnerclass;

2. Internal classes access member variables for external classes in the following ways

Private int i=1;//member variables for external classes

Class innerclass{

Private int i=2;

Private string Innername= " def "

public void Tese () {

Int i=3;

Sysout (i);//3

Sysout (THIS.I);//2

Sysout (OUTCLASS.THIS.I);

1 if the external member variable is int a=1, write it directly here.

Sysout (Outername);

Sysout (Innername);

Normal access when accessing the method

}

}

Note: Methods in a static inner class cannot access non-static members inside the outside

    1. Anonymous inner class

An implementation class object that creates an interface directly inside a class

Eg:

New Iplaygame () {

public void PlayGame () {

Sysout (Implement interface with Anonymous inner class)}

}.playgame ();

You can also write this in the inside of the class:

Iplaygame ip= New Iplaygame () {

public void PlayGame () {

sysout (" Implement Interface with anonymous inner class ")}} ;

Ip.playgame ();//Access this method through this way



From for notes (Wiz)

5: Object-oriented summary

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.