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

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

时间:2018-04-20 00:11:07      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:bubuko   构造方法   定义   宠物商店   cli   anim   需要   分享   string   

一)学习总结

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

存在的错误:
1、父类向上转型申明的属性,子类只能用其调用父类中含有的方法;
2、向下转型时,必须确认子类和父类存在关系
改正后
技术分享图片
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()) ; 
  } 
}

运行结果为
技术分享图片
)那么,程序的运行结果到底是什么呢?利用eclipse打开println(per)方法的源码,查看该方法中又调用了哪些方法,能否解释本例的运行结果?
技术分享图片

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

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

修改后

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

}
public class Test{  
      public static void main(String args[]){ 
             Person per = new Person("张三",20) ; 
             System.out.println(per);
             System.out.println(per.toString()) ; 
  } 
}

运行结果
技术分享图片

覆写了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修饰
并且需要实现接口中所有的抽象方法,这里需要再实习接口中的run方法。
(二)实验总结
1、宠物商店中,总是不能将需要输入的东西传到数组中,不能很好的写出相关代码;
2、申明数组后,必须对数组内每个成员进行实例化,否则会出现空指针现象
3、继承必须对父类所有方法进行重写。
4、上转型

对象转型

上转型对象

上转型对象:使父类对象引用指向一个子类实例
上转型格式:父类名 父类对象=new 子类名(子类实例);
上转型的叫法:父类对象xxx是子类对象的上转型对象

上转型对象的特点:

上专性对象不能对子类新添加的变量或方法进行操作
上转型对象调用的方法只能是子类重写过的方法以及继承且没有修改的方法
上转型对象可以调用子类继承或隐藏的变量

(三)代码托管(务必链接到你的项目)

https://gitee.com/hebau_java_cs16/Java_CS01ZY/tree/master
码云提交历史截图
技术分享图片

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

标签:bubuko   构造方法   定义   宠物商店   cli   anim   需要   分享   string   

原文地址:https://www.cnblogs.com/zy14122/p/8858723.html

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