java扩展类继承

时间:2020-10-15 04:36:14

标签: java subclass

Sum s = new Sum();
Sum.SetToZero z = new Sum.SetToZero();

Scanner input = new Scanner(System.in);
String read = input.nextLine();

while (!read.equals("end")) {

    if (read.equals("add")) {
        s.add()
    } 
    else if (read.equals("get")) {
        System.out.println(s.returnTotal());
    }
    else if (read.equals("zero")) {
        z.zero();
    }

   read = input.nextLine();
}

班级:

public class Sum {

    int total = 0;

    public void add() {
        total += 1;
    }

    public int returnTotal() {
        return total;
    }

    public static class SetToZero extends Sum {

        public void  zero() {
            total = 0;
        }
    }
}

输入:

add
add
zero
add
get
add
get
end

输出:

3
4

想要的输出:

1
2

子类不应该继承总数并将其设置为零吗?我究竟做错了什么?我知道我可以将zero移入主要课程,但我希望将其放在单独的课程中。感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

通过将static设置为变量class Sum { static int total = 0; public void add() { total += 1; } public int returnTotal() { return total; } public static class SetToZero extends Sum { public void zero() { total = 0; } } } ,可以获得所需的输出。

 <?xml version="1.0" encoding="UTF-8" ?> 
- <!--  Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.3.0-b170407.2038 svn-revision#2eaca54d17a59d265c6fe886b7fd0027836c766c. 
  --> 
- <wsdl:definitions xmlns:tns="http://ouaf.oracle.com/webservices/c1/C1-AccountMail" xmlns:ouaf="http://ouaf.oracle.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" name="C1-AccountMail" targetNamespace="http://ouaf.oracle.com/webservices/c1/C1-AccountMail" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wssutil="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
  <wsp:UsingPolicy wssutil:Required="true" /> 
  <wsdl:documentation>C1-AccountMail version 1: Retrieve an Account's Mailing Address</wsdl:documentation> 
- <wsdl:types>
- <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ouaf="http://ouaf.oracle.com/" targetNamespace="http://ouaf.oracle.com/" elementFormDefault="unqualified">
- <xsd:element name="Fault">
- <xsd:complexType>
- <xsd:all>
  <xsd:element name="ResponseStatus" type="xsd:string" /> 
  <xsd:element name="ResponseCode" type="xsd:int" /> 
  <xsd:element name="ResponseText" type="xsd:string" /> 
- <xsd:element name="ResponseData" minOccurs="0">
- <xsd:complexType>
  <xsd:attribute name="parm1" type="xsd:string" /> 
  <xsd:attribute name="parm2" type="xsd:string" /> 
  <xsd:attribute name="parm3" type="xsd:string" /> 
  <xsd:attribute name="parm4" type="xsd:string" /> 
  <xsd:attribute name="parm5" type="xsd:string" /> 
  <xsd:attribute name="parm6" type="xsd:string" /> 
  <xsd:attribute name="parm7" type="xsd:string" /> 
  <xsd:attribute name="parm8" type="xsd:string" /> 
  <xsd:attribute name="parm9" type="xsd:string" /> 
  <xsd:attribute name="text" type="xsd:string" /> 
  <xsd:attribute name="category" type="xsd:string" /> 
  <xsd:attribute name="numParm" type="xsd:int" /> 
  <xsd:attribute name="number" type="xsd:int" /> 
  </xsd:complexType>
  </xsd:element>
- <xsd:element name="ServerMessage" minOccurs="0">
- <xsd:complexType>
- <xsd:all>
  <xsd:element name="Category" type="xsd:string" /> 
  <xsd:element name="Number" type="xsd:string" /> 
  <xsd:element name="CallSequence" type="xsd:string" /> 
  <xsd:element name="ProgramName" type="xsd:string" /> 
  <xsd:element name="Text" type="xsd:string" /> 
  <xsd:element name="Description" type="xsd:string" /> 
  <xsd:element name="Table" type="xsd:string" /> 
  <xsd:element name="Field" type="xsd:string" /> 
  </xsd:all>
  </xsd:complexType>
  </xsd:element>
  </xsd:all>
  </xsd:complexType>
  </xsd:element>
- <xsd:simpleType name="money">
  <xsd:restriction base="xsd:decimal" /> 
  </xsd:simpleType>
- <xsd:simpleType name="listAction">
- <xsd:restriction base="xsd:token">
  <xsd:enumeration value="delete" /> 
  <xsd:enumeration value="add" /> 
  <xsd:enumeration value="update" /> 
  </xsd:restriction>
  </xsd:simpleType>
  </xsd:schema>
- <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ouaf="http://ouaf.oracle.com/" targetNamespace="http://ouaf.oracle.com/webservices/c1/C1-AccountMail" elementFormDefault="qualified">
  <xsd:import namespace="http://ouaf.oracle.com/" /> 
- <xsd:element name="C1-AccountMail">
- <xsd:complexType>
- <xsd:sequence>
- <xsd:element name="accountId" minOccurs="0">
- <xsd:simpleType>
- <xsd:restriction base="xsd:string">
  <xsd:maxLength value="65536" /> 
  </xsd:restriction>
  </xsd:simpleType>
  </xsd:element>
- <xsd:element name="results" minOccurs="0" maxOccurs="unbounded">
- <xsd:complexType>
- <xsd:sequence>
- <xsd:element name="premise" minOccurs="0">
- <xsd:simpleType>
- <xsd:restriction base="xsd:string">
  <xsd:maxLength value="4000" /> 
  </xsd:restriction>
  </xsd:simpleType>
  </xsd:element>
- <xsd:element name="premiseInfo" minOccurs="0">
- <xsd:simpleType>
- <xsd:restriction base="xsd:string">
  <xsd:maxLength value="4000" /> 
  </xsd:restriction>
  </xsd:simpleType>
  </xsd:element>
  </xsd:sequence>
  <xsd:attribute name="action" type="ouaf:listAction" /> 
  </xsd:complexType>
  </xsd:element>
  </xsd:sequence>
  </xsd:complexType>
  </xsd:element>
  </xsd:schema>
  </wsdl:types>
- <!--  wsdl:message elements describe potential transactions --> 
- <!--  request for operation C1-AccountMail --> 
- <wsdl:message name="C1-AccountMailRequest">
  <wsdl:part name="body" element="tns:C1-AccountMail" /> 
  </wsdl:message>
- <!--  response for operation C1-AccountMail --> 
- <wsdl:message name="C1-AccountMailResponse">
  <wsdl:part name="body" element="tns:C1-AccountMail" /> 
  </wsdl:message>
- <!--  fault type --> 
- <wsdl:message name="Fault">
  <wsdl:part name="fault" element="ouaf:Fault" /> 
  </wsdl:message>
- <!--  wsdl:portType describes messages in an operation --> 
- <wsdl:portType name="C1-AccountMailPortType">
  <wsdl:documentation>C1-AccountMail version 1: Retrieve an Account's Mailing Address</wsdl:documentation> 
- <wsdl:operation name="C1-AccountMail">
  <wsdl:input message="tns:C1-AccountMailRequest" /> 
  <wsdl:output message="tns:C1-AccountMailResponse" /> 
  <wsdl:fault name="fault" message="tns:Fault" /> 
  </wsdl:operation>
  </wsdl:portType>
- <!--  wsdl:binding states a serialization protocol for this service --> 
- <wsdl:binding name="C1-AccountMailSoapBinding" type="tns:C1-AccountMailPortType">
  <wsp:PolicyReference URI="policy:Wssp1.2-2007-Https-BasicAuth.xml" /> 
- <!-- leverage off soap:binding document style @@@(no wsdl:foo pointing at
    the soap binding) --> 
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 
- <!-- semi-opaque container of network transport details classed by
      soap:binding above @@@ --> 
- <wsdl:operation name="C1-AccountMail">
- <!--  again bind to SOAP? @@@ --> 
  <soap:operation soapAction="http://10.1.5.86:6500/webservices/C1-AccountMail#C1-AccountMail" /> 
- <!-- further specify that the messages in the wsdl:operation
            "C1-AccountMail" use SOAP? @@@ --> 
- <wsdl:input>
  <soap:body use="literal" /> 
  </wsdl:input>
+ <wsdl:output>
  <soap:body use="literal" /> 
  </wsdl:output>
- <wsdl:fault name="fault">
  <soap:fault name="fault" use="literal" /> 
  </wsdl:fault>
  </wsdl:operation>
  </wsdl:binding>
- <!--  wsdl:service names a new service "C1-AccountMailService" --> 
+ <wsdl:service name="C1-AccountMailService">
  <wsdl:documentation>C1-AccountMail version 1: Retrieve an Account's Mailing Address</wsdl:documentation> 
- <!--  connect it to the binding "C1-AccountMailSoapBinding" above --> 
- <wsdl:port name="C1-AccountMailPort" binding="tns:C1-AccountMailSoapBinding">
- <!--  give the binding a network address --> 
  <soap:address location="http://10.1.5.86:6500/ouaf/webservices/C1-AccountMail" /> 
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>

答案 1 :(得分:0)

除了名称中指出的内容外,例如不使用小写字母开头的类名;我认为它不起作用的原因是因为您为SumSum.SetToZero使用了两个不同的变量实例。由于SetToZero具有Sum的所有属性,因此您无需创建新变量。我认为您应该对此进行更改:

Sum s = new Sum();
Sum.SetToZero z = new Sum.SetToZero();
Sum.SetToZero s = new Sum.SetToZero(); // use s for all operations 

这是修改后的main方法的外观:


public static void main(String[] args) {
    Sum.SetToZero s = new Sum.SetToZero();

    Scanner input = new Scanner(System.in);
    String read = input.nextLine();

    while (!read.equals("end")) {

        if (read.equals("add")) {
            s.add();
        } 
        else if (read.equals("get")) {
            System.out.println(s.get());
        }
        else if (read.equals("zero")) {
            s.zero();
        }

        read = input.nextLine();
    }
}

运行此命令时,我看到了预期的输出:

src : $ java Sum
add
add
zero
add
get
1
add
get
2
end