• 《Java技术》第三次作业--面向对象——继承、抽象类、接口


    (一)学习总结
    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语句做构造方法的第一条语句。
    运行结果:GrandParent Created.String:Hello.Grandparent.
    Parent Created
    Child Created
    构造一个对象,先调用其构造方法,来初始化其成员函数和成员变量。
    子类拥有父的成员变量和成员方法,如果不调用,则从父类继承而来的成员变量和成员方法得不到正确的初始化。
    不能反过来调用,因为父类根本不知道子类有什么变量,而且这样一来子类也得不到初始化的父类变量,导致程序运行出错!
    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();
        }
    }
    

    子类对象传给父类不能直接调用子类原有的方法,只能调用重写的方法,删除animal.sleep();
    Dog dog = animal;
    类型不匹配:不能自动从 Animal 转换为 Dog,需强制转换, Dog dog = (Dog) animal;
    如果父类引用的对象是父类本身,那么在向下转型的过程中是不安全的,编译不会出错,但是运行时会出现java.lang.ClassCastException错误。它可以使用instanceof来避免出错此类错误。
    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
    输出类对象会默认调用object类的tostring方法
    (2)那么,程序的运行结果到底是什么呢?利用eclipse打开println(per)方法的源码,查看该方法中又调用了哪些方法,能否解释本例的运行结果?

    print方法中调用了write方法,打印了类本身
    (3)在Person类中增加如下方法

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

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

    重写tostring方法后调用的都是重写后的方法
    4.汽车租赁公司,出租汽车种类有客车、货车和皮卡三种,每辆汽车除了具有编号、名称、租金三个基本属性之外,客车有载客量,货车有载货量,皮卡则同时具有载客量和载货量。用面向对象编程思想分析上述问题,将其表示成合适的类、抽象类或接口,说明设计思路。现在要创建一个可租车列表,应当如何创建?
    定义一个抽象类出租车,有抽象方法载客量和载货量,分别定义具有编号、名称、租金三个基本属性的客车类、火车类、皮卡类继承出租车,重写父类的抽象方法,客车实现抽象方法,输出载客量,货车实现抽象方法,输出载货量,皮卡实现抽象方法,输出载客量和载货量,定义租车类,说明出租汽车类数组,构造方法初始化。
    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();
            }
        }
    

    不能,子类中有未实现的父类中的方法,在Dog类中添加方法public void run() {System.out.println("I'm runing");},将eat方法的可视性改为public。
    运行结果
    (二)实验总结
    饲养员定义对象数组时,需要对每一个对象进行实例化。
    银行取款时必须同时满足密码正确余额充足。
    如果父类引用的对象是父类本身,那么在向下转型的过程中是不安全的,编译不会出错,但是运行时会出现java.lang.ClassCastException错误。它可以使用instanceof来避免出错此类错误。
    一个子类只能有一个父类,即单继承,但一个父类可以有多个子类。
    上转型对象不能操作子类新增加的成员变量,不能使用子类新增的方法。
    上转型对象可以操作子类继承或隐藏的成员变量,也可以使用子类继承的或重写的方法。
    上转型对象调用方法时,就是调用子类继承和重写过的方法。而不会是新增的方法,也不是父类原有的方法。
    在接口中定义的常量必须是pubilc static final,这是系统默认的规定,所以常量也可以没有任何修饰符。
    接口中只有方法声明,而无方法实现,接口中声明的方法必须是public abstract,也是系统默认的规定。
    (三)代码托管(务必链接到你的项目)
    码云提交历史截图
    上传实验项目代码到码云,在码云项目中选择“统计-提交”,设置搜索时间段,搜索本周提交历史,并截图。
    https://gitee.com/hebau_java_cs16/Java_CS02CQ

  • 相关阅读:
    leetCode-Two Sum
    leetCode-Pascal's Triangle II
    leetCode-Maximum Average Subarray I
    css 实现垂直水平居中
    poping 心法
    我的机密
    MSMQ消息队列的使用
    生成最大单号 scope_identity
    sqlserver ADO.net 查询数据库加锁,事务提交
    漂亮的JS插件
  • 原文地址:https://www.cnblogs.com/chenqiu/p/8878697.html
Copyright © 2020-2023  润新知