动手动脑

动手动脑

 

构造函数即为在对象创建的时候使用为对象分配空间,创建子类对象时,即会创立父类对应值的空间,所以需要调用父类的构造函数。

课后验证

1.

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();

}

}

2.

public class ExplorationJDKSource {

/**

 * @param args

 */

public static void main(String[] args) {

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

}

}

class A{}

 

3.

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());

}

}

 

4

public class ParentChildTest {

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);

}

}

 

5

class Mammal{}

class Dog extends Mammal {}

class Cat extends Mammal{}

public class TestCast

{

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;

}

}

 

6.

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 TestInherits

{

    public static void main(String args[])

 {

         Child c = new Child();

  }

}

 

7.

public class TestInstanceof

{

public static void main(String[] args)

{

//声明hello时使用Object类,则hello的编译类型是ObjectObject是所有类的父类

//hello变量的实际类型是String

Object hello = "Hello";

//StringObject类的子类,所以返回true

System.out.println("字符串是否是Object类的实例:" + (hello instanceof Object));

//返回true

System.out.println("字符串是否是String类的实例:" + (hello instanceof String));

//返回false

System.out.println("字符串是否是Math类的实例:" + (hello instanceof Math));

//String实现了Comparable接口,所以返回true

System.out.println("字符串是否是Comparable接口的实例:" + (hello instanceof Comparable));

String a = "Hello";

//String类既不是Math类,也不是Math类的父类,所以下面代码编译无法通过

//System.out.println("字符串是否是Math类的实例:" + (a instanceof Math));

}

}

 

8

class Parent

        

{

public int value=100;

public void Introduce()

{

         System.out.println("I'm father");

     }

}

class Son extends Parent

{

public int value=101;

      public void Introduce()

{

         System.out.println("I'm son");

}

}

class Daughter extends Parent

{

public int value=102;

public void Introduce()

{

         System.out.println("I'm daughter");

}

}

public class TestPolymorphism

{

    public static void main(String args[])

    {

        Parent p=new Parent();

        p.Introduce();

        System.out.println(p.value);

        p=new Son();

        p.Introduce();

        System.out.println(p.value);

        p=new Daughter();

        p.Introduce();

        System.out.println(p.value);

    }

}

 

原文地址:https://www.cnblogs.com/2210633591zhang/p/11749817.html