10.12(动手动脑)

发布时间 2023-10-14 09:39:58作者: 徐星凯

子函数的创建,父函数构造函数的实现情况

package homework;

class Grandparent
{


    public Grandparent()
    {

        System.out.println("GrandParent Created.");

    }


    public Grandparent(String string)
    {

        System.out.println("GrandParent Created.String:" + string);

    }

}



class Parent extends Grandparent
{


    public Parent()
    {

        //super("Hello.Grandparent.");

        System.out.println("Parent Created");

        // super("Hello.Grandparent.");

    }

}



class Child extends Parent
{


    public Child()
    {

        System.out.println("Child Created");

    }

}



public class text
{


    public static void main(String args[])
    {

        Child c = new Child();

    }

}

在执行子类的构造函数时必须先执行父类的构造函数,子类继承父类,首先要定义出一个父类才能定义子类

若不想函数继续被继承下去,可以在class前面加上final代表这是最后一代,无法被继续继承,声明的方法不允许被覆盖,变量不允许更改

public final class text
{
    private final String detail;
    private final String postCode;


    public text()
    {
        this.detail = "";
        this.postCode = "";

    }
    public text(String detail , String postCode)
    {
        this.detail = detail;
        this.postCode = postCode;
    }

    public String getDetail()
    {
        return this.detail;
    }

    public String getPostCode()
    {
        return this.postCode;
    }

    public boolean equals(Object obj)
    {
        if (obj instanceof text)
        {
            text ad = (text)obj;
            if (this.getDetail().equals(ad.getDetail()) && this.getPostCode().equals(ad.getPostCode()))
            {
                return true;
            }
        }
        return false;
    }
    public int hashCode()
    {
        return detail.hashCode() + postCode.hashCode();
    }
package homework;
public class text {

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(new A());
    }

}

class A{}