zoukankan      html  css  js  c++  java
  • java第三次作业

    《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关键字必须放在子类构造方法的首行。

    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父类无法调用该方法

    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方法,直接输出函数名默认调用toString方法。
    (2)那么,程序的运行结果到底是什么呢?利用eclipse打开println(per)方法的源码,查看该方法中又调用了哪些方法,能否解释本例的运行结果?
    该方法调用了getClass()方法,getName()方法, Integer.toHexString()方法。
    所以最后返回了类名+@字符+引用地址。
    (3)在Person类中增加如下方法

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

    姓名:张三,年龄:20
    姓名:张三,年龄:20
    在Person类中覆写了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();
            }
        }
    

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

    码云

    https://gitee.com/hebau_java_cs16/Java_cs01Yxh/tree/master

  • 相关阅读:
    nginx自定义header支持
    使用 navicat 导入导出数据库
    eureka 服务注册与发现
    反射应用之动态代理
    接口应用之“静态代理”
    工厂模式
    计算程序运行的时间(以求得1-10000之间的素数为例)
    抽象类的模板方法设计模式
    ==和equals()方法的区别
    单例设计模式之饿汉式和懒汉式的区别
  • 原文地址:https://www.cnblogs.com/web5217/p/8886776.html
Copyright © 2011-2022 走看看