##JAVA作业3

(一)学习总结

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

不同编译,因为在parent构造方法中,调用父类构造方法没有放在第一行。应该将语句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();
    }
}

(1)sleep方法是Dog类中定义的方法,所以不能用Animal类型的变量调用sleep方法
(2)animal是父类的Animal的变量不能将值传给子类Dog的变量
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

System.out.println(per);默认调用父类Object 的toString方法。

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

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

程序的运行结果为:
张三20
张三20
(3)在Person类中增加如下方法

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

重新运行程序,程序的执行结果是什么?说明什么问题?

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

说明子类Person类重复了Object类中toString()方法
4.汽车租赁公司,出租汽车种类有客车、货车和皮卡三种,每辆汽车除了具有编号、名称、租金三个基本属性之外,客车有载客量,货车有载货量,皮卡则同时具有载客量和载货量。用面向对象编程思想分析上述问题,将其表示成合适的类、抽象类或接口,说明设计思路。现在要创建一个可租车列表,应当如何创建?
一个"出租汽车"抽象类,具有编号、名称和租金三个属性,定义两个接口,一个接口里定义载客量,另一个接口定义载货量;
写出“客车类”继承“出租汽车”并实现载客量接口;“货车类”继承“出租汽车”类并实现载货量接口;“皮卡类”继承“出租汽车”
类并实现载货量和载客量接口。写出test类设置信息并展示。

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

不能通过编译,eat方法前main应该加上pubic;没有调用接口中的Run函数
运行结果为:
I'm breathing
I'm eating

(二)实验总结
若子类定义了自己的构造方法,它先执行继承自父类的无参数构造方法,再执行自己的构造方法。
父类的非私有方法均可以被子类继承。

接口(interface)可以被用来实现类间多继承结构。接口内部只能定义 public 的抽象方法和静态的、公有常量,因此所有的方法需要在子类中实现。
接口与类的不同在于:

(1) 没有变量的声明,但可以定义常量。

(2) 只有方法的声明,没有方法的实现。
[public] interface 接口名称 [extends 父接口列表]
{
//抽象方法和全局常量
}
public指明任意类均可使用这个接口,缺省时,表明只有与接口定义在同一个包中的类才可访问此
extends是关键字,父接口列表表示一个接口可以有多个父接口,用逗号分开,而类只能有一个父类。子接口继承父接口中所有的常量和方法。

(三)代码托管(务必链接到你的项目)
https://gitee.com/wangjiaming/hebau_cs01wjm

原文地址:https://www.cnblogs.com/wjm666/p/8884784.html