Object-oriented Advanced class features

Source: Internet
Author: User

When we write a class, we are actually describing the properties and behavior of its object, and we do not produce the object, only through the new keyword will produce the object, then the system will allocate memory space to the object, its method can be used for external invocation. We sometimes hope that, regardless of whether the object is produced or not, no matter how many objects are produced, there is only one copy of certain data in the memory space, for example, all Chinese people have a country name, each Chinese share the name of the country, It is not necessary to assign a separate variable to represent the country name in each Chinese instance object.

The design idea of class attribute and class method

• A class property is a variable that is shared among objects of that class. When you design a class, you parse which class properties do not change depending on the object, and set these properties to class properties. The corresponding method is set to the class method.

• If the method is not related to the caller, such a method is usually declared as a class method, which simplifies the invocation of a method because it is not necessary to create an object to invoke a class method

Keyword static

• Scope of Use:

Java class, you can use the static Adornment property, method, code block, inner class

• The modified members have the following characteristics:

Load as the class loads

Precedence over the existence of objects

Decorated members, shared by all objects

When access permission is allowed, the object is not created and is called directly by the class

class variables

• Class variables (class attributes) are shared by all forces of the class

Class method

• When there is no instance of an object, you can use the class name. Method Name () to access the class method that is marked by the static.

• Only the static property of the class can be accessed inside the static method, and the non-static properties of the class cannot be accessed

• Because the static method can be accessed without an instance, the static method cannot have this and no super

• Overloaded methods need to be static or non-static at the same time

Single example (singleton) design pattern

The design pattern is the optimal code structure, the programming style, and the problem-solving way after summarizing and theoretical in a lot of practice. The design pattern is like the classic game, different chess game, we use the different game, saves ourselves to think and grope again.

The so-called single-case design pattern, is to take certain methods to ensure that in the whole software system, only one object instance can exist for a class, and the class only provides a method to get its object instance. If we want the class to produce only one object in a virtual machine, we first have to set the access permission of the class's constructor to private so that the class's object cannot be produced outside the class with the new operator, but the object of that class can still be produced inside the class. Because the object of the class cannot be obtained at the beginning of the class, only a static method of the class can be called to return objects created inside the class, and static methods can only access static member variables in the class, so the variables that point to the class object generated inside the class must also be defined as static.

Class single{

Private constructor, cannot create objects of this class outside the class

Private single () {}

Private, accessible only within the class

private static Single Onlyone = new single ();

Getsingle () is static and can be accessed without creating an object

public static single Getsingle () {

return onlyone;

}

}

Understand the syntax of the Main method

Because the Java virtual machine needs to invoke the main () method of the class, the method must have access to public, and because the Java virtual machine does not have to create an object when it executes the main () method, the method must be static, which takes an array parameter of type string. This array holds parameters that are passed to the class being run when Java is executed.

Member of class: initialization block

• Initialization blocks (block of code) function:

Initializing a Java object

• Initialization block in a class if there is a modifier, it can only be modified by static, called static block, and when the class is loaded, the declaration of the class attribute and the static code block order are executed and executed only once.

static blocks are typically used to initialize static (Class) properties

• Non-static code block: code block with no static decoration

1. You can have output statements.

2. You can initialize the properties of a class, the declaration of a class.

3. You can call a static variable or method.

4. If there are multiple non-static blocks of code, follow the top-to-bottom order according to the

Execution.

5. Every time an object is created, it is executed once. and executes before the constructor

• Static code block: a code block decorated with static

1. You can have output statements.

2. You can initialize the properties of a class, the declaration of a class.

3. You cannot initialize a non-static property. That is, you cannot call a non-static genus

Sex and methods.

4. If you have more than one static block of code, execute it sequentially from top to bottom.

5. Static code blocks are executed before non-static blocks of code.

6. Static code block executes only once

Keyword: final

• When declaring classes, properties, and methods in Java, you can use the keyword final to decorate, which means "final."

The final tagged class cannot be inherited. Improve the security, improve the readability of the program.

The final markup method cannot be overridden by a quilt class.

A variable (member or local) of the final tag is called a constant. The name is capitalized and can only be assigned once.
static final: Global constants

Abstract class

• With the definition of a new subclass in the inheritance hierarchy, the classes become more specific, and the parent class is more general and more generic. The design of a class should ensure that the parent class and subclass can share the feature.  Sometimes a parent class is designed so abstractly that it does not have a concrete instance, and such a class is called an abstract class. • When you decorate a class with the abstract keyword, the class is called an abstract class; • When you use abstract to modify a method, the method is called an abstract method. Abstract method: Only the declaration of the method, there is no implementation of the method. End with a semicolon • Classes that contain abstract methods must be declared as abstract classes. • Abstract classes cannot be instantiated. Abstract classes are used to inherit, and subclasses of an abstract class must override the abstract method of the parent class and provide the method body. If you do not rewrite all the abstract methods, it is still abstract. • You cannot use abstract to modify properties, private methods, constructors, static methods, and final methods. Interfaces • Sometimes you must derive a subclass from several classes to inherit all of their properties and methods. However, Java does not support multiple inheritance. With the interface, you can get multiple inheritance effects. • Interface (interface) is a collection of definitions of abstract methods and constant values. In essence, an interface is a special abstract class that contains only the definitions of constants and methods, without the implementation of variables and methods. • A class can implement multiple interfaces,  Interfaces can also inherit the characteristics of other interfaces and interfaces: using interface to define all member variables in an interface is by default modified by public static final.  All methods in the interface are decorated by public abstract by default. There is no constructor interface in the interface with multiple inheritance mechanisms. • The class that implements the interface must provide a concrete implementation of all the methods in the interface before it can be instantiated. Otherwise, it is still an abstract class. • Interfaces are implemented as long as they are implemented. • Similar to inheritance, there is polymorphism between interfaces and implementation classes • If the class implementing the interface does not implement all of the methods in the interface, the class must be defined as an abstract class • The interface can also inherit another interface, using the extends keyword. Summary: • You can implement the same behavior of unrelated classes through an interface, without having to consider the hierarchical relationships between these classes. • Interfaces can be used to specify the methods that multiple classes need to implement, typically to define the expansion capabilities of an object. • Interfaces are primarily used to define specifications. The coupling relationship is lifted. Members of the class: inner class • In Java, the definition of a class is allowed to reside inside another class, which is called an inner class, which is called an outer class.  The Inner class is typically used within the class or statement block that defines it, and the full name must be given when it is referenced externally. The name of the Inner class cannot be the same as the class name that contains it; Inner class can use private data from an external class, as it is a member of an external class, and can be used between members of the same classMutual access. The external class needs to access the members of the inner class: The inner class. member or inner class object. • Classification: Member inner Class (static member inner class and non-static member inner Class) Local inner class (no modifiers), Anonymous inner class inner class attribute · Inner class as a member of a class: Unlike external classes, which can be declared as final, Inner class can be declared as private or protected Inner class can be declared as static. However, the non-static member variables of the outer class can no longer be used at this time; Inner class: Can be declared as an abstract class, so it can be inherited by other inner classes note that a member in a non-static inner class cannot be declared static, only the outer class or static inner class can declare a static member Anonymous Inner classAnonymous inner classes cannot define any static members, methods, and classes, only one instance of an anonymous inner class can be created. An anonymous inner class must be behind new, implementing an interface with its implication or implementing a class.

Object-oriented Advanced class features

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.