《Java技术》第三次作业
(一)学习总结
1.阅读下面程序,分析是否能编译通过?如果不能,说明原因。应该如何修改?程序的运行结果是什么?为什么子类的构造方法在运行之前,必须调用父 类的构造方法?能不能反过来?
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.")”放在子类Parent的构造方法的第一句
-
子类默认调用父类无参构造方法,但是如果调用父类含参构造方法要用关键字super而且必须放在子类构造方法第一句。
-
不能反过来调用,父类并不知道子类有什么变量,子类也得不到初始化父类变量。
-
运行结果:
GrandParent Created.String:Hello.Grandparent.
Parent Created
Child Created
2.阅读下面程序,分析程序中存在哪些错误,说明原因,应如何改正?正确程序的运行结果是什么?
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();
}
}
-
向上转型只能调用子类继承或覆写的方法,子类继承了方法shout,但是并没有sleep
-
向下转型的格式为:子类名称 子类对象=(子类)父类实例,显然没有强制转换
修改:class Animal{ void shout(){ System.out.println("动物叫!"); } void sleep(){ 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 = (Dog)animal; dog.sleep(); Animal animal2 = new Dog(); dog = (Dog)animal2; dog.shout(); } }
3.运行下列程序
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)方法的源码,查看该方法中又调用了哪些方法,能否解释本例的运行结果?
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 ;
}
-
toString()方法返回一个字符串用来描述当前对象,返回的具体内容:类名@对象的hash码16进制表示
-
结果:
姓名:张三,年龄:20
姓名:张三,年龄:20 -
子类中完成了对父类的toString类的重写
4.汽车租赁公司,出租汽车种类有客车、货车和皮卡三种,每辆汽车除了具有编号、名称、租金三个基本属性之外,客车有载客量,货车有载货量,皮卡则同时具有载客量和载货量。用面向对象编程思想分析上述问题,将其表示成合适的类、抽象类或接口,说明设计思路。现在要创建一个可租车列表,应当如何创建? -
定义一个抽象类车辆,还有抽象方法承载量,分别定义客车、货车和皮卡三种类去继承车辆抽象类,对抽象类的中的抽象方法进行重写
-
定义租车类,声明该类的对象数组,通过向上转型分别实例化
5.阅读下面程序,分析代码是否能编译通过,如果不能,说明原因,并进行改正。如果能,列出运行结果
interface Animal{
void breathe();
void run();
void eat();
}
class Dog implements Animal{
public void breathe(){
System.out.println("I'm breathing");
}
void eat(){
System.out.println("I'm eating");
}
}
public class Test{
public static void main(String[] args){
Dog dog = new Dog();
dog.breathe();
dog.eat();
}
}
- 不能通过编译,接口中的方法,默认的是抽象方法, 在子类实现抽象方法时,用public即可
码云:https://gitee.com/hebau_java_cs16/Java_CS02NYJ/tree/master