为何子类的构造方法在运行以前,必须调用父 类的构造方法?能不能反过来?java
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(); } }
不一样经过编译,由于在parent构造方法中,调用父类构造方法没有放在第一行。
应如何修改:将 super("Hello.Grandparent.")放在parent构造方法的第一行。
运行结果: GrandParent Created.String:Hello.Grandparent.
Parent Created
Child Created
构造一个对象,先调用其构造方法,来初始化其成员函数和成员变量。
子类拥有父的成员变量和成员方法,若是不调用,则从父类继承而来的成员变量和成员方法得不到正确的初始化。
不能反过来调用也是这个缘由,由于父类根本不知道子类有神魔变量并且这样一来子类也得不到初始化的父类变量,致使程序运行出错!git
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方法。 将其去掉
第二行错误的缘由是下转型须要加上“(类型)“, 改正:Dog dog =(Dog) animal;编程
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
缘由:对象输出时必定会调用Object类中的toString方法打印内容,直接输出函数名默认调用toString方法。
(2)那么,程序的运行结果究竟是什么呢?利用eclipse打开println(per)方法的源码,查看该方法中又调用了哪些方法,可否解释本例的运行结果?
print方法中调用了write方法,打印了类自己。
(3)在Person类中增长以下方法eclipse
public String toString(){ return "姓名:" + this.name + ",年龄:" + this.age ; }
从新运行程序,程序的执行结果是什么?说明什么问题?
结果函数
姓名:张三,年龄:20 姓名:张三,年龄:20
覆写了toString方法,调用的为覆写后的方法,打印类名也默认调用覆写后的方法。this
客车有载客量,货车有载货量,皮卡则同时具备载客量和载货量。用面向对象编程思想分析上述问题,
将其表示成合适的类、抽象类或接口,说明设计思路。如今要建立一个可租车列表,应当如何建立?
解:定义一个抽象类(或者接口)“出租汽车”,有抽象方法承载量,定义客车类、火车类、皮卡类去继承出租汽车(或者实现接口),实现父类中的抽象方法,
客车实现抽象方法,打印载客量,货车实现抽象方法,打印载货量,皮卡实现抽象方法,打印载客量和载货量,
定义租车类,声明出租汽车类数组,经过构造方法中参数以及上转型初始化。设计
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 abstract 因此在子类实现抽象方法时,应该用public修饰
而且须要实现接口中全部的抽象方法,这里须要再实习接口中的run方法。code
在宠物商店一题中,商店类中将选中的宠物记录在购买清单的数组中,不要直接赋值引用,声明一个宠物类(须要用到instanceof,下转型),
将宠物信息赋值一份,修改下数量,存在数组,能够在重载一个构造方法,参数是自身类,做用是实现自身的复制。
Data类中的toString方法返回的字符串格式是英文月份那种格式,不是yyyy-MM-dd。
在进行比较排序时,能够经过自定义格式(yyyy-MM-dd)的字符串直接比较,也能够getTime(返回自 1970 年 1 月 1 日 00:00:00 GMT
以来此 Date 对象表示的毫秒数)进行比较。对象
https://gitee.com/hebau_java_cs16/Java_CS02tx