(一)学习总结git
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()语句放在输出语句以前。程序的运行结果为:安全
GrandParent Created.String:Hello.Grandparent. Parent Created Child Created
之因此调用父类方法,就是要用父类的构造方法为父类中的属性初始化,就表示现有父类实例而后才能产生子类实例。eclipse
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.sleep();有错,父类不能够调用子类的方法,Dog为子类,父类转为子类需强制转换。父类引用的对象是父类自己,在向下转型的过程当中是不安全的,运行有错。运行结果为:学习
汪汪......! 狗狗睡觉......
正确程序为:测试
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(); Dog dog = (Dog)animal; dog.sleep(); Animal animal2 = new Animal(); if (animal2 instanceof Dog){ dog = (Dog)animal2; dog.shout(); } } }
4.运行下列程序this
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)程序的运行结果以下,说明什么问题?.net
Person@166afb3 Person@166afb3
随机输出了一些地址信息
(2)那么,程序的运行结果究竟是什么呢?利用eclipse打开println(per)方法的源码,查看该方法中又调用了哪些方法,可否解释本例的运行结果?
public void println(Object x) { String s = String.valueOf(x);//若是x为空返回空,不为空返回调用toString的内容 synchronized (this) { print(s); newLine(); } }
(3)在Person类中增长以下方法
public String toString(){ return "姓名:" + this.name + ",年龄:" + this.age ; }
从新运行程序,程序的执行结果是什么?说明什么问题?
结果为:
姓名:张三,年龄:20 姓名:张三,年龄:20
子类覆写了父类的toString()方法,直接输出的是对象调用覆写的toString()方法。
(二)实验总结
实验内容:
1.程序设计思路:程序设计思路:定义员工类,具定义属性姓名、年龄、性别,定义每一个的setter、getter方法,定义管理层类继承员工类,定义本身的属性职务、年薪,定义职员类继承员工类,定义本身的属性所属部门和月薪。在定义一个测试类,进行输出测试
2.按照下面要求完成类的设计
程序设计思路:定义两个抽象类,分别为平面图形抽象类(提供求该类对象周长和面积的方法)和立体图形抽象类(提供求该类对象表面积和体积的方法),再分别定义一个球类、圆柱类,圆锥类、矩形类、三角形类、圆类,分别继承平面图形抽象类和立体图形抽象类。再定义一个测试类,对每一个类设置数据进行输出。
问题1:在输出的时候传参都为空
缘由:没有把参数传递过去
解决方案:对每一个方法都定义为有返回值的方法,而后传参,就传过去了。
3.程序设计思路:定义一个动物抽象类,定义一个eat()方法,分别定义狮子类,猴子类,鸽子类,继承动物类,子类里都定义有本身的属性食物,再定义一个饲养员类,定义有本身的属性姓名,和一个饲养动物的方法(用对象数组来完成),在定义一个测试类,下标来表示动物,定义其食物。
问题1:传参为空
缘由:将食物属性放到了抽象类里了
解决方案:分别在每一个类里定义食物属性,而后定义toString()方法来表示输出内容。
(三)[代码托管](https://git.oschina.net/hebau_cs15/Java-CS02yyh)
码云commit历史截图