(一)学习总结
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()位置不对无法实现调用
调用父类的构造方法时应放在子类的构造方法的第一句
应该super()放在类构造方法的第一句
- 修改
class Parent extends Grandparent {
public Parent() {
super("Hello.Grandparent.");
System.out.println("Parent 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();
}
}
- 不可执行 如图
- 原因
父类中没有sleep无法继承 却直接写animal.sleep()
不能从 Animal 转换为 Dog 需要强制转型明确对象
声明父类中没有实例
- 修改为
class Animal{
void shout(){
System.out.println("动物叫!");
}
void sleep(){
}
}
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 =(Dog) animal;
dog.sleep();
Animal animal2 = new Animal();
Dog a=new Dog();
animal2=a;
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
- Preson类没有明显的继承一个类 ,则成为Object的子类。
输出的 Per 和Per.toString 都将输出得到相同的内存地址 调用了父类Obiect中的toString()方法
(2)那么,程序的运行结果到底是什么呢?利用eclipse打开println(per)方法的源码,查看该方法中又调用了哪些方法,能否解释本例的运行结果?
public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}
(3)在Person类中增加如下方法
public String toString(){
return "姓名:" + this.name + ",年龄:" + this.age ;
}
重新运行程序,程序的执行结果是什么?说明什么问题?
- 程序的Person中覆写了Object类中的to String()方法,输出对象时调用的是被子类覆写过的toString()方法
5.自我总结
- 学习了面向对象中继承类 多态类 抽象类等内容 和以前学的内容联系不大 写起程序来比以前顺了很多
虽然和其他同学比起来还有很多不足 作业完成的也会太好 但会更加努力 多写代码。
(二)实验总结
1.员工信息的设计
- 程序设计思路:
利用继承的方法 定义父类和三个子类
定义员工类 声明员工信息,再定义管理类和职员类 分别声明职务年薪和部门运行
同时继承父类中员工的信息- 问题:子类中无法继承调用出员工的信息
- 解决方案:
定义super()
输出时 return 后需加super.toString ()+其他
- 类图
2.几何图形的设计
- 程序设计思路:
利用抽象类和继承的方法,定义两个抽象类和六个子类
定义三个平面图形类写出计算方法继承平面图形抽象类
定义三个立体图形类写出计算方法继承立体图形抽象类- 问题:
开始写时不理解抽象类的使用
Test类中不会写输出方法- 解决方案: 看老师课件
- 类图
(三)代码托管
- 链接 :https://git.oschina.net/hebau_cs15/Java-CS02lcx.git