How to understand abstract classes and abstract classes
If a lot of people want to buy fruit, there is only one action to eat, but some people need to buy it in cash, some use credit cards, and some use credit accounts. To define a class for everyone, you need to define multiple identical methods to eat. If you define an abstract class, implement the methods to eat in it, and then create an abstract method to buy. The classes of each person are derived from this abstract class. You only need to implement the buy method, and you can directly use the parent class method. If you want to change the methods you want to eat, you don't need to change them. You just need to change the methods in this abstract class.
One interface:
Public interface IGrabber {// <summary> // whether or not the public interface IGrabber is available. /// </Summary> bool Enabled {get; set ;}/// <summary> /// capture the producer name. /// </Summary> string Name {get; set ;}/// <summary> /// Maximum length of the source string. /// </Summary> uint MaxSourceLength {get; set ;}/// <summary> /// minimum length of the source string. /// </Summary> uint MinSourceLength {get; set ;}/// <summary> /// capture. /// </Summary> /// <param name = "source"> source to be crawled. </Param> /// <param name = "target"> capture the result. </Param> // <returns> whether the capture is successful. </Returns> bool Grab (string source, out string [] targets); // <summary> // capture. /// </Summary> /// <param name = "source"> source to be crawled. </Param> /// <param name = "targets"> capture the result. </Param> // <returns> </returns> bool Grab (IList <Keys> source, out string [] targets)
An abstract class:
Public abstract class BaseGrabber: IGrabber {# region IGrabber member. Public virtual bool Enabled {get; set;} public virtual string Name {get; set;} public virtual uint MaxSourceLength {get; set;} public virtual uint MinSourceLength {get; set ;} public abstract bool Grab (string source, out string [] targets); public abstract bool Grab (IList <Keys> source, out string [] targets ); # endregion /// <summary> /// determine whether to reconstruct the data source (identify the impact of arrow keys, deletion keys, and other buttons on the input characters, and return the actual input items ). /// </Summary> public virtual bool IsRestructured {get; set;} protected BaseGrabber () {this. name = this. getType (). name; this. minSourceLength = 0; this. maxSourceLength = 256; this. enabled = true; this. isRestructured = true ;}/// <summary> // reconstruct the data source (identify the effect of keys such as direction keys and deletion keys on the input characters and return the actual input items ). /// </Summary> /// <param name = "source"> </param> /// <returns> </returns> public virtual string RestructureSource (string source) {return source ;}/// <summary> // reconstruct the data source (identify the impact of the arrow keys, deletion keys, and other keys on the input characters, and return the actual input items ). /// </Summary> /// <param name = "source"> </param> /// <returns> </returns> public virtual IList <Keys> RestructureSource (IList <Keys> source) {int count = source. count; for (int I = 0; I <count; I ++) {Keys key = source [I]; switch (key) {case Keys. back: source. removeAt (I); source. removeAt (I-1); I-= 2; count-= 2; break;} return source ;}
I finally figured it out, or the above example is. From the interface to the abstract class, from the abstract class to the class, and from the class to the object. This is object-oriented.
The following are some basic comments about abstract classes:
Abstract classes and abstract methods are modified by abstract
The abstract method does not have a method body.
The derived class of the abstract class must implement the abstract method body.
Abstract classes can only be used as base classes and cannot be instantiated.
The key point is that it cannot be instantiated and can be easily modified for the sake of code clarity.
How do you understand interfaces and abstract classes in your own way?
An abstract class can be used as a program to list the functions that your project should possess. However, it is only a piece of paper to be done. If you build a power station interface for example, you only need to let the user know what type of electric wires and what type of transformer are used to connect them to the outlet, so that the power can be generated from the power station instead of how the power is generated. this is probably the case for reference only.
How do I understand abstract classes and interfaces in java? What are the differences in definition? What are the common functions?
In the concept of object-oriented, we know that all objects are depicted through classes, but not all classes are used to depict objects, if a class does not contain enough information to depict a specific object, such a class is an abstract class.
Abstract classes are often used to represent the abstract concepts we have come up with in the analysis and design of problem domains. They are abstractions of a series of seemingly different but essentially identical specific concepts, we cannot instantiate them (we can't get a specific thing) So it is called abstraction.
For example, we want to describe "Fruit", which is an abstraction. It has some commonalities such as quality and volume (fruit has quality), but lacks features (Apple and orange are both fruits, they have their own characteristics). We can't get the only thing that can represent the fruit (because Apple and orange cannot represent the fruit). It can be described using abstract classes, therefore, abstract classes cannot be instantiated. When we use a class to describe "apple", this class can inherit the abstract class describing "Fruit". We all know that "apple" is a kind of "Fruit ".
In the Object-Oriented field, abstract classes are mainly used to hide types. We can construct a fixed abstract description of a group of actions, but this group of actions can have any specific implementation method. This abstract description is an abstract class, and any specific implementation of this group may be represented by all the derived classes of this abstract class.
All abstract methods in interfaces and abstract classes cannot be implemented in detail. Instead, all abstract methods should be implemented in their subclasses (function bodies should be available, even if {} is empty ), java designers may consider the flexibility of abstract methods. each subclass can implement abstract methods as needed.
Abstract class is defined as follows:
Public abstract class AbstractClass // there must be at least one abstract method in it
{
Public int t; // common data member
Public abstract void method1 (); // abstract method. The subclass of an abstract class must implement the abstract method in the abstract class.
Public abstract void method2 ();
Public void method3 (); // non-abstract Method
Public int method4 ();
Publi int method4 (){
...... // The default behavior that can be granted to non-abstract methods in abstract classes, that is, the specific implementation of methods
}
Public void method3 (){
...... // The default behavior that can be granted to non-abstract methods in abstract classes, that is, the specific implementation of methods
}
}
The interface is defined as follows:
Public interface Interface
{
Static final int I; // The interface cannot contain common data members. It can only have static data members that cannot be modified. static indicates global, and final indicates unchangeable, static final modification is not required and is implicitly declared as static and final.
Public void method1 (); // methods in the interface must be abstract methods, so abstract modification is not required.
Public void method2 (); // The interface cannot assign the default behavior of the method, that is, there cannot be specific implementation of the method.
}
In short, an abstract class is a class with incomplete functions. An interface is only a set of abstract method statements and static data that cannot be modified. Neither of them can be instantiated.
In a sense, an interface is a special form of abstract class. in java, an abstract class represents an inheritance relationship.