补:课后作业(10.12)

发布时间 2023-10-20 17:10:05作者: HA_wind

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

复制代码
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{}
复制代码

 

 用insterdof来判断两种类型之间是否可以进行相互转化

复制代码
package homework;
public class text
{
    public static void main(String[] args)
    {

        Object hello = "Hello";

        System.out.println("Object对象可以被转换为Object" + (hello instanceof Object));

        System.out.println("Object对象可以被转换为String" + (hello instanceof String));

        System.out.println("Object对象可以被转换为Math" + (hello instanceof Math));

        System.out.println("Object对象可以被转换为Comparable" + (hello instanceof Comparable));
        String a = "Hello";

    }
}
复制代码

 子类可以赋值给父类,反之则不行

复制代码
package homework;

class Mammal{}
class Dog extends Mammal {}
class Cat extends Mammal{}
public class text
{
    public static void main(String args[])
    {
        Mammal m;
        Dog d=new Dog();
        Cat c=new Cat();
        m=d;
        //d=m;
        d=(Dog)m;
        //d=c;
        //c=(Cat)m;

    }
}
复制代码

被注释掉的部分会报错,即无法自动转换类型

复制代码
package homework;

public class text {
    public static void main(String[] args) {
        Parent parent=new Parent();
        parent.printValue();
        Child child=new Child();
        child.printValue();

        parent=child;
        parent.printValue();

        parent.myValue++;
        parent.printValue();

        ((Child)parent).myValue++;
        parent.printValue();

    }
}

class Parent{
    public int myValue=100;
    public void printValue() {
        System.out.println("Parent.printValue(),myValue="+myValue);
    }
}
class Child extends Parent{
    public int myValue=200;
    public void printValue() {
        System.out.println("Child.printValue(),myValue="+myValue);
    }
}
复制代码

 

复制代码
package homework;


public class text {
    public static void main(String[] args) {
        Parent parent=new Parent();
        parent.printValue();
        Child child=new Child();
        child.printValue();

        parent=child;
        parent.printValue();

        parent.myValue++;
        parent.printValue();
        System.out.println(parent.myValue);
        System.out.println(((Child)parent).myValue);

        ((Child)parent).myValue++;
        parent.printValue();

    }
}

class Parent{
    public int myValue=100;
    public void printValue() {
        System.out.println("Parent.printValue(),myValue="+myValue);
    }
}
class Child extends Parent{
public int myValue=200;
public void printValue() { System.out.println("Child.printValue(),myValue="+myValue);
}
}
复制代码

 子类中的同名函数进行了覆盖,父类的函数不再执行,但是变量不会被覆盖,子类中其实存在着两个myValue,一个是从父类中继承来的,另一个是自身定义的

parent=child会将parent默认转换为子类,但是parent还是当作父类使用所以parent。value++输出值是101;