java中没有抽象方法的抽象类有什么意义?仅仅是为了让别人不能创建该类的对象?
怪我咯
怪我咯 2017-04-18 10:45:59
0
6
745
怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(6)
伊谢尔伦

The most commonly used place is to implement the template method (Template Method)?
For example, you want to write a batch of local proxies for remote services. For this batch of agents, the steps of obtaining the request address, sending the request, and obtaining the corresponding content are all common. Only the two steps of defining the character encoding type and defining the response callback are different.

At this time, you can define an abstract class and complete the request process. The differences are left to the actual subclasses to define. It's not that the designer [doesn't allow] the abstract class to be instantiated, but that some attributes cannot be determined before knowing the characteristics of the remote service, making instantiation impossible.

The example in JDK is java.io.InputStream. Its read(byte[], int, int) method calls the java.io.InputStream.read() method. Read() is an abstract method and needs to be defined in a subclass. You can learn from the source code.

 public abstract int read() throws IOException;

 public int read(byte b[], int off, int len) throws IOException {
    if (b == null) {
        throw new NullPointerException();
    } else if (off < 0 || len < 0 || len > b.length - off) {
        throw new IndexOutOfBoundsException();
    } else if (len == 0) {
        return 0;
    }

    int c = read();
    if (c == -1) {
        return -1;
    }
    b[off] = (byte)c;

    int i = 1;
    try {
        for (; i < len ; i++) {
            c = read();
            if (c == -1) {
                break;
            }
            b[off + i] = (byte)c;
        }
    } catch (IOException ee) {
    }
    return i;
}
伊谢尔伦
  • You have to see if it inherits any classes or implements any interfaces

  • Most likely some shared methods are placed

  • If not, why not change it to an interface

左手右手慢动作

Some public methods and instance fields may have been extracted.

迷茫

It should not be said that abstract classes without abstract methods have special meaning, but the ordinary meaning of abstract classes. Given a default implementation, there may be less code than directly implementing the interface. On the other hand, what is the point of requiring an abstract class to retain one or two abstract methods?

Peter_Zhu

On the contrary, in order for subclasses of this class to create certain methods. Of course, this is also a requirement of the interface, but the interface requires all methods to be abstract methods. But abstract classes can predefine some methods that do not need to be overridden by subclasses.

Abstract classes and interfaces are all for abstraction. I won’t talk about the benefits of abstraction. For example, we want to implement a Equal接口或是类,定义两个方法 equalunequal。如果用接口实现,一个类必须实现这两个方法。可是如果用抽象类实现,可以把unequal在抽象类里实现为!equal,这样在其子类里就需要再实现unequal.

Personally, I prefer abstract classes (but Java不支持多继承)。比如Compareable接口其实还有一个要求,如果a<b && b < ca < c,接口里不能保证这是正确的。当然抽象类也解决不了,但是对于上面的Equal, the implementation of abstract classes is more in line with additional requirements.

洪涛

Example:
Based on: spring mvc

public abstract class BaseController {
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected HttpSession session;

    @ModelAttribute
    public void setReqAndResp(HttpServletRequest request, HttpServletResponse response){
        this.request = request;
        this.response = response;
        this.session = request.getSession();
    }
    protected Map<String,String> getLoginUser(){
        Object loginUser = request.getAttribute(Config.LOGIN_USER);
        Map<String,String> loginUserMap = new HashMap<>();
        loginUserMap.put(Config.USER_CODE,loginUser==null?"":loginUser.toString());
        return loginUserMap;
            //return (User) session.getAttribute(Constants.LOGIN_USER);
    }
}

Common global properties request response session
Common method getLoginUser()
Of course there will be others (some common properties and methods).
Since there is a controller, service dao can also be used.
I usually extract some reset code into the parent class for use.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template