• 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调用的是父类构造函数,调用父类构造函数要写在第一个
    修改方法:把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()
    对于向下转型,需要强制转型,即必须明确指明要转型的子类类型: 格式:子类名称 子类对象 =(子类)父类实例;
    改正:

    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 = (Dog) animal;
            dog.sleep(); 
            Animal animal2 = new Animal();
            if(animal2 instanceof 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

    说明:System.out.println(per);默认调用父类Object 的toString方法
    (2)那么,程序的运行结果到底是什么呢?利用eclipse打开println(per)方法的源码,查看该方法中又调用了哪些方法,能否解释本例的运行结果?
    如果参数为空字符串,则返回空,否则,返回toString()的返回值。
    toString()返回一个字符串用于描述当前对象,返回的具体内容:类名@对象的hash码十六进制表示
    (3)在Person类中增加如下方法

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

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

    执行结果:
    姓名:张三,年龄:20
    姓名:张三,年龄:20
    说明在Person类中完成了对父类Object的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类中必须重写接口中的所有方法,在Dog类中再加上void run()方法

    (二)实验总结
    随着Java学习时间越来越长,也对Java越来越熟悉了,也理解了一些面向对象,总的来说,遇到的问题自己大多都能解决
    起初对于向上转型和向下转型不太理解,后来多看了看课本就理解了
    (三)代码托管

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

  • 相关阅读:
    ASP.net中页面事件的先后顺序
    我回来了
    ASP.NET中添加引用不能显示
    VS2008中MVC无法打开项目文件,此安装不支持该项目类型
    windows2003 IIS错误
    C#中使用TimeSpan计算两个时间的差值
    javascript做在翻译
    GridView导出EXCEL
    用资源管理器限制大数据量查询
    linux单机配置DG过程记录
  • 原文地址:https://www.cnblogs.com/jg666666/p/8886568.html
Copyright © 2020-2023  润新知