1.学习使用思惟导图对Java面向对象编程的知识点(封装、继承和多态)进行总结。
封装java
继承git
多态编程
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(); } }
不能。构造函数调用必须是构造函数的第一个语句。
修改:eclipse
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(); } }
错误:this
animal.sleep(); Dog dog = animal; Animal animal2 = new Animal();
缘由:Animal中没有sleep方法,不能从Animal转换为Dog。animal2对象与dog对象未创建好关系,应在声明父类对象animal2时先发生向上转型关系。
修改以下:.net
class Animal{ void shout(){ System.out.println("动物叫!"); } public void sleep() { System.out.println("狗狗睡觉......"); } } class Dog extends Animal{ public void shout(){ System.out.println("汪汪......!"); } public void sleep() { System.out.println("狗狗睡觉......"); } } public class Test1{ public static void main(String args[]) { Animal animal = new Dog(); animal.shout(); animal.sleep(); Dog dog = (Dog) animal; dog.sleep(); Animal animal2 = new Dog(); 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方法,都会输出,并且输出的是随机地址。
(2)那么,程序的运行结果究竟是什么呢?利用eclipse打开println(per)方法的源码,查看该方法中又调用了哪些方法,可否解释本例的运行结果?
println(per)方法的源码
public void println(Object x) { String s = String.valueOf(x); synchronized (this) { print(s); newLine(); } }
又调用了valueOf方法、newLine()方法
解释:per在输出时先运行了valueOf,接着输出per.toString()时,直接就把上面的进行输出了。
(3)在Person类中增长以下方法
public String toString(){ return "姓名:" + this.name + ",年龄:" + this.age ; }
从新运行程序,程序的执行结果是什么?说明什么问题?
姓名:张三,年龄:20 姓名:张三,年龄:20
说明,object中自己就有一个toString方法,若是不在题中写toString方法,就会调用默认的方法。
1.员工类
2.计算类
3.喂养类
代码行数(新增/累积) | 学习时间(新增/累积) | 本周学习内容 | |
---|---|---|---|
目标 | 5000行 | 300小时 | |
第2-4周 | 100/100 | 20/20 | 学习了数组和方法 |
第5周 | 200/300 | 30/50 | 学习了String类和StringBuffer类 |
第6周 | 800/1100 | 40/90 | 学习了this、static关键字,Singleton模式 |
第八周 | 1200/1700 | 60/110 | 继承和多态,抽象方法 |