Java第四次作业

(一)学习总结

1.学习使用思维导图对Java面向对象编程的知识点(封装、继承和多态)进行总结。

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

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("Hello.Grandparent.");放在子类的构造方法之前
class Parent extends Grandparent {
public Parent() {     
	super("Hello.Grandparent.");
    System.out.println("Parent Created");
      
}
}
不能反过来,super调用父类中指定构造方法的操作,语句必须放在子类构造方法的首行。

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

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 animal = new Dog() 是dog的向上转型
父类animal里没有sleep方法
Dog dog = animal发生向下转型
dog = (Dog)animal2;
修改后
    class Animal{
  void shout(){
      System.out.println("动物叫!");
       
}
  public void sleep() {
	// TODO Auto-generated method stub
	  System.out.println("汪汪......!");  
}
  class Dog extends Animal{
      public void shout(){  
          System.out.println("汪汪......!");  
     }
      public void sleep() {
          System.out.println("狗狗睡觉......");
         } 
}
public void sleep1() {
	// TODO Auto-generated method stub
    //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();
        dog = (Dog)animal2;
        dog.shout();
    }
}
运行结果
动物叫!
汪汪......!
汪汪......!

4.运行下列程序

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
说明不管加不加toString方法,他们的运行结果都是一样的,对象输出时一定会调用toString方法打印内容

(2)那么,程序的运行结果到底是什么呢?利用eclipse打开println(per)方法的源码,查看该方法中又调用了哪些方法,能否解释本例的运行结果?
(3)在Person类中增加如下方法

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

程序设计思路

    定义一个员工类,具有性别,年龄和姓名属性,定义一个管理层类,继承员工类的属性和方法,然后再定义一个职员类,并继承员工类的属性和方法。


程序设计思路

  设计一个平面图形抽象类和一个立体图形抽象类,分别设计一个求面积,一个求周长的方法
和一个求表面积和体积的方法,然后分别设计三角形,圆和矩形,继承求周长和求面积的方
法,设计球,圆柱,圆锥继承求体积和表面积的方法。

遇到的问题

    类之间建立联系



代码托管
https://git.oschina.net/hebau_cs15/javacs02hxd.git

posted @ 2017-04-18 16:09  柠檬冬冬  阅读(135)  评论(0编辑  收藏  举报