《Java技术》第XX次作业

(一)学习总结

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

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.")”放在子类Parent的构造方法的第一句。

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

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

sleep()方法为子类中新增的方法,故而animal父类无法调用该方法

改后的程序:

class Animal{
  void shout(){
    System.out.println("动物叫!");
}
void sleep(){
     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 = (Dog)animal;
        dog.sleep(); 
        Animal animal2 = new Dog();
        dog = (Dog)animal2;
        dog.shout();
    }
}

3.运行下列程序

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

(2)那么,程序的运行结果到底是什么呢?利用eclipse打开println(per)方法的源码,查看该方法中又调用了哪些方法,能否解释本例的运行结果?

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

(3)在Person类中增加如下方法

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

运行结果:
姓名:张三,年龄:20
姓名:张三,年龄:20
说明了,子类Person类重复了Object类中toString()方法

5.阅读下面程序,分析代码是否能编译通过,如果不能,说明原因,并进行改正。如果能,列出运行结果:

interface Animal{    
        void breathe();
        void run();
        void eat();
    }
    class Dog implements Animal{
        public void breathe(){
            System.out.println("I'm breathing");
        }
        void eat(){
            System.out.println("I'm eating");
        }
    }
    public class Test{
        public static void main(String[] args){
            Dog dog = new Dog();
            dog.breathe();
            dog.eat();
        }
    }

不能:接口中方法,默认是public abstract 所以在子类实现抽象方法时,应该用public修饰

(二)实验总结

一个思想,一个和数据库类似的两层模式三级映像的模式思想

2.按照下面要求完成类的设计

(1)设计一个平面图形抽象类(提供求该类对象周长和面积的方法)和一个立体图形抽象类(提供求该类对象表面积和体积的方法)
(2)设计球类、圆柱类,圆锥类、矩形类、三角形类、圆类,分别继承平面图形抽象类和立体图形抽象类。
(3)建立测试类,进行测试。画出类图。

3.. 参考类图,重构下面的实例,分析和理解多态的含义和用途

(1)某动物园有一饲养员小李,每天需要给他所负责饲养的一只狮子、五只猴子和十只鸽子喂食。 请用一个程序来模拟他喂食的过程
(2)第一次重构,引入继承,利用抽象类和对象多态重构程序,Animal类采用抽象类, 合并Feeder类中的方法
(3)第二次重构,修改feedAnimals方法,让它接收一个Animal数组

(三)[代码托管]git@gitee.com:java1601/first_java_operation.git

  • 码云提交历史截图
posted on 2018-04-19 21:30  燕超  阅读(134)  评论(1编辑  收藏  举报