java 接口与继承
一、继承条件下的构造方法调用
运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent的另一个构造函数,注意这句调用代码是否是第一句,影响重大!
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() {
//super("Hello.Grandparent.");
System.out.println("Parent Created");
// super("Hello.Grandparent.");
}
}
class Child extends Parent {
public Child() {
System.out.println("Child Created");
}
}
public class TestInherits {
public static void main(String args[]) {
Child c = new Child();
}
}
2)运行结果截图
3)结论
通过 super 调用基类构造方法,必须是子类构造方法中的第一个语句。
一、参看ExplorationJDKSource.java示例
1) 源代码
public class ExplorationJDKSource {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(new A());
}
}
class A{}
2) 运行结果截图
3)结论
前面示例中,main方法实际上调用的是:
public void println(Object x),这一方法内部调用了String类的valueOf方法。
valueOf方法内部又调用Object.toString方法:
public String toString() {
return getClass().getName() +"@" +
Integer.toHexString(hashCode());
}
hashCode方法是本地方法,由JVM设计者实现:
public native int hashCode();
java 接口与继承的更多相关文章
- java接口可以继承多个接口
接口是常量值和方法定义的集合.接口是一种特殊的抽象类. java类是单继承的.classB Extends classA java接口可以多继承.Interface3 Extends Interf ...
- Java 接口多继承
按照理解,一般都是说Java 类是单继承,但可以实现多个接口.但是可以通过接口来实现类的多继承.(如何通过接口来实现多继承???) 那么就一直以为Java里面是单继承,今天看FutureTask源码的 ...
- JAVA接口的继承与集合
复习 20190701 接口补充 一. java是单继承多实现 单继承: 一个类只能有一个父类 public class D extends D1 { } 2. 多实现 一个类可以同时实现多个接口 当 ...
- JAVA 接口与继承作业——动手动脑以及课后实验性问题
一.继承条件下的构造方法调用 运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent的另一个构 ...
- Java接口与继承作业
为什么子类的构造方法在运行之前,必须调用父类的构造方法?能不能反过来?为什么不能反过来? 因为子类继承了父类,那么就默认的含有父类的公共成员方法和公共成员变量,这些方法和变量在子类里不再重复声明.如果 ...
- Java 接口与继承 道至简第六章发表阅读笔记
一.继承条件下的构造方法调用 class Grandparent { public Grandparent() { System.out.println("GrandParent Creat ...
- java接口与继承
class Grandparent { public Grandparent() { System.out.println("GrandParent Created."); } p ...
- Java:接口继承接口 (多继承机制)
在看API文档时,突然发现一个接口可以是其他接口的子接口,这说明接口之间会存在继承的关系.查找了相关的资料,做一个总结. 是继承还是实现 首先要搞清楚接口之间的关系使用的关键字是extends还是im ...
- java 类的继承和接口的继承
父类 public class person { String name; int age; void eat(){ System.out.println("吃饭"); } voi ...
随机推荐
- Hibernate get和load区别
1.从返回结果上对比:load方式检索不到的话会抛出org.hibernate.ObjectNotFoundException异常get方法检索不到的话会返回null 2.从检索执行机制上对比: ...
- The report for triangle problem
本次实验主要使用eclipse 编写三角形判定的代码,并用junit进行测试. 1.安装junit和hamcrest 下载junit-4.12.jar和hamcrest-all-1.3.jar 并且拖 ...
- 获取action name在asp.net mvc
Update for MVC 3 ViewContext.Controller.ValueProvider.GetValue("action").RawValue ViewCont ...
- [codeforces 317]A. Perfect Pair
[codeforces 317]A. Perfect Pair 试题描述 Let us call a pair of integer numbers m-perfect, if at least on ...
- linux中tar命令用法
把常用的tar解压命令总结下,当作备忘: tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其 ...
- ubuntu linux 下wine的使用
ubuntu linux 下wine的使用 之前写了一篇 ubuntu15.10下编译安装wine1.8rc4 这一篇是来写它的使用的. 1.安装Wine支持包 现在,需要安装非开源(但免费)的支持包 ...
- python字符串函数
- 1.8---字符串是否是旋转而成(CC150)
答案:利用了XY , YX中第一个XYXY包含了第二个 public class Solution{ public static void main(String[] args){ System.ou ...
- 无密码执行sudo指令
参考:http://askubuntu.com/questions/334318/sudoers-file-enable-nopasswd-for-user-all-commands 配置: 执行vi ...
- espcms内容页相册调用代码
{%forlist from=$photo key=i%} <li style="position: absolute; width: 600px; left: 0px; top: 0 ...