简体   繁体   English

抽象类和子类以及方法调用

[英]Abstract class and subclass, and method invocation

Good day everybody. 大家好

I have some confusion about abstract superclass and subclasses in servlets: 我对servlet中的抽象超类和子类有些困惑:

I have abstract servlet superclass: 我有抽象的servlet超类:

public abstract class CatalogPage extends HttpServlet {


    public CatalogPage() {

        super();

    }

    private CatalogItem[] items;

    private String[] itemsID;

    private String title;



    public void setItems(String[] itemsID) {

        this.itemsID = itemsID;

        items = new CatalogItem[itemsID.length];

        for(int i=0; i<items.length; i++) {

                items[i] = Catalog.getItem(itemsID[i]);

            }   
    }



    public void setTitle(String title) {
        this.title = title;
    }



    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

            if (items == null) {

                response.sendError(response.SC_NOT_FOUND, "Missing Items");         
                return;
            }

            response.setContentType("text/html");

            PrintWriter out = response.getWriter();
            String docType =
              "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
              "Transitional//EN\">\n";
            out.println(docType +
                        "<HTML>\n" +
                        "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
                        "<BODY BGCOLOR=\"#FDF5E6\">\n" +
                        "<H1 ALIGN=\"CENTER\">" + title + "</H1>");

            CatalogItem item;

            for(int i=0; i<items.length; i++) {

                    item = items[i];


            if(item == null) {

                out.println("<FONT COLOR=\"RED\">" +
                        "Unknown item ID " + itemsID[i] +
                        "</FONT>");


                    }  else {


                out.println();
                String formURL = "OrderPage";

                formURL = response.encodeURL(formURL);

                out.println
                  ("<FORM ACTION=\"" + formURL + "\">\n" +
                   "<INPUT TYPE=\"HIDDEN\" NAME=\"itemID\" " +
                   "        VALUE=\"" + item.getItemID() + "\">\n" +
                   "<H2>" + item.getShortDescription() +
                   " ($" + item.getCost() + ")</H2>\n" +
                   item.getLongDescription() + "\n" +
                   "<P>\n<CENTER>\n" +
                   "<INPUT TYPE=\"SUBMIT\" " +
                   "VALUE=\"Add to Shopping Cart\">\n" +
                   "</CENTER>\n<P>\n</FORM>");

                    out.println("<HR>\n</BODY></HTML>");

            }

        }

    }

}

And of abstract superclass, subclass: 以及抽象超类,子类:

@WebServlet("/KidsCatalogPage")
public class KidsBooksPage extends CatalogPage {

    public void init() {

            String[] kbp = {"lewis001", "alexander001", "rowling001"};  

            setItems(kbp);
            setTitle("All-Time Best Children's Fantasy Books");

    }

}

If I invoke for subclass KidsBooksPage I know it do initialisation init() method first, but the question is: 如果我为子类KidsBooksPage调用,我知道它首先执行初始化init()方法,但是问题是:

What make my subclass KidsBooksPage invoke abstract super class CatalogPage . 是什么使我的子类KidsBooksPage调用抽象超类CatalogPage How it works?? 这个怎么运作?? Can Understand it. 可以理解。 Please explain to me. 请给我解释一下。

Thank you. 谢谢。

As Jon Skeet said in comments, this is about a core concept (Inheritance) in Object-Oriented programming (OOP), has nothing to do with Servlets specifically (except maybe your homework 😉), and is not really a question for StackOverflow.com. 正如Jon Skeet在评论中所说的那样,这是关于面向对象编程(OOP)中的核心概念(继承),与Servlet无关(也许与您的作业home无关),对于StackOverflow.com而言,这并不是真正的问题。 。

If you understand conventional programming and how conventional compiling and linking predetermine at compile-time exactly what bits will be executed at run-time, and therefore wonder how execution can seem to "jump around" between classes, then you need to learn about 'late binding' & 'dynamic dispatch', the special sauce that makes OOP so powerful. 如果您了解常规编程以及常规的编译和链接方式如何在编译时准确地确定运行时将执行哪些位,因此想知道执行看起来如何在类之间“跳来跳去”,那么您需要学习“后期”绑定”和“动态调度”,使OOP如此强大的特殊调味料。

'abstract' is a bit of distraction regarding your question. 关于您的问题,“抽象”有点让人分心。 Simply means that a concrete subclass is required, but without abstract your question remains the same. 只是意味着需要一个具体的子类,但是如果没有abstract您的问题将保持不变。

Try reading up on Inheritance and Late Binding. 尝试阅读继承和后期绑定。

What Is Inheritance? 什么是继承?

Inheritance 遗产

Late Binding 后期绑定

Dynamic Dispatch 动态调度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM