• 第三次作业


    (一)学习总结

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

     不能通过编译

     运行结果为:

    Constructor call must be the first statement in a constructor
        super()必须在构造方法的第一行。

    修改: 把那个SUPER()语句移动到第一句。

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

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

    1.sleep方法是Dog类中自定义的方法,因此不能Animal类型的变量不能调用sleep方法
    2.animal是父类的Animal的变量不能将值传给子类Dog的变量

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

    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)方法的源码,查看该方法中又调用了哪些方法,能否解释本例的运行结果?
    (3)在Person类中增加如下方法

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

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

    (1). Person类里面没有写tostring方法,测试类里面就直接调用了。

    (2.)程序的运行结果为:
    张三20
    张三20

    (3).程序运行结果为:
    姓名:张三,年龄:20
    姓名:张三,年龄:20

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

    不能编译通过,1.Dog类里面的eat方法应该得有public。

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

  • 相关阅读:
    docker基础命令
    Dubbo添加Filter过滤器
    Jvm FullGC 如何排查?
    ElastaticSearch----es聚合,并获取query的排序结果
    java8 StringJoiner拼接字符串
    ElastaticSearch--- es多字段聚合
    java使用Mysql批量更新(先查询,再插入或更新)
    Mybatis批量插入,批量更新
    谷粒商城学习——P51商品服务-API-三级分类-删除-删除效果细化
    谷粒商城学习——P50商品服务-API-三级分类-删除-逻辑删除
  • 原文地址:https://www.cnblogs.com/endeavor992/p/8886384.html
Copyright © 2020-2023  润新知