码迷,mamicode.com
首页 > 编程语言 > 详细

java第三次作业

时间:2018-04-19 21:56:13      阅读:400      评论:0      收藏:0      [点我收藏+]

标签:...   利用   强制   str   托管   如何   覆写   编程   思路   

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.")没有放在第一行,把它放在第一行就行了。
结果为
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.sleep()是因为上转型后Dog类里面的sleep方法并不是继承或者重写的,所以有错,删除这个方法就行了。
Dog dog=animal 是因为向下转型时候应该强制类型转换,应该为Dog dog=(Dog)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
Person类里面没有写tostring方法,测试类里面就直接调用了。
(2)那么,程序的运行结果到底是什么呢?利用eclipse打开println(per)方法的源码,查看该方法中又调用了哪些方法,能否解释本例的运行结果?

(3)在Person类中增加如下方法

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

重新运行程序,程序的执行结果是什么?说明什么问题?
程序执行结果:
姓名:张三,年龄:20
姓名:张三,年龄:20
新增加的tostring方法覆写了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();
        }
    }

不能编译通过,1.Dog类里面的eat方法应该有public
2.Dog类应该覆写所有父类的方法,run()方法没有覆写。
(二)实验总结
排序时,可以用gettime进行比较。
(三)代码托管
https://gitee.com/deep_tenderness/hebau_cs01WSS/tree/master/ex04
技术分享图片

java第三次作业

标签:...   利用   强制   str   托管   如何   覆写   编程   思路   

原文地址:https://www.cnblogs.com/5267wss/p/8884268.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!