从主类波谷多态调用toString

时间:2018-11-01 02:25:06

标签: java inheritance polymorphism

我有一个算法,该算法具有一个名为<head runat="server"> <title></title> <script src="Scripts/jquery-1.10.2.min.js"></script> <script type="text/javascript"> !function ($, window) { $(function () { init(); }); var getAddresses = function (postcode, callback) { //var $xhr = $.getJSON('/lookupPostcode/' + postcode); //$xhr.done(function (data) { // callback(data); //}); //$xhr.error(function () { // callback([]); //}) //using the following array to populate the select. var datalist = [{ "ID": "1", "ParentID": "0", "Name": "Parent1" }, { "ID": "2", "ParentID": "0", "Name": "Parent2" }, { "ID": "3", "ParentID": "1", "Name": "Child 1.1" }, { "ID": "4", "ParentID": "1", "Name": "Child 1.2" }, { "ID": "5", "ParentID": "3", "Name": "SubChild 1.1.1" }, { "ID": "6", "ParentID": "2", "Name": "Child 2.1" }]; callback(datalist); }; var init = function () { $("input.address").each(function () { var $input = $(this); var $icon = $input.next('.address-button'); //you are using the .row class to find the select control, but from your form, I can't find this class. var $select = $input.parents('.row').eq(0).find("select"); //debugger; $select.css("display", "none"); var onKeyUp = function (e) { if (e.keyCode === 13) { e.preventDefault(); e.stopPropagation(); $icon.trigger("click"); } }; var onKeyDown = function (e) { if (e.keyCode === 13) { e.preventDefault(); e.stopPropagation(); } }; $input.on("keyup", onKeyUp); $input.on("keydown", onKeyDown); $icon.on("keyup", onKeyUp); $icon.on("keydown", onKeyDown); $select.on("keyup", onKeyUp); $icon.on("click", function () { $select.empty(); $select.append("<option value=''>Enter above</option>"); getAddresses($input.val(), function (addresses) { //populate select options with addresses $.each(addresses, function (index, item) { //debugger; $select.append("<option value='" + item.ID + "'>" + item.Name + "</option>"); }); $select.css("display", "block"); }); }); $select.on('change', function (event) { var ua = window.navigator.userAgent; var is_ie = /MSIE|Trident/.test(ua); //get the selected text and populate the input text. $input.val($(this).find("option:selected").text()); //hide the select control if (!is_ie) { $select.css("display", "none"); } $select.css("display", "none"); }); }); }; }(jQuery, window); </script> </head> <body> <form action="http://localhost:8000/processForm" method="post"> <label for="input_1" class="form-control-label col-xs-12"> Postcode </label> <div class="col-xs-12 row"> <div class="input-group"> <input type="text" name="questions[1]" autocomplete="off" id="input_1" class="form-control address" value="" /> <a class="btn btn-xs input-group-addon address-button" id="input_address_addon" role="button" tabindex="0"> <img src="http://i.imgur.com/H9FIags.jpg" style="height:10px;width:10px" /> Search </a> </div> <div class="col-xs-12 col-sm-8 col-sm-offset-4 col-md-7"> <select class="form-control selectpicker addSelect" id="input_address_select" > <option value="">Enter above</option> </select> </div> </div> <button type="submit" class="btn submit btn-navigation"> Continue </button> </form> </body> 的通用类以及一些从Employee继承的特定类,如下所示:

Employee

我正在寻找一种避免单独调用每个对象并从继承自especific类的abstract class Employee { private String name; private String document; public String toString(){ return "\nName and Document"; } } class Driver extends Employee { private String licencePlate; public String toString(){ return "Plate: XXXXX"; } } class TelOperator extends Employee { private String phone; public String toString(){ return "Phone: XXX - XXXXXX"; } } class Ideone { public static void main (String[] args) throws java.lang.Exception { TelOperator telOp = new TelOperator(); System.out.println(telOp); Driver driver = new Driver(); System.out.println(driver); } } 执行主类的toString方法的方法。 我的输出将是这样的:

//驱动程序

  1. 名称和文件
  2. 盘子:XXXXX

//电话接线员

  1. 名称和文件
  2. 电话:XXX-XXXXX

您可以检查源代码here

3 个答案:

答案 0 :(得分:0)

只需在子类的super.toString()方法中添加对toString()的调用:-

public String toString() {
    return super.toString() + "\nPhone: XXX - XXXXXX";
}

答案 1 :(得分:0)

如果要一次打印两个子类,则必须创建一个子类的对象并在主对象的toString中调用它们的toString。希望我能正确理解您的问题。

答案 2 :(得分:0)

您可以为员工创建一个Factory类,然后工厂决定应将哪个子类返回给调用程序。然后,它将创建该子类的实例,然后将其返回到调用程序。

希望我能正确理解您的问题。