JAVA第四次作业

《Java技术》第四次作业

(一)学习总结

1.学习使用思维导图对Java面向对象编程的知识点(封装、继承和多态)进行总结。

参考资料: XMind

2.阅读下面程序,分析是否能编译通过?如果不能,说明原因。应该如何修改?程序的运行结果是什么?为什么子类的构造方法在运行之前,必须调用父 类的构造方法?能不能反过来?

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() {        
        System.out.println("Parent Created");
        super("Hello.Grandparent.");
    }
}
class Child extends Parent {
    public Child() {
        System.out.println("Child Created");
    }
}
public class Test{
    public static void main(String args[]) {
        Child c = new Child();
    }
}

不能通过编译,
原因:构造函数调用必须是构造函数中的第一个语句
super("Hello.Grandparent.");语句与System.out.println("Parent Created");换一下位置。
运行结果:

GrandParent Created.String:Hello.Grandparent.
Parent Created
Child Created    

3 . 阅读下面程序,分析程序中存在哪些错误,说明原因,应如何改正?正确程序的运行结果是什么?

class Animal{
  void shout(){
      System.out.println("动物叫!");
  }
}
class Dog extends Animal{
      public void shout(){  
          System.out.println("汪汪......!");  
     }
      public void sleep() {
       System.out.println("狗狗睡觉......");
      } 
}
public class Test{
    public static void main(String args[]) {
        Animal animal = new Dog(); 
        animal.shout();
        animal.sleep();
        Dog dog = animal;
        dog.sleep(); 
        Animal animal2 = new Animal();
        dog = (Dog)animal2;
        dog.shout();
    }
}

(1).错误:没有为类型 Animal 定义方法 sleep()
原因:上转型对象调用方法时,调用的是子类继承和重写过的方法,而sleep是新增的方法
改正:将上转型对象强制转换到一个子类对象((Dog) animal).sleep();
(2).错误:类型不匹配:不能从 Animal 转换为 Dog
原因:对于向下转型,需要强制转型,并且父类是用本身类实例化对象的,并不知道谁是它的子类,转化时就会出现错误。
改正: Dog dog = (Dog) animal;
(3).错误:运行出现错误

原因:父类引用的对象是父类本身,在向下转型的过程是不安全的。
改正:使用instanceof

        if(animal2 instanceof Dog){
        	Dog dog1 = (Dog)animal2;
        	dog1.shout();
        }

4.运行下列程序

class Person { 
   private String name ; 
   private int age ; 
   public Person(String name,int age){ 
         this.name = name ; 
         this.age = age ; 
   } 
}
public class Test{  
      public static void main(String args[]){ 
             Person per = new Person("张三",20) ; 
             System.out.println(per);
             System.out.println(per.toString()) ; 
  } 
}

(1)程序的运行结果如下,说明什么问题?

Person@166afb3
Person@166afb3
以上程序是输出了一些地址信息,从程序的运行结果可以发现,加与不加toString最终的输出结果是一样的,也就是说对象输出时一定会调用Object类中的toString方法打印出内容。
(2)那么,程序的运行结果到底是什么呢?利用eclipse打开println(per)方法的源码,查看该方法中又调用了哪些方法,能否解释本例的运行结果?
源码:

public void println(Object x) {
        String s = String.valueOf(x);
        synchronized (this) {
            print(s);
            newLine();
        }
    }

调用了valueOf(x)方法,
如果参数为空字符串,则返回值为空;不为空,则返回tostring()的返回值。
(3)在Person类中增加如下方法

public String toString(){ 
        return "姓名:" + this.name + ",年龄:" + this.age ; 
 } 

重新运行程序,程序的执行结果是什么?说明什么问题?
可参考教材P229
运行结果:

名字:张三, 年龄:20
名字:张三, 年龄:20

程序的Person类中覆写了Object类中的toString方法,这样直接输出对象时调用的是被子类覆写过的toString方法。

5.其他需要总结的内容。

(二)实验总结

1.员工

  • 程序设计思路:先定义Employee类,具有姓名,年龄,性别属性,定义Manager类,继承Employee类,具有职务,年薪属性,定义Staff类,继承Employee类,具有部门,月薪属性,最后定义Test类,输出员工的所有信息。

2.类的设计

  • 程序设计思路:设计平面图形抽象类,在类里提供对象周长和面积的方法,设计矩形类、三角形类、圆形类,继承平面图形抽象类,并在各自的类里提供求该类对象周长和面积的方法。设计立体图形抽象类,在类里提供对象表面积和体积的方法,设计球类、圆柱类、圆锥类,继承立体图形抽象类,并在各自的类里提供求该类对象表面积和体积的方法。定义测试类,输出求出的数据。

3.参考类图,重构下面的实例,分析和理解多态的含义和用途
(1)某动物园有一饲养员小李,每天需要给他所负责饲养的一只狮子、五只猴子和十只鸽子喂食。 请用一个程序来模拟他喂食的过程。

分析这种编程方式有什么不合理的地方。

  • 程序麻烦

(2)第一次重构,引入继承,利用抽象类和对象多态重构程序,Animal类采用抽象类, 合并Feeder类中的方法

分析程序是否还可以进一步优化
(3)第二次重构,修改feedAnimals方法,让它接收一个Animal数组

  • 程序设计思路:定义抽象类Animal,给出eat方法,定义狮子类、猴子类、鸽子类都继承Animal,并在类里定义相应的方法,在方法里输出信息,最后定义测试类,输出所有信息。
  • 实验问题分析:
    问题1:运行出现错误

    原因:没有掌握向上转型的原理。
    解决方案:将程序改为

(三)代码托管

  • 码云commit历史截图
posted @ 2017-04-17 22:43  luhan1  阅读(203)  评论(0编辑  收藏  举报