继承和接口

动手动脑【继承条件下的构造方法使用】

源程序代码:package com;

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 KaoShi{

public static void main(String args[]) {

        Child c = new Child();

此代码的运行结果为:GrandParent Created.

Parent Created

Child Created

若将public parent类中的代码改为:public Parent() {

        super("Hello.Grandparent.");

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

    则运行的结果如下:GrandParent Created.String:Hello.Grandparent.

Parent Created

Child Created

若将public parent类中的代码改为:public Parent() {

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

       super("Hello.Grandparent.");

程序将会报错:说明通过super调用基类构造方法,必须是子类构造方法中的第一个语句。

动手动脑【finally 类是不可以被继承的,其中的属性也不能被修改】

程序代码:public final class Address

{

private final String detail;

      private final String postCode;//在构造方法里初始化两个实例属性

      public Address()

      {this.detail = "";

           this.postCode = ""}

public Address(String detail , String postCode)

      {this.detail = detail;

           this.postCode = postCode;

      }

      //仅为两个实例属性提供getter方法

      public String getDetail()

      {return this.detail;

      }

public String getPostCode()

      {

            return this.postCode;

      }

      //重写equals方法,判断两个对象是否相等。

      public boolean equals(Object obj)

      {

           if (obj instanceof Address)

           {

                 Address ad = (Address)obj;

                 if (this.getDetail().equals(ad.getDetail()) && this.getPostCode().equals(ad.getPostCode()))

                 {

                      return true}return false

public int hashCode()

      {

           return detail.hashCode() + postCode.hashCode();

      }

 

  public static void main(String args[])

  {

        Address ad=new Address("abc","efg");

        String a=new String();

       System.out.println( ad.getDetail());

        System.out.println(ad.getPostCode());

}

  }

动手动脑【探索jdk的奥秘】

源程序代码:package com;

public  class KaoShi {

public static void main(String[] args) {

           System.out.println(new A());

}   

}

class A{}

运行结果为:com.A@2a139a55

动手动脑【神奇的+号】

程序代码:

public class Fruit

{

       

    public String toString()

    {

        return "Fruit toString.";

    }

 

    public static void main(String args[])

    {

        Fruit f=new Fruit();

        System.out.println("f="+f);

    //  System.out.println("f="+f.toString());

    }

}

运行结果:f=Fruit toString.

结果分析:在+运算中,当任何一个对象与String对象连接时,会隐式地调用其toString()方法,默认情况下,次方法返回“类名@+hashCode。为了返回有意义的信息可以进行重写。

posted on 2015-11-08 10:26  宁宁妞  阅读(187)  评论(0编辑  收藏  举报