java第四次作业1502班马浩加

(一)学习总结

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

不能通过编译 构造函数的调用必须是构造函数中的第一个语句,子类的构造方法必须先调用父类构造,再执行子类构造
修改为

class Parent extends Grandparent {
    public Parent() {         
        super("Hello.Grandparent.");
        System.out.println("Parent Created");
    }
}

修改后运行结果

GrandParent Created.String:Hello.Grandparent.
Parent Created
Child Created

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类向上转型为Aniaml类,所调用的sleep方法是被子类覆写的父类方法,而父类中没有sleep()方法.
animal.sleep();
应该改为:

class Animal{
  void shout(){
      System.out.println("动物叫!");
  }
  void sleep(){
      System.out.println("动物睡觉");
  }
}

问题二:Dog dog = animal;向下转型,需要强制转型,即必须明确指明要转型的子类类型
应该改为:

Dog dog = (Dog)animal;

问题三:Animal animal2 = new Animal(); 父类引用的对象是父类本身,在向下转型的过程中是不安全的,编译不会出错,但是运行时会出现java.lang.ClassCastException错误。
dog = (Dog)animal2;
应该改为:

Animal animal2 = new Dog();
        dog = (Dog)animal2;

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

说明了:System.out.println(per); per无论有没有调用toString()方法,都输出了类名和地址信息,都会调用object中toString()方法
System.out.println(per.toString()) ;
(2)那么,程序的运行结果到底是什么呢?利用eclipse打开println(per)方法的源码,查看该方法中又调用了哪些方法,能否解释本例的运行结果?

public void println(Object x) {
        String s = String.valueOf(x);
        synchronized (this) {
            print(s);
            newLine();
        }
    }
    
valueOf(x):

如果参数为空字符串,则返回值为空;否则,返回tostring()的返回值。
(3)在Person类中增加如下方法:

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

重新运行程序,程序的执行结果是什么?说明什么问题?
可参考教材P229
说明了“子类Person类覆写了Object类中toString()方法”。
(二)实验总结

实验内容:

1.定义员工类,具有姓名、年龄、性别属性,并具有构造方法和显示数据方法。定义管理层类,继承员工类,有自己的属性职务和年薪。定义职员类,继承员工类,并有自己的属性所属部门和月薪。定义一个测试类,进行测试。画出类图。
出现问题:
不太熟练继承类和如何调用。以员工类为父类,管理类和职员类为子类继承父类。
解决方案:一点点学习,对继承和如何调父类用有了更深入的了解,很熟悉了,具体测试类的代码如下:

public class test {
	public static void main(String[] args) {
		Yuangong yu = new Yuangong();
		yu.setName("李明");
		yu.setAge(30);
		System.out.println("****父类员工****");
		System.out.println("姓名:"+yu.getName()+" 年龄:"+yu.getAge());
		Manage ma = new Manage();
		ma.setName("刘芳");
		ma.setAge(28);
		ma.setDuty("员工");
		ma.setYearsalary(20000);
		System.out.println("****子类员工****");
		System.out.println("姓名:"+ma.getName()+" 年龄:"+ma.getAge()+" 职务:"+ma.getDuty()+" 年薪:"+ma.getYearsalary());
		Employee em = new Employee();
		em.setName("韩磊");
		em.setAge(25);
		em.setDepartment("市场部");
		em.setMonthsalary(5000);
		System.out.println("****子类员工****");
		System.out.println("姓名:"+em.getName()+" 年龄:"+em.getAge()+" 职务:"+em.getDepartment()+" 年薪:"+em.getMonthsalary());
	}

2.按照下面要求完成类的设计
(1)设计一个平面图形抽象类(提供求该类对象周长和面积的方法)和一个立体图形抽象类(提供求该类对象表面积和体积的方法)
(2)设计球类、圆柱类,圆锥类、矩形类、三角形类、圆类,分别继承平面图形抽象类和立体图形抽象类。
(3)建立测试类,进行测试。画出类图。
出现问题:不太会使用super关键字。
解决方案: 在子类重写父类方法后,要访问父类被重写的方法,需要用super关键字来引用当前类的父类。super的用法有两种情况:
(1) 访问的父类中的成员变量和成员方法 super.变量名
super.方法名([参数表])
(2)调用父类的构造方法
super([参数表])
如下是关于圆柱继承子类代码使用关键字super来调用父类实现:

class Yuanzhu extends Space{      /*圆柱的继承子类*/
	public Yuanzhu(double radius,double high){
		super(radius,high);
	}

(三)代码托管:https://git.oschina.net/hebau_cs15/mhj.git

posted @ 2017-04-17 12:39  马浩加同学  阅读(339)  评论(0)    收藏  举报