Java单继承
2016-03-12 00:00
411 查看
摘要: Java单继承
In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those classes.
Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).
Excepting Object, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object.
Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object. Such a class is said to be descended from all the classes in the inheritance chain stretching back to Object.
A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. ——Java Documentation
Java单继承
Java允许类的单继承,接口的多继承,这里先看看类的单继承,可以通过创建几个不同的类继承同一个主类,实现多态。In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those classes.
Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).
Excepting Object, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object.
Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object. Such a class is said to be descended from all the classes in the inheritance chain stretching back to Object.
实验
package org.dc; import java.util.Arrays; /** * java中只允许单继承,所以可以通过构造两个不同的子类对于java的父类进项不同的维度的继承. */ class Processor {// 父类中没有出现static方法,所以父类的一切特性都可以被继承 public String name() { return getClass().getSimpleName(); /* * Returns the runtime class of this Object. The returned Class object * is the object that is locked by static synchronized methods of the * represented class. Returns the simple name of the underlying class as * given in the source code. Returns an empty string if the underlying * class is anonymous. */} Object process(Object input) { return input; } } class Uppercharacter extends Processor {// 继承Processor @Override Object process(Object input) { // 方法的重写构成了多态性,注意这里是重写不是重载! // 重写的方法在形式(参数类型)上与父类的方法一致,只是内容上有所变化,这体现了java的多态性 return ((String) input).toUpperCase(); /* * Converts all of the characters in this String to upper case using the * rules of the default locale */ } } class Splitregex extends Processor {// 继承Processor @Override Object process(Object input) {// 方法的重写构成了多态性 return Arrays.toString(((String) input).split(" ")); /* * Returns a string representation of the contents of the specified * array. Splits this string around matches of the given regular * expression. */} } public class UsingClassInheritance { public static void process(Processor p, Object input) {// has-a // 关系,直接调用已经封装好的Processor对象 /* * 父类类型的引用可以调用父类中定义的所有属性和方法,而对于子类中定义而父类中没有的方法,它是无可奈何的; * 同时,父类中的一个方法只有在在父类中定义而在子类中没有重写的情况下,才可以被父类类型的引用调用. * 对于父类中定义的方法,如果子类中重写了该方法,那么父类类型的引用将会调用子类中的这个方法,这就是动态连接. */ System.out.println("The runtime class of this Object is " + p.name()); System.out.println(p.process(input)); } public static String sentence = "Hello World!"; public static void main(String[] args) { process(new Uppercharacter(), sentence); process(new Splitregex(), sentence); } }
实验结果
The runtime class of this Object is Uppercharacter HELLO WORLD! The runtime class of this Object is Splitregex [Hello, World!] |
继承的目的
The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself.A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. ——Java Documentation
相关文章推荐
- java中单继承与多继承
- 深入理解Java之数据类型
- java内部类复习
- Java基础(环境设置,基础语法,函数数组)
- 20145320 《Java程序设计》第2周学习总结
- 开发维护一个大型Java项目的建议
- SpringMVC对Ajax的支持
- JAVA学习笔记01(增补)
- 【Java】ServerSocket的学习笔记
- Java习惯用法
- [转]World Wind Java开发之四——搭建本地WMS服务器
- java虚拟机类加载机制
- [转]World Wind Java开发之五——读取本地shp文件
- 二分图(Java)
- Rotate List | Java最短代码实现
- Java基础05-循环问题,数组
- struts2学习(一) 环境搭建以及简单的演示程序
- 第三方SDK:JPush SDK Eclipse
- java控件使用与布局
- java输出九乘九乘法表